2019-12-1 报数

在这里插入图片描述
在这里插入图片描述
第一次写`

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值