1136 25 A Delayed Palindrome 1024 25 Palindromic Number 1001 20 A+B Format 1005 20 Spell It Right 1035 20 Password 1061 20 Dating 1073 20 Scientific Notation 1077 20 Kuchiguse 1082 25 Read Number in Chinese 1093 25 Count PAT’s 1108 20 Finding Average 1140 20 Look-and-say Sequence 1152 20 Google Recruitment 1019 20 General Palindromic Number
string的题目以20分为主。
类型转换
string
char
1 2
string a; char n[10]=a.c_str();
int
1 2
string a; int n = atoi(a.c_str());
1 2 3 4 5 6
string s; cin >> s try{//防止不是数字 float n = stof(s); cout << n + 1; }catch (...) { }
double
1 2
string a; double n=atof(a.c_str());
to string
int
1 2
int a; string s = to_string(a);
输入
getline(cin,s)
🎈1022 Digital Library
若输入的字符串中含有空格可利用getline读入一整行(需导入string库)
不需要getchar()消除回车
1 2 3 4
#include<string> string s; getline(cin, s);//abc d cout << s;//abc d
Palindromic Number :回文数 pivot :支点;枢轴;中心点;最重要的人(或事物);中心;核心 consecutive :连续的 capital English letter:大写英文字母 case sensitive :区分大小写 Scientific notation:科学计数法 notorious reflection exaggerated stereotype corresponding
#include<iostream> #include<string> usingnamespace std; string s, a; string add(string a, string b){ string ans; int pos = 0; int num; for (int i = a.size() - 1; i >= 0; i--) { num = (a[i] - '0') + (b[i] - '0'); if (pos) { num++; pos = 0; } int n = num % 10; char c = n + '0'; ans = c + ans; if (num >= 10) { pos = 1; } } if (pos) ans = '1' + ans; return ans; } booljudge(string s){ for (int i = 0; i < s.size() / 2; i++) { if (s[i] != s[s.size() - i - 1]) returnfalse; } returntrue; } intmain(){ cin >> s; int step = 0; while (step < 10) { if (judge(s)) { cout << s << " is a palindromic number.\n"; return0; } step++; a.clear(); for (int i = s.size() - 1; i >= 0; i--) { a += s[i]; } cout << s << " + " << a << " = "; //加法 s = add(s, a); cout << s << endl; } cout << "Not found in 10 iterations.\n"; }
#include<iostream> #include<string> #include<stack> usingnamespace std; intmain(){ int a, b; cin >> a >> b; a = a + b; if (a < 0) cout << "-"; a = abs(a); if (a == 0) { cout << 0; return0; } stack<int>s; while (a > 0) {//对于数字987,654,分别将654、987入栈 s.push(a % 1000); a = a / 1000; } printf("%d", s.top());//第一组数可能小于3位 s.pop(); if (s.size() >= 1)printf(",");
while (s.size() > 1) { printf("%03d%s", s.top(), ","); s.pop(); } if (s.size() == 1) printf("%03d", s.top()); }
String写法
对于’,’的输出位置要考虑清楚
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream> #include<string> usingnamespace std; intmain(){ int a, b; cin >> a >> b; string s = to_string(a + b); if (s[0] == '-') { cout << "-"; s.erase(s.begin()); } for (int i = 0; i < s.size(); i++) { cout << s[i]; if ((s.size() - i - 1) % 3 == 0 && (s.size()>3&&i < s.size() - 3)) cout << ','; } }
#include<iostream> #include<string> usingnamespace std; int N; intmain(){ cin >> N; int legalnum = 0;//合法数量 double ans = 0;//和 string s; for (int i = 0; i < N; i++) { cin >> s; bool flag = false; if (s[0] == '-') { s = s.substr(1, s.size() - 1); flag = true;//负数 } double m = 10;//整数每次*10 double n = 1;//小数点后每次*0.1 bool legal = true; double number = 0;//本数字 for (int j = 0; j < s.size(); j++) { if (s[j] - '0' >= 0 && s[j] - '0' <= 9) { number = number * m + (s[j] - '0') * n; if (n != 1) n = n * 0.1;//小数每次*0.1 } elseif (s[j] == '.' && m == 10) {//第一次遇到小数点 m = 1; n = 0.1; } else { legal = false; break; } if (int(n * 10000) == 1) {//三位小数n!=0.0001 legal = false; break; } } if (!legal || number > 1000) { cout << "ERROR: "; if (flag)cout << "-"; cout << s << " is not a legal number\n"; } else { legalnum++; if (flag) ans -= number; else ans += number; } } if (legalnum == 0) cout << "The average of 0 numbers is Undefined\n"; elseif (legalnum == 1){ cout << "The average of 1 number is " ; printf("%.2f", ans ); } else { cout << "The average of " << legalnum << " numbers is "; printf("%.2f", 1.0 * ans / legalnum); } }
#include<iostream> #include<string> #include<math.h> #include<vector> usingnamespace std; int N, len; string s; boolcheck(int n){ for (int i = 2; i * i <= n; i++) { if (n % i == 0) { returnfalse; } } returntrue; } intmain(){ cin >> N >> len; cin >> s; for (int i = 0; i <= N - len; i++) { int n = stoi(s.substr(i, len)); if (check(n)) { cout << s.substr(i, len); return0; } } cout << 404; }
#include<iostream> #include<vector> using namespace std; //vector<int>B; vector<int>A; int N, b; int main() { cin >> N >> b; if (N == 0) { cout << "Yes\n0"; return 0; } //B.push_back(1); while (N != 0) { int x = N % b; A.push_back(x); N = N / b; } bool flag = true; for (int i = 0; i <=A.size()/2; i++) { if (A[i] != A[A.size() -1 - i]) { cout << "No\n"; flag = false; break; } } if(flag) cout<<"Yes\n"; for (int i = A.size()-1 ; i >= 0; i--) { cout << A[i]; if (i !=0) cout << " "; } }