栗酱数数

链接:https://ac.nowcoder.com/acm/challenge/terminal
来源:牛客网

题目描述
栗酱在酒桌上玩一个小游戏,第一个人从1开始数数,如果遇到数字中含4或者数字是4的倍数则跳过报下一个,谁数错了就要罚酒一杯。

所以栗酱想让你写个程序把所有数生成出来,这样她就可以作弊直接读了。你一定能解决的吧?

输入描述:
只有一组数据,一个数n代表从1开始数到n。(n≤100000)
输出描述:
按顺序输出所有1到n之间任何一位都不是4的数,每两个数之间用一个回车隔开。
示例1
输入
复制
9
输出
复制
1
2
3
5
6
7
9

题目思路:

利用数组进行所有数据的判断,再输出

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define ll long long
using namespace std;

int main() {
	int n;
	while (cin >> n) {
		int b[100005];
		memset(b, 0, sizeof(b));
		for (int i = 1; i <= n; i++) {
			if (i % 4 == 0) {//把是4的倍数的=1
				b[i] = 1;
				continue;//continue防超时
			}
			int a = i;
			while (a != 0) {
				if (a % 10 == 4) {
					b[i] = 1;//把带4的=1
					break;//break防超时
				}
				a = a / 10;
			}
		}
		for (int i = 1; i <= n; i++) {
			if (b[i] == 0) {
				cout << i << endl;
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44231195/article/details/89286622
今日推荐