ccf-csp #201912-1 报数

201912-1

题目思路

题意在题目描述中已经表达得很清楚了,如果注意读题,不遗漏信息,这应该是一道妥妥的水题。

设变量 c n t cnt 表示报出了多少个数字(不计入被跳过的数),变量 n u m num 表示包含跳过的数,当前报到多少,数组 a a 记录四个人被跳过的次数,函数 c h e c k check 检查 n u m num 是否为包含7或者为7的倍数的数。

我们只要去模拟这个报数的过程,不断递增 n u m num 的值,同时用 c h e c k check 函数检查是否需要跳过,如果不需要跳过就增加 c n t cnt 的值,否则增加被跳过的次数。直到 c n t cnt 的值等于 n n

我们发现, ( n u m 1 ) % 4 = = 0 (num-1)\%4==0 的数是轮到甲报的, ( n u m 1 ) % 4 = = 1 (num-1)\%4==1 的数是轮到乙报的……,于是我们就可以在他们被跳过时,用 a [ ( n u m 1 ) % 4 ) ] + + a[(num-1)\%4)]++ 进行计数。

代码如下

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3 + 10;
int n, a[10];

/**
  * 检查n是否为包含7或者为7的倍数的数
  * 如果是就返回0,否则返回1
  */
int check(int n) {
	if (n % 7 == 0) return 0;
	while (n) {
		if (n % 10 == 7) return 0;
		n /= 10;  
	}
	return 1;
}

int main()
{
	scanf("%d", &n);
	int cnt = 0, num = 0;
	while (1) {
		num++;
		if (check(num)) {
			cnt++;
		} else {
			a[(num - 1) % 4]++;
		}
		if (cnt == n) break;
	}
	for (int i = 0; i < 4; i++) 
		printf("%d\n", a[i]);
	return 0;
}
发布了59 篇原创文章 · 获赞 103 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42292229/article/details/103579203