试题编号: 201912-1
试题名称: 报数
时间限制: 1.0s
内存限制: 512.0MB
问题描述:


解题思路:
用模拟法解决。变量num用于计数,变量count用于报出的数的个数,数组cnt[]用于统计每个人的跳过的次数。
答案程序:
#include<iostream>
using namespace std;
bool have7(int n)
{
while(n)
if(n % 10 == 7)
return 1;
else n /= 10;
return 0;
}
int main()
{
int n, num = 1, count = 1,N = 4, cnt[N];
cin>>n;
while(count<=n) {
if(num % 7 == 0 || have7(num))
cnt[(num-1)%N]++;
else
count++;
num++;
}
for(int i=0;i<N;i++)
cout<<cnt[i]<<endl;
return 0;
}
结果:

本文介绍了如何使用C++来解决报数问题,通过模拟法进行编程,利用变量记录报数状态和次数,并给出了解题思路及答案程序。
828

被折叠的 条评论
为什么被折叠?



