PAT甲级1100 Mars Numbers:[C++题解]进制位、使用stringstream类读入
生活随笔
收集整理的這篇文章主要介紹了
PAT甲级1100 Mars Numbers:[C++题解]进制位、使用stringstream类读入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 題目分析
題目分析
分析:
使用char型二維數組 names[][5] 存儲這些 火星文。
ac代碼
#include<iostream> #include<sstream> using namespace std;char names[][5] ={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };//返回火星文對應的數:即names數組中的下標 int get(string word){for(int i=0;i<25;i++){if(names[i] == word){if(i<13) return i;return 13*(i-12);}}return -1; // 一定不會執行 }int main(){int n;cin>>n;getchar(); //如果使用 getline ,先把第一行的回車干掉while(n--){string line;getline(cin ,line); //讀一行stringstream ssin(line);//是數字if(line[0]<='9'){int v;ssin >>v; //從緩沖區讀數據if(v<13) cout<<names[v]<<endl;else{cout<<names[12+v/13];if( v %13 == 0) cout<<endl;else cout<<" "<<names[v%13]<<endl;}}else{ //讀入的是字母int res=0;string word;while(ssin>>word){ //從getline讀入的一行中依次讀入東西,有多少讀多少res+=get(word); }cout<<res<<endl;}}}題目鏈接
PAT甲級1100 Mars Numbers
總結
以上是生活随笔為你收集整理的PAT甲级1100 Mars Numbers:[C++题解]进制位、使用stringstream类读入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT甲级1027 Colors in
- 下一篇: PAT甲级1019 General Pa