竹園論壇

標題: 53 - simple problem 4 [打印本頁]

作者: jd3    時間: 2014-6-6 00:08
標題: 53 - simple problem 4
本帖最後由 domen111 於 2014-6-6 07:58 編輯

http://toj.twbbs.org/oj/pro/53/

simple problem 系列差不多到這裡會卡卡的
因為我從 simple problem 4 開始WA了所以發一下...


這題很神奇的是自己寫input很容易WA (我WA了一整排~)(不曉得學長藏了什麼測資?)
簡單的方法是用 stringstream , 時間很寬鬆不需要考慮效率
更神奇的是你可能在AC後才發現沒有  mod 1000000007 !!!!!!!


關於stringstream可以看這篇:
    http://www.dotblogs.com.tw/v6610 ... vert_and_clear.aspx
stringstream 的注意事項:記得每次要清空後再使用




CODE:
#include<iostream>
  • #include<cstdio>
  • #include<cstring>
  • #include<cstdlib>
  • #include<sstream>

  • using namespace std;

  • stringstream ss;
  • string str;

  • int main()
  • {
  •    
  •     while(getline(cin, str))
  •     {
  •         int ans = 0;
  •         int add;
  •         ss << str;
  •         while(ss >> add)
  •             ans += add;
  •         
  •         ss.str("");
  •         ss.clear();
  •         
  •         printf("%d\n",ans);
  •     }
  •    
  •     return 0;
  • }




  • 作者: domen111    時間: 2014-6-6 08:00
    使用istringstream不用clear的方法:
    #include<iostream>
  • #include<cstdio>
  • #include<cstring>
  • #include<cstdlib>
  • #include<sstream>
  • using namespace std;

  • string str;
  • int main()
  • {
  •         while(getline(cin, str))
  •         {
  •                 int ans = 0;
  •                 int add;
  •                 istringstream ss(str);
  •                 while(ss >> add)
  •                         ans += add;
  •                 printf("%d\n",ans);
  •         }
  • }








  • 歡迎光臨 竹園論壇 (http://forum.tfcis.org/) Powered by Discuz! X3.2