You are teaching kindergarten! You wrote down the numbers from 1 to n, in order, on a whiteboard.
When you weren’t paying attention, one of your students erased one of the numbers.
Can you tell which number your mischievous student erased?
The first line of input contains a single integer n (2 ≤ n ≤ 100), which is the number of numbers that you wrote down.
The second line of input contains a string of digits, which represents the numbers you wrote down (minus the one that has been erased). There are no spaces in this string. It is guaranteed to contain all of the numbers from 1 to n, in order, except for the single number that the student erased.
Output a single integer, which is the number that the tricky student erased.
样例说明
第一行输入一个n代表接下来有1-n, 一共n个数,在其中删除一个,求删除的那一个数
思路
先存储1-100序列的所有的字符s,再和输入的字符一一比较,注意各位和十位还有十位和百位的输出位数问题
【样例1】
5
1235
【样例2】
10
1234568910
【样例3】
15
1234567891012131415
【样例1】
4
【样例2】
7
【样例3】
11
代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string so(int i)
{
string s1 = "", s2;
while(i)
{
s1 += i % 10 + ‘0‘;//从低位向高位 所以转换的字符数字放好相反
i /= 10;
}
reverse(s1.begin(), s1.end());
s2 = s1;
return s2;
}
int main()
{
int n;
cin >> n;
string s1, s2 = "";
cin >> s1;
for(int i = 1; i <= 100; i ++ )
{
s2 += so(i);
}
int t = 1;
for(int i = 0; i <= s1.size(); i ++ )
{
//特判1
if(s1[0] != s2[0])//1
{
cout << "1" << endl;
return 0;
}
if(s1[i] != s2[i] || s1[i + 1] != s2[i + 1])//当前字符和下一位字符有一个不一样的
{
if(t == 1)//t表示位数 1代表个位
{
if(s2[i] == s1[i] && s2[i - 1] == ‘9‘)//特判10
cout << s2[i] << s2[i + 1] << endl;
else
cout << s2[i + 1] << endl;//个位
}
else if(t == 2)//2代表十位
{
cout << s2[i] << s2[i + 1] << endl;常规十位
}
else if(t == 3)cout << "100" << endl;//特判100
return 0;
}
if(i >= 9){i ++;t = 2;}
if(i >= 188){t = 3;}//188是100 的一在s2中的下标位置
}
return 0;
}
问题 K: Missing Number时间限制: 1 Sec 内存限制: 128 MB
原文:https://www.cnblogs.com/jw-zhao/p/14645863.html