美文网首页
字符串输入问题

字符串输入问题

作者: 土豆有点 | 来源:发表于2017-08-29 16:22 被阅读4次

PAT 甲级 1100

People on Mars count their numbers with base 13:

Zero on Earth is called "tret" on Mars.

The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.

For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.

For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (< 100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.

Output Specification:

For each number, print in a line the corresponding number in the other language.

Sample Input:

4

29

5

elo nov

tam

Sample Output:

hel mar

may

115

13

在pat甲级1100遇到这么一个问题,就是我要输入int类型n,之后我就要输入字符串。但问题来了,我要输入的有可能是一行字符串。

int main()
{
    init(); //打表 :在程序中一次性计算出所有需要用的结果,之后的查询直接取这些结果就行
    int T;
    cin>>T;
    while(T--){
        string str;
        getline(cin, str);//不能用cin,用“cin>>”从输入流提取数据,遇空格就终止
        if(str[0] >= '0' && str[0] <= '9'){//字符转换成数字
            int num = 0;
            for(int i = 0; i < str.length(); i++){
                num = num * 10 + (str[i] - '0');
            }
            cout << numToStr[num] << endl;
        }
        else{
            cout << strToNum[str] << endl;
        }
    }
    return 0;
}

但是这样直接运行会报字符串内存越界的错误,刚开始百思不得其解。后来经过调式发现,原来在我们开始输入T的时候就输了一个换行,而这个换行会被str读取,导致我们无法继续输入,并且程序也会报错。
然后解决这个问题呢,就是在cin>>T的后面加一句getchar(),让getcahr读取换行。这样就避免输入时换行对str的影响

相关文章

  • 最长不含有重复字符的子串

    问题: 输入一组字符串,字符串中含有重复字符,求最大不重复的子字符串长度。 描述: 输入的字符串为 abcabcd...

  • 字符串输入问题

    PAT 甲级 1100 People on Mars count their numbers with base ...

  • 字符串问题

    字符串问题 单词拆分 输入: s = "applepenapple", wordDict = ["apple", ...

  • JavaScript pratice

    1.翻转字符串 问题描述:输入一个字符串,倒序输出该字符串样例:输入:"hello"输出:"olleh"分析:可以...

  • 字符串的排列问题(Java)

    问题描述: 输入一个字符串,打印出该字符串的所有排列。例如,输入字符串”abc”,则输出有字符’a’,’b’,’c...

  • 全排列算法

    问题: 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,...

  • 344. 反转字符串

    344. 反转字符串 问题 编写一个函数,其作用是将输入的字符串反转过来。 示例 1: 输入: "hello"输出...

  • [蓝桥杯]字符串的输入输出处理

    问题 1094: 字符串的输入输出处理 题目描述 字符串的输入输出处理。 输入 第一行是一个正整数N,最大为100...

  • 经典面试题20 - 字符串的全排列

    问题 输入一个字符串,打印出该字符串中字符的所有排列。 例如输入字符串abc,则输出由字符a、b、c 所能排列出来...

  • 百度前端算法题

    题目描述 输入第一行输入一串只含01的字符串,第二行输入n个问号(?),第三行输出.可以看做字符串匹配问题.假设...

网友评论

      本文标题:字符串输入问题

      本文链接:https://www.haomeiwen.com/subject/blaodxtx.html