程序设计编程练习题解题报告(第5周)

《程序设计方法(C\C++)》是北京理工大学的一门课程,该课通过北京理工大学网络教室评测的编程练习题计算平时成绩。

15 谁能出线

核心思想:搜索最大的数字,输出下标。


#include <stdio.h>

int main() {
    int scores[10];
    int maxScore,i;
    maxScore=0;
    for (i=0;i<10;i++) {
        scanf("%d",&scores[i]);
        if (maxScore<scores[i])
            maxScore=scores[i];
    }
    for (i=0;i<10;i++)
        if (scores[i]==maxScore)
            printf("%d\n",i);
    return 0;
}

16 等值数列段

核心思想:枚举起点,对于每个起点找尽可能远的等值数列,然后与已知最大的比较。

#include <stdio.h>

int main() {
    int i,j,n;
    int start,end,len;
    len=0;
    scanf("%d",&n);
    int seq[n];
    for (i=0;i<n;i++)
        scanf("%d",&seq[i]);
    for (i=0;i<n;i++) {
        for (j=i+1;j<n;j++)
            if (seq[i]!=seq[j])
                break;
        if (len<j-i) {
            start=i;
            end=j-1;
            len=j-i;
        }
    }
    if (len>1)
        printf("The longest equal number list is from %d to %d.\n",start,end);
    else
        printf("No equal number list.\n");
    return 0;
}

17 大家一起做游戏

核心思想:模拟题中叙述的过程,使用一个变量不断地轮流地扫描这N的人,使用一个数组记录其是否被踢出,并用另一个变量统计其在每一轮中实际的序号(即此变量只在扫到没被踢出的元素是自增)。

#include <stdio.h>

int main() {
    int i,j,k,n,number;
    scanf("%d %d",&n,&number);
    int children[n];
    for (i=0;i<n;i++)
        children[i]=i+1;
    k=n-1;
    for (i=1;i<n;i++) {
        j=0;
        while (j<number) {
            k++;
            k%=n;
            if (children[k])
                j++;
        }
        children[k]=0;
    }
    for (i=0;i<n;i++)
        if (children[i])
            printf("The left child is NO %d.\n",i+1);
    return 0;
}

Posted

in

, ,

by

Tags:

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.