您的位置:首页 > 理论基础

法133计算机科学课第12周实践题目及参考解答

2013-11-12 20:21 134 查看
课程主页在:/article/1405311.html

Problem A:2447: 求N组数的最大公约数
Description
计算一组数的最大公约数
Input
第一行是数据的组数N,从第二行是N组由两个整数(a和b)构成的输入,a和b之间用空格隔开,每组输入单独占一行
Output
每组的两个整数(a和b)的最大 公约数,每个结果独占一行
Sample Input
3
98 72
80 36
12 144
Sample Output
2
4
12
算法提示:



参考解答:
#include<stdio.h>
int main()
{
    int n,i,r;
    int a,b;
    freopen("input.txt","r",stdin);
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        scanf("%d%d",&a,&b);
        while(b!=0)
        {
            r=a%b;
            a=b;
            b=r;
        }
        printf("%d\n",a);
    }
    return 0;
}


Problem B:2448: 分离正整数中的各位数
Description
输出正整数的各位数
Input
若干个用空格隔开的正整数
Output
每个正整数的各位数字,个位数在前,十位数紧随,最高位在最后,每位数后面有一个空格。每个正整数对应的输出占一行。
Sample Input
123 9523 89
Sample Output
3 2 1
3 2 5 9
9 8
算法提示:



参考解答:
#include<stdio.h>
int main()
{
    int n,a;
    freopen("input.txt","r",stdin);
    while(scanf("%d",&n) != EOF)
    {
        a=n;
        while(a>0)
        {
            printf("%d ",a%10);
            a=a/10;
        }
        printf("\n");
    }
    return 0;
}


Problem C:2449: 刑警的射击成绩
Description
刑警培训结束,进行了射击科检验。教官要对学员射击的成绩进行分析,得出各分数段人数统计。
Input
输入若干个0-10间的整数(最高10环,脱靶为0)表示成绩,人数不确定,输入以一个0-10以外的数作为。
Output
各分数段(A:9环以上,B:7环以上,C:5环以上,D:不足5环)的人数,每项成绩占一行
Sample Input
9 7 3 5 8 5 6 7 9 10 0 6 99
Sample Output
A:3
B:3
C:4
D:2
参考解答:
#include<stdio.h>
int main()
{
    int score, a=0, b=0, c=0, d=0;
    freopen("input.txt","r",stdin);
    scanf("%d",&score);
    while(score>=0&&score<=10)
    {
        if(score>=9) a++;
        else if(score>=7) b++;
        else if(score>=5) c++;
        else d++;
        scanf("%d",&score);
    }
    printf("A:%d\n",a);
    printf("B:%d\n",b);
    printf("C:%d\n",c);
    printf("D:%d\n",d);
    return 0;
}

解2:
#include<stdio.h>
int main()
{
    int score, a=0, b=0, c=0, d=0;
    freopen("input.txt","r",stdin);
    while(scanf("%d",&score)&&(score>=0&&score<=10))
    {
   if(score>=9) a++;
        else if(score>=7) b++;
        else if(score>=5) c++;
        else d++;
    }
    printf("A:%d\n",a);
    printf("B:%d\n",b);
    printf("C:%d\n",c);
    printf("D:%d\n",d);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: