第一次写`
#include <cstdio>
#include <cstring>
bool judge(int n);
int main(){
int a[4][2], count = 0, n, i = 1;
memset(a, 0, sizeof(a));
scanf("%d", &n);
int xiabiao = 0;
while (count < n){ //不应该是<= 这样会多运行一次循环!!
a[xiabiao][0] = i++;
if (judge(a[xiabiao][0])){
a[xiabiao][1]++;
}else count++;
xiabiao++;
if (xiabiao == 4){
xiabiao = 0;
}
}
for (int i = 0; i < 4; i++){
printf("%d\n", a[i][1]);
}
return 0;
}
bool judge(int n){
if (n % 7 == 0) return true;
while (n != 0){
if (n % 10 == 7) return true;
n /= 10;
}
return false;
}
改进后的收获:
可以改为一维数组,通过i就可以确定数组下标
#include <stdio.h>
bool inclu(int n);
int main(){
int count[4] = {0};
int i = 0; //这里是0而不是1!!
int total = 0;
int n;
scanf("%d", &n);
while (total < n){ //不应该是<= 这样会多运行一次循环!!
i++;
if (inclu(i)){
count[(i - 1) % 4]++; //由i可以确定下标,不需要创建二维数组
// 0 1 2 3 下标
// 1 2 3 4 值 如何对应上?(i - 1) % 4即可 !!
// 5 6 7 8
}else total++;
}
for (int i = 0; i < 4; i++){
printf("%d\n", count[i]);
}
return 0;
}
bool inclu(int n){
if (n % 7 == 0) return true;
while (n != 0){
if (n % 10 == 7) {
return true;
}
n /= 10;
}
return false;
}