Description
Calculate a + b
Input
The input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.
Sample Input
1 2
3 4
Sample Output
3
7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include"stdafx.h" #include<vector> usingnamespacestd; intmain() { int a, b; vector<int> slove; int tmp;
while (cin >> a&&cin >> b) { tmp= (a + b); slove.push_back(tmp); } for(auto i:slove) cout << (int)i << endl; system("pause"); return0; }
intfun(char *str1, char *str2) { size_t length = strlen(str1); size_t length2 = strlen(str2); length = length < length2 ? length:length2; int i; for (i = 0; i < length; i++) { char c1 = str1[i], c2 = str2[i];
if (c1 > 'a'&&c1 < 'z') c1 = c1 - ('a' - 'A'); if (c2 >= 'a'&&c2 <= 'z') c2 = c2 - ('a' - 'A'); if (c1!=c2) break; } if (i == length) return-1; return i; }
检索出字符串中出现最多的字符,并返回该字符
ASCII共定义了256个代码(从0-255),从0-32位为控制字符(33个)(ASCII control characters),从33-127位为可打印字符(95个)(ASCII printable characters)。从0-127是标准的ASCII编码,从128-255是扩展的ASCII编码。
charfun(char *str) { char o = 'a' - 'A'; char c = '\0'; //存储ASCII256个字符的的出现次数 int data[256]; memset(data, 0, 256 * sizeof(int)); size_t length = strlen(str); //遍历字符串 for (int i = 0; i<length; i++) { c = str[i]; data[c]++; if (c >= 'A' && c <= 'Z' && data[c + o] > 0) { data[c + o]++; } if (c >= 'a' && c <= 'z' && data[c - o] > 0) { data[c - o]++; } }
//找出最大的 char result = '\0'; int maxNumber = 0; for (int i = 0; i<256; i++) { int number = data[i]; if (number > maxNumber) { result = i; maxNumber = number; } } return result; }
getline获取一行输入,以回车符触发getline结束,且不保存最后的回车符
空行结束输入,(注意与文件尾EOF结束的区别)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include"stdafx.h" #include<iostream> #include<vector> usingnamespacestd; intmain() { string str; vector <string> vec; while (getline(cin,str)&&!str.empty()) { vec.push_back(str); } for (auto i : vec) { cout << i<< endl; } system("pause"); return0; }