[問題] ZJ d098

看板C_and_CPP作者 (小賴)時間14年前 (2010/07/13 09:22), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串1/1
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 遇到的問題: (題意請描述清楚) ZJ d098 http://zerojudge.tw/ShowProblem?problemid=d098 希望得到的正確結果: 如題目 程式跑出來的錯誤結果: 只要有一個字串中含有非數字的字元 後面剩下的就加不到了 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) Dev-C++ 有問題的code: (請善用置底文標色功能) #include<iostream> #include<sstream> using namespace std; int main() { string s; int i; while(getline(cin,s)) { stringstream ss(s),str; string t; int sum=0,z; bool x=1; while(ss>>t) { for(i=0;i<t.length();i++) if(t[i]<48||t[i]>57) { x=0; break; } if(x) { str.clear(); str<<t; str>>z; sum+=z; } } cout<<sum<<endl; } //system("pause"); return 0; } 補充說明: -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.23.148.201

07/13 11:38, , 1F
2輪的istringstream,第2輪搭配.eof(),不需逐字元檢查
07/13 11:38, 1F

07/13 13:12, , 2F
查一下標頭檔 iomanip, 裡 hex 操作子的用法
07/13 13:12, 2F

07/13 13:42, , 3F
這個網站好讚 ~ 原來還有這樣的解題系統
07/13 13:42, 3F
文章代碼(AID): #1CEx_lA5 (C_and_CPP)