问题 K: Missing Number时间限制: 1 Sec 内存限制: 128 MB
题目描述
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,再和输入的字符一一比较,注意各位和十位还有十位和百位的输出位数问题
样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))
【样例1】
5
1235
【样例2】
10
1234568910
【样例3】
15
1234567891012131415
样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))
【样例1】
4
【样例2】
7
【样例3】
11
代码
第一种做法:
构造一个正确的字符串去比较
ac代码:
#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;
    
}
第二种做法:
构造一个数组, 把输入的字符串转换成数组的形式
ac代码:
#include<iostream>
#include<algorithm>
using namespace std;
int a[100]; 
int b[100];
int main()
 {
    for(int i = 1; i <= 100; i ++)b[i] = i;
    int n;cin >> n;
    string s2;cin >> s2;
    int cnt = 1;
    if(s2.size() < 9)//字符串小于0~9, 一次向后一位
        for(int i = 0; i < s2.size(); i ++)
            a[cnt ++] = s2[i] - '0';
    else//字符串1~99, 10~99的时候一次向后两位 
    {
        for(int i = 0; i < 9; i ++)
            a[cnt ++] = s2[i] - '0';
        for(int i = 9; i < s2.size(); i += 2)
            a[cnt ++] = (s2[i] - '0') * 10 + (s2[i + 1] - '0');
    }
    int idx = 0;
    for(int i = 1; i <= cnt; i ++)
        if(a[i] != b[i]){idx = i;break;}
    if(!idx)cout << 100 << endl;//特判100
    else cout << idx << endl;
    return 0;
}

                
            
        
浙公网安备 33010602011771号