1016 Part A + B (15 minutes)

topic:


思路:

A and B are in the range of 10 10 to be greater than the int, string type employed, using c ++ to the string can not consider the length of the string internally self-adjusting. String type is stored, for the subsequent conversion of its type, the data obtained from a string type to integer in sstream in c ++ function in the stringstream types have this function, be applied to this. pa and pb to set a long int or int using data overflow.

Code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <string>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     string s1, s2;
10     string a = "";
11     string b = "";
12     char Da, Db;
13     cin >> s1 >> Da >> s2 >> Db;
14     for(int i = 0; i < s1.length(); i++)
15     {
16         if(s1.at(i) == Da)     //s1.at()类似指针 
17         {
18             a = a + s1.at(i); 
19         }
20     }
21     for(int i = 0; i < s2.length(); i++)
22     {
23         if(s2.at(i) == Db)
24         {
25             b = b + s2.at(i);
26         }
27     }
28     long int pa = 0, pb = 0;     
29     stringstream ss1, ss2;        //包含在sstream函数中,可以将字符串类型转换为整型 
30     ss1 << a;    //向ss1流中传入a 
31     ss1 >> pa;    //向pa中写入流入ss1后转换成int类型的值 
32     ss2 << b;    //向ss2流中传入b 
33     ss2 >> pb;    //向pb中写入流入ss2后转换成int类型的值
34     cout << pa + pb << endl;
35     
36     return 0;
37  } 

 总结:

要对c++函数库应用有所了解,sstream函数具有字符串类型转化为整型的功能。

Guess you like

Origin www.cnblogs.com/Anber82/p/11128185.html