实验22:统计字符频数,请把参考代码中的for循环改成while循环
https://www.dotcpp.com/oj/problem1012.html
参考代码:
#include
int main()
{
char a;
int n1=0;//数字个数
int n2=0;//字母的个数
int n3=0;//其他字符个数
for(;(a=getchar())!='\n';)
{
printf("%c\n",a);
if( a>='0'&& a='a'&&a='A'&&a<='Z'))
{
n2=n2+1;
}
else
{
n3=n3+1;
}
}
}
return 0;
}
实验24:求相同的余数
https://www.dotcpp.com/oj/problem3011.html
使用穷举法,从2开始,2、3、4、5、6、7、依次递增
穷举多少次呢??
一直穷举,一直到某个x满足 a%x==b%x && b%x==c%x,我就跳出循环
参考代码:
#include
int main() {
int x=2;
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
while(1)
{
//判断是否满足条件a%x==b%x && b%x==c%x,如果满足说明x找到了,跳出循环
}
return 0;
}
实验25:求因数之和(嵌入continue)
https://www.dotcpp.com/oj/problem1767.html
请大家在条件语句中增加continue,要求不影响程序运行结果
1、特殊法,先取N=20这个特殊情况
2、循环?次 1、2、3、4、5、6。。。。。、20
循环20 次
每次循环中要找出能被7整除的数,并求和
3、从特殊到一般
参考代码:
#include
int main() {
int i=1;
int N;
scanf("%d",&N);
int he=0;
while(i<=N)
{
if(i%7==0)
{
//printf("%d\n",i);
he=he+i;
}
i=i+1;
}
printf("%d",he);
return 0;
}
实验26:求1-N所有数的因子(循环嵌套、双层循环)
https://www.dotcpp.com/oj/problem1769.html
把参考代码中的while修改为for循环
参考代码;
#include
int main()
{
int i=1;
int N;
scanf("%d",&N);
int X=1;
while(i<=N)
{
printf("%d ",X);
int j=1;
while(j<=(X/2))
{
if(X%j==0)
{
printf("%d ",j);
}
j++;
}
printf("\n");
X++;
i++;
}
return 0;
}