输入一个整数,若达到3个7则输出good luck,否则输出包含数字7的个数
#include<stdio.h>
#include<string.h>
int main() {
int n;
scanf("%d",&n);
int count = 0;
while(n > 0){
if(n % 10 == 7){
count++;
}
n /= 10;
}
if(count == 3){
printf("good luck");
}else{
printf("%d",count);
}
return 0;
}
按照题意,不知道多少位的一个整数,你需要循环取余除10,分解出每一位,然后判断是不是7,是7count++,最后判断count>2
如果只追求结果正确,不要过程,显然定义一个char数组来放字符要简单的多
供参考:
#include <stdio.h>
int main()
{
int n, cnt = 0;
scanf("%d", &n);
while (n) {
if (n % 10 == 7) cnt++;
n /= 10;
}
if (cnt >= 3)
printf("good luck");
else
printf("%d", cnt);
return 0;
}
若达到3个7===这是啥意思?是整数中有3个7吗? 有4个7怎么处理?