ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
①输入两个正整数m和n,求其最大公约数和最小公倍数;
②输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数
③求Sn a+aa+aaa+aaaa+......aa..a(n个a)的值,其中a是一个数字,n表示a的位数
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
1、输入两个正整数m和n,求其最大公约数和最小公倍数;
#include<stdio.h>
#include<math.h>
//输入两个正整数m和n,求其最大公约数和最小公倍数;
//方法:辗转相除法
int main()
{
int m, n; //两个正整数m和n
int p, k; //最小公倍数,余数
printf("please enter two positive integers: \n");
scanf("%d,%d",&m,&n);
p = m * n;
while (k = m%n){
m = n;
n = k;
}
p = p / n;
printf("最大公约数是:%d\n",n);
printf("最小公倍数是:%d\n",p);
return 0;
}
結果:
please enter two positive integers:
3,6
最大公约数是:3
最小公倍数是:6
2、输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数
#include<stdio.h>
#include<math.h>
//输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数
int main()
{
char c;
int letters = 0, spaces = 0, digitals = 0, others = 0;
printf("输入一行字符:");
while ((c = getchar())!= '\n'){
if (c>='a'&& c<='z' || c>='A' && c <= 'Z'){
letters++;
}
else if (c == ' '){
spaces++;
}
else if (c >= '0' && c <= '9'){
digitals++;
}
else
{
others++;
}
}
printf("the number of letters:%d\n",letters);
printf("the number of spaces:%d\n",spaces);
printf("the number of digitals:%d",digitals);
printf("the number of others:%d",others);
return 0;
}
3、//求Sn a+aa+aaa+aaaa+......aa..a(n个a)的值,其中a是一个数字,n表示a的位数,例如
//2+22+222+2222+22222(此时a5)
//n由键盘输入
#include<stdio.h>
#include<math.h>
int main()
{
int a, n,sum=0;
printf("please enter the number of a and n:");
scanf("%d,%d",&a,&n);
for (int i = 0; i < n;i ++){
sum += a;
a = a * 10 + a;
}
printf("the sum=%d\n", sum);
return 0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。