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

课程主页在: http://blog.csdn.net/sxhelijian/article/details/13705597

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;
}

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迂者-贺利坚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值