//题目16:输入两个正整数m和n,求其最大公约数和最小公倍数。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//分析:最大公约数--取2个数中最小的一个数,for循环,m%i==0&&n%i==0
//最大公倍数是m*n,m*i%n==0
//辗转相除法:如果两个数有最大公约数A,那么这两个数,以及这两个数的差,还有大数除以小数的余数,必然都是A的倍数.
//所以当最后两个数刚好能整除时, 较小的数就是最大公约数.
void main(){
int m, n;
scanf("%d%d",&m,&n);
printf("\n");
int mina = 0;
int maxa = 0;
//temp是取两个数的小的
int temp = m > n ? n : m;
//temp2是取两个数的大的
int temp2 = m + n - temp;
int temp3 = 0;
//方法1
/*for (int i = temp; i > 0; i--)
{
if ((m%i==0)&&(n%i==0))
{
maxa = i;
break;
}
}
for (int i = 1; i <=temp2; i++)
{
if (temp*i%temp2 == 0)
{
mina = temp*i;
break;
}
}*/
//方法2
while (temp != 0){//直到小数是0为止,那么大数就是最大公约数
//辗转相除法的使用
temp3 = temp2%temp;//大数除小数,取余
temp2 = temp;//小数赋值给大数
temp = temp3;//余赋值给小数
}
maxa = temp2;
//在已知最大公约数的情况下,最小公倍数就等于m*n/maxa
mina = m*n / maxa;
printf("\n最大公约数是%d,最小公倍数是%d", maxa,mina);
system("pause");
}
![]()
//题目17:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//分析:将字符串存入字符数组,用for分别检索英文字母、空格、数字和其它字符
//char型可以转成Int类型,通过ASCII表就可以得出数字的范围时48~57;字母的范围是65~90;97~122;空格是32
void main(){
char str[30] = "adfa-123 12 asdf'sad13";
int num = 0;
int ch = 0;
int nul = 0;
int other = 0;
int temp = 0;
for (int i = 0; i < 30; i++)
{
if (str[i]=='\0')
{
break;
}
else{
temp = (int)str[i];
if (temp>47 && temp<58)
{
num++;
}
else if ((temp>64 && temp<91) || (temp>96 && temp < 123)){
ch++;
}
else if (temp==32)
{
nul++;
}
else{
other++;
}
}
}
printf("\n数字的个数%d,字母的个数%d,空格的个数%d,其他字符的个数%d。",num,ch,nul,other);
system("pause");
}
![]()
//题目18:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时
//共有5个数相加),几个数相加有键盘控制。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//分析:键盘输入数字,决定相加的个数
//
int getnum(int num){
int a = 2;
int res = 0;
for (int i = num; i >-1; i--)
{
res += a*(int)(pow(10, i));
}
return res;
}
void main(){
int num = 0;
scanf("%d",&num);
int s = 0;
//方法1
/*for (int i = 0; i <num; i++)
{
s += getnum(i);
}*/
//方法2
int count = 0;
int a = 2;
int tn = 0;
while (count < num){
//每次实现加的那个数的值,a永远是200..0,tn是2222,两者相加 就变成正确的数
tn = tn + a;
s += tn;
a = a * 10;
count++;
}
printf("\n%d",s);
system("pause");
}
![]()