题解:P13185 [GCJ 2016 Qualification] Counting Sheep
lihaojun120113 · · 题解
前置准备
一个可能对你新的函数:
string s=to_string(now);
这句代码的作用是把一个数字转化为字符串。
题目解析与注意事项
给定一个起始数字 INSOMNIA。
解题步骤:
-
判断最特殊的情况——当
N=0 时输出INSOMNIA。 -
从
N 开始,依次检查N, 2N, 3N, \cdots 的数字组成,记录已经出现过的数字。 -
当所有数字全部出现过时,返回当前数字。
注意:每一个测试案例时都要初始化!
代码
#include <bits/stdc++.h>
using namespace std;
int T,n;
int main(){
cin>>T;
for(int i=1;i<=T;i++){
cin>>n;
if(n==0){
cout<<"Case #"<<i<< ": INSOMNIA" << endl;
continue;
}
int vis[15]={0};//注意每次都要初始化哟
int now=n;
while(1){
string s=to_string(now);//把数字转为字符串的高级函数
for(int j=0;j<s.length();j++){
char c=s[j];
if('0'<=c&&c<='9'){
vis[c-'0']=1;
}
}
int count=0;
for(int j=0;j<=9;j++){
if(vis[j]) count++;
}
if (count==10) {
cout<<"Case #"<<i<<": "<<now<<endl;
break;
}
now+=n;
}
}
}
结束语
欢迎大家为本题解提出宝贵意见!