[题解]CSP-报数

文章目录


题目链接.

算法思路

甲、乙、丙、丁分别编号0、1、2、3,用取模运算来记录是甲、乙、丙、丁中的哪一个人。若满足要求就跳过,这时用n++来表示这一次不计入报出的数。对判断是否报出的数来说,也是用取模运算求得每一位数字。

//第五次实验 5.
#include<iostream>
using namespace std;

int n, person = 0;
int jump[4] = {}; //四人跳过的次数; 甲,乙,丙,丁:0, 1, 2, 3

bool jump_over(int x);

int main()
{
	int i;
	scanf("%d", &n);
	for ( i = 1; i <= n; i++)
	{
		if (jump_over(i)) //跳过
		{
			jump[person]++;
			n++;//这次不算
		}
		person++;
		person = person % 4;
	}
	for ( i = 0; i < 4; i++)
	{
		printf("%d\n", jump[i]);
	}
	
	return 0;
}

bool jump_over(int x) //是否跳过
{
	//是 7 的倍数
	if (x == (x / 7) * 7)
	{
		return true;
	}
	//含有 7
	while (x)
	{
		if (x % 10 == 7) // x 的个位数
		{
			return true;
		}
		x = x / 10;
	}
	return false;
}

结果分析

得分100分,用时0ms,空间使用2.941MB,时间复杂度为 O(n)。

猜你喜欢

转载自blog.csdn.net/weixin_44092088/article/details/110285605
今日推荐