1到 100 的所有整数中出现多少次数字9

分析:

定义一个变量time=0,利用for循环遍历1-100,若i除以10余数为9/商为9,则time加1。

注意:99中9出现了两次,可以利用其除以10的余数为9且商也为9,进行运算。

#include<stdio.h>
#include<stdlib.h>

int main(){
	int time = 0;
	int i = 1;
	for (i; i < 101; i++){
		if(i%10==9){
			time += 1;
		}	
		if (i / 10 == 9){
			time += 1;
		}
	}
	printf("%d\n", time);
system("pause");
return 0;
}

运行结果:

猜你喜欢

转载自blog.csdn.net/qq_42142477/article/details/82856992
今日推荐