2014年6月10日 星期二

sstream

  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     string line;  
  9.     stringstream ss;  
  10.     int sum[3],a;  
  11.     for (int i = 0; i < 3; i++)  
  12.     {  
  13.         getline(cin, line);  
  14.         ss.clear();  
  15.         ss.str(line);  
  16.         sum[i] = 0;  
  17.         while (1)  
  18.         {  
  19.             ss >> a;  
  20.             if (ss.fail()) break;  
  21.             sum[i] += a;  
  22.         }  
  23.         //cout << sum[i] << endl;  
  24.     }     
  25.     int iMax = 0;  
  26.     for (int i = 1; i < 3;i++)  
  27.     if (sum[i]> sum[iMax])  
  28.         iMax = i;  
  29.     cout << iMax + 1 << ' '<< sum[iMax] << endl;  
  30.     return 0;  
  31. }  

2014年6月9日 星期一

cin.getline

  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     char line1[100];  
  8.     char line2[100];  
  9.     char line3[100];  
  10.     while (cin.getline(line1,100) )  
  11.     {  
  12.         cin.getline(line2, 100);  
  13.         cin.getline(line3, 100);  
  14.   
  15.         cout << line3 << endl;  
  16.         cout << line1 << endl;  
  17.         cout << line2<< endl;  
  18.     }     
  19.             return 0;  
  20. }  

string to int

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    string s1,s2,s3,s1a="",s2a="",s3a="";
    cin >> s1>>s2>>s3;
    for (int i=0;i<s1.length();i++)
        if (s1[i]>='0' && s1[i]<='9')
           s1a = s1a + s1[i];
     for (int i=0;i<s2.length();i++)
        if (s2[i]>='0' && s2[i]<='9')
           s2a = s2a + s2[i];
     for (int i=0;i<s3.length();i++)
        if (s3[i]>='0' && s3[i]<='9')
           s3a = s3a + s3[i];
    int n1,n2,n3;
    istringstream(s1a) >>n1;
    istringstream(s2a) >> n2;
    istringstream(s3a)  >>n3;
    cout << n1+n2+n3<< endl;

    return 0;
}

字元比對

  1. #include <iostream>   
  2. #include <string>   
  3. using namespace std;   
  4.   
  5. int main()   
  6. {   
  7.     string rstr, mstr;   
  8.     cin >> rstr >>mstr;   
  9.     int i=1,j=1;   
  10.     bool isOk = true;   
  11.     while (i<=rstr.length() && isOk == true)   
  12.     {   
  13.         while  (rstr[i]!= mstr[j] && j<=mstr.length())   
  14.         {   
  15.             j++;   
  16.         }   
  17.         if (rstr[i]==mstr[j])   
  18.         {   
  19.             i++;   
  20.             j++;   
  21.         }   
  22.         else  
  23.             isOk = false;   
  24.     }   
  25.     if (isOk == true)   
  26.       cout << "YES" << endl;   
  27.     else  
  28.         cout << "NO" << endl;   
  29.     return 0;   
  30. }  

2014年6月5日 星期四

  1. //String Sample
  2. #include <iostream>   
  3. #include <string>    
  4. using namespace std;   
  5. int main()   
  6. {   
  7.     string s;   
  8.     while (cin >> s)   
  9.     {   
  10.         for (int i =s.length()-1 ; i >= 0;i--)   
  11.         cout <<s[i];   
  12.         cout << endl;   
  13.     }   
  14.     return 0;   
  15. }