第一次csp认证准备20191201报数
代码(100分)如下:
#include <iostream>
using namespace std;
bool judge(int num)
{
//判断数字是否能报
if(num%7==0)
return false;
while(num)
{
if(num%10==7)
return false;
num/=10;
}
return true;
}
int main()
{
int n;
cin>>n;
int count=0;//记录读的次数
int num=1;//记录报数
int result[4]= {0}; //记录跳过的次数
for(int i=0; count<n; )
{
if(!judge(num++))//跳过
result[i]++;
else
count++;
i++;
i%=4;
}
for(int i=0; i<4; i++)
cout<<result[i]<<endl;
return 0;
}
但是不知道为什么,用while循环,就只有80分,如下:
#include <iostream>
using namespace std;
bool judge(int num)
{
//判断数字是否能报
if(num%7==0)
return false;
while(num)
{
if(num%10==7)
return false;
num/=10;
}
return true;
}
int main()
{
int n;
cin>>n;
int count=0;//记录读的次数
int num=1;//记录报数
int result[4]= {0}; //记录跳过的次数
while(count<n)
{
for(int i=0; i<4; i++)
{
if(!judge(num++))//跳过
result[i]++;
else
count++;
}
}
for(int i=0; i<4; i++)
cout<<result[i]<<endl;
return 0;
}