#include<iostream> using namespace std; int main() { int a, b, sum; cin >> a >> b; sum = a + b > 0 ? a + b : -(a + b); if (a + b < 0) cout << "-"; string s = to_string(sum); if (s.length() > 3){ for (int i = s.length()-3; i > 0; i = i - 3) s.insert(i, ","); cout << s << endl; } else cout << sum << endl; return 0; }
string.insert (pos, str);
在 pos 位置插入 str#include<iostream> using namespace std; int main() { string words[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; string N; cin >> N; int sum = 0; for (int i = 0; i < N.length(); i++) sum += stoi(N.substr(i,1)); string s = to_string(sum); cout << words[stoi(s.substr(0,1))]; for (int i = 1; i < s.length(); i++) cout << " " << words[stoi(s.substr(i,1))]; return 0; }
stoi()
to_string()
s.find_first_of('.') != s.find_last_of('.')
)和小數位數是否超過2(s.length()-s.find('.') > 3
)#include<iostream> using namespace std; int main() { int n, num = 0; cin >> n; string s; double data, sum = 0; for (int i = 0; i < n; i++){ bool islegal = true; cin >> s; for (int j = 0; j < s.length(); j++){ if ((s[j]>'9' || s[j]<'0') && s[j]!='-' && s[j]!='.'){ cout << "ERROR: " << s << " is not a legal number\n"; islegal = false; break; } } if (islegal){ if (s.find('.') != string::npos){ if(s.find_first_of('.') != s.find_last_of('.') || s.length()-s.find('.') > 3){ cout << "ERROR: " << s << " is not a legal number\n"; continue; } } data = stod(s); if (data >= -1000 && data <= 1000){ sum += data; num++; } else cout << "ERROR: " << s << " is not a legal number\n"; } } if (!num) cout << "The average of 0 numbers is Undefined"; else if (num == 1) printf("The average of 1 number is %.2f", num, sum); else printf("The average of %d numbers is %.2f", num, sum/num); return 0; }
#include<iostream> #include<cstring> using namespace std; int main(){ int n, k = 0; scanf("%d", &n); char s[50], t[50]; double num, sum = 0.0; for (int i = 0; i < n; i++){ scanf("%s", s); sscanf(s, "%lf", &num); sprintf(t, "%.2f", num); bool islegal = true; for (int j = 0; j < strlen(s); j++) if (s[j] != t[j]) islegal = false; if (islegal && num >= -1000 && num <= 1000){ k++; sum += num; } else printf("ERROR: %s is not a legal number\n", s); } if (!k) printf("The average of 0 numbers is Undefined\n"); else if (k == 1) printf("The average of 1 number is %.2f\n", sum); else printf("The average of %d numbers is %.2f\n", k, sum / k); return 0; }
#include<iostream> using namespace std; int main() { int n, z; scanf("%d", &n); for (int i = 0; i < n; i++){ scanf("%d", &z); string s = to_string(z); int len = s.length(); int a = stoi(s.substr(0,len/2)); int b = stoi(s.substr(len/2)); printf("%s", !b || z % (a * b) ? "No\n" : "Yes\n"); } return 0; }
string.substr(pos)
可表示pos到end的子串ios
string substr (size_t pos = 0, size_t len = npos) const;
算法
#include<iostream> using namespace std; int main() { int D, N; cin >> D >> N; string s = to_string(D); for (int i = 1; i < N; i++){ string next; int sum = 1; for (int j = 0; j < s.length(); j++){ if (j == s.length()-1){ next.append(s.substr(j,1)); next.append(to_string(sum)); break; } if (s[j] == s[j+1]) sum++; else{ next.append(s.substr(j,1)); next.append(to_string(sum)); sum = 1; } } s = next; } cout << s << endl; return 0; }
#include<iostream> using namespace std; int main() { string s; int n, j; cin >> s >> n; for (int k = 1; k < n; k++){ string t; for (int i = 0; i < s.length(); i = j){ for (j = i; j < s.length() && s[j] == s[i]; j++); t += s[i] + to_string(j - i); } s = t; } cout << s; return 0; }