10000到99999中不含4的数的个数(两种方法)
对所需判断数的每一位数字进行判断:
#include <stdio.h>
int main() {
int count = 0;
for (int num = 10000; num <= 99999; num++) {
int temp = num;
int has_four = 0;
while (temp > 0) {
int digit = temp % 10;
if (digit == 4) {
has_four = 1;
break;
}
temp /= 10;
}
if (!has_four) {
count++;
}
}
printf("%d\n", count);
return 0;
}
把所需判断数转化为字符串进行判断:
#include <stdio.h>
int main(){
int count=0;
char str[8];
for(int i=10000;i<=99999;i++){
sprintf(str,"%d",i);
for(int j=0;j<5;j++){
if(str[j]=='4'){
count++;
break;
}
}
}
printf("%d",90000-count);
return 0;
}
#include <stdio.h>
int main() {
int count = 0;
for (int num = 10000; num <= 99999; num++) {
int temp = num;
int has_four = 0;
while (temp > 0) {
int digit = temp % 10;
if (digit == 4) {
has_four = 1;
break;
}
temp /= 10;
}
if (!has_four) {
count++;
}
}
printf("%d\n", count);
return 0;
}
把所需判断数转化为字符串进行判断:
#include <stdio.h>
int main(){
int count=0;
char str[8];
for(int i=10000;i<=99999;i++){
sprintf(str,"%d",i);
for(int j=0;j<5;j++){
if(str[j]=='4'){
count++;
break;
}
}
}
printf("%d",90000-count);
return 0;
}
全部评论
相关推荐