7-31至7-35

7-31 输出最大公约数(应用break语句) (100 分)

输入正整数m和n,输出它们的最大公约数,要求在循环中应用break语句。

输入样例:

36 24

输出样例:

12
#include <stdio.h>
int main(){
    int a;
    int b;
    int r=1;
    scanf("%d%d",&a,&b);
    while(1){
        r = a % b;
        a = b;
        b = r;
        if(r==0) break;
    }
    printf("%d\n",a);
    return 0;
}

 

7-32 输出ASCII码 (100 分)

输入一串字符(以#字符结束),依次输出每个字符及其ASCII码(不包括结束符#)。

输入格式:

一串字符以#结束。

输出格式:

按样例格式输出。

输入样例:

A1?!#

输出样例:

A-65
1-49
?-63
!-33
#include <stdio.h>
#include <math.h>

int main(){
    char ch;
    scanf("%c",&ch);
    while(ch!='#'){
        printf("%c-%d\n",ch,ch);
        scanf("%c",&ch);
    }
    return 0;
}

 

7-33 输出数字和 (100 分)

输入一串字符(以#字符结束),输出这串字符中所有数字字符的和。

输入样例:

ABC123DE4FG#

输出样例:

10
#include <stdio.h>
#include <math.h>

int main(){
    char ch;
    scanf("%c",&ch);
    int sum = 0;
    while(ch!='#'){
        if(ch>='1'&&ch<='9') sum+=ch-48;
        scanf("%c",&ch);
    }
    printf("%d",sum);
    return 0;
}

 

7-34 不能被2、3、5、7和13整除的数 (100 分)

输入整数a和b,将a,b之间(包括a、b本身)的不能被2、3、5、7和13整除的数输出。

输入样例:

100 200

输出样例:

101 103 107 109 113 121 127 131 137 139 149 151 157 163 167 173 179 181 187 191 193 197 199
#include <stdio.h>
#include <math.h>

int main(){
    int a,b;
    scanf("%d%d",&a,&b);
    for(int i=a;i<=b;i++)
    {
        if(i%2!=0&&i%3!=0&&i%5!=0&&i%7!=0&&i%13!=0)
            printf("%d ",i);
    }
    return 0;
}

 

7-35 输出最大公约数(应用continue语句) (100 分)

编程输入正整数m和n,输出它们的最大公约数,要求在循环中应用continue语句。

输入样例:

24 36

输出样例:

12
#include <stdio.h>
int main(){
    int a;
    int b;
    int r=1;
    scanf("%d%d",&a,&b);
    while(1){
        r = a % b;
        a = b;
        b = r;
        if(r!=0) continue;
        break;
    }
    printf("%d\n",a);
    return 0;
}

 

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值