String
徐仓仓 Lv3

📅time:8.18 - 8.19

🍔题目:

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

    cin、scanf后要用getchar()接收换行符才能用getline()

    或者使用scanf("%d\n", &n); 将回车抵消

    1
    2
    3
    4
    5
    string s1,s2;//输入ab/nc
    cin >> s1;
    getline(cin,s2);
    cout << s1;//ab
    cout << s2;//无
  • cin+getchar()

    一行中每个string用空格隔开

    1
    2
    3
    4
    5
    6
    vector<string>P;
    while (cin >> s) {//abc d e/n ff
    P.push_back(s);
    char c = getchar();//读取空格或回车
    if (c == '\n') break;//若回车则结束输入
    }
  • 指定格式

          🎈1108 Finding Average
    
    1
    2
    3
    scanf("%s", a);
    sscanf(a, "%lf", &temp);
    sprintf(b, "%.2f",temp);

vrR0q1.png

输出

1
2
3
4
5
int main() {
string s;
cin >> s;
cout << s.size() - (s.size()+1);//4294967295,被默认为unsigned int
}

因为 unsigned int最大值:4294967295 ,int的最大值是 2147483647 (10^9)

判断

  • 数字
1
bool f = isdigit(s[i]);
  • 字母

    1
    bool f = (s[i] >= 'A' && s[i] <= 'N');

常见函数

  • 比大小

    1
    2
    3
    4
    //可以直接进行比较
    bool cmp(string a,string b){
    return a>b;
    }
  • 输入输出

    1
    2
    3
    4
    string s;
    cin>>s;
    cout<<S;
    printf("%s",s.c_str());//将string转换为字符数组
  • 迭代器

    1
    string::iterator it;
  • 插入insert()

    1
    2
    insert(pos,string);
    insert(it,it2,it3);//串[it2,it3)将被插在it的位置上 it、it2、it3是迭代器,如s.begin()+2
  • 删除erase()

    1
    2
    3
    s.erase(it);
    s.erase(first,last);//删除[first,last)区间(迭代器表示)
    s.erase(pos,length);//pos:起始下标,length:长度
  • 清空clear()

    1
    s.clear();
  • 截取substr()

    1
    s.substr(pos,len);//从下标pos开始,长度为len的子串,复杂度为O(len)
  • 查找find()

    1
    2
    3
    //复杂度为O(nm)
    s.find(s2);//返回说第一次出现的位置,否则,返回string::npos
    s.find(s2,pos);//从pos开始找
  • 替代replace()

    1
    2
    s.replace(pos,len,s2);//把s从pos开始,长度为len的子串替换为s2
    s.replace(it1,it2,str2);//迭代器[it1,it2)范围的子串替换为s2
  • 翻转reverse()

    1
    reverse(s.begin(), s.end());

Palindromic Number :回文数
pivot :支点;枢轴;中心点;最重要的人(或事物);中心;核心
consecutive :连续的
capital English letter:大写英文字母
case sensitive :区分大小写
Scientific notation:科学计数法
notorious
reflection
exaggerated
stereotype
corresponding

附录

1136 A Delayed Palindrome

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<string>
using namespace 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;
}
bool judge(string s) {
for (int i = 0; i < s.size() / 2; i++) {
if (s[i] != s[s.size() - i - 1])
return false;
}
return true;
}
int main() {
cin >> s;
int step = 0;
while (step < 10) {
if (judge(s)) {
cout << s << " is a palindromic number.\n";
return 0;
}
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";
}

1024 Palindromic Number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<string>
using namespace std;
string N, ans;
int K;
string add(string N) {
string num;
int pos = 0;
for (int i = 0; i < N.size(); i++) {
int n = (N[i] - '0') + (N[N.size() - 1 - i] - '0');
if (pos) {
n++;
pos = 0;
}
if (n > 9) pos = 1;
n = n % 10;
char temp = n + '0';
num = temp + num;
}
if (pos) num = '1' + num;
return num;
}
bool judge(string N) {
for (int i = 0; i < N.size(); i++) {
if (N[i] != N[N.size() - 1 - i]) {
return false;
}
}
return true;
}
int main() {
cin >> N >> K;
if (N.size() == 1) {
cout << N << endl << 0 << endl;
return 0;
}
int step = 0;
while (step < K) {
if (judge(N)) {
cout << N << endl << step << endl;
return 0;
}
step++;
N = add(N);
}
cout << N << endl;
cout << K << endl;
}

1001 A+B Format

👀主要不知道怎么int转string:string s = to_string(123);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
a = a + b;
if (a < 0) cout << "-";
a = abs(a);
if (a == 0) { cout << 0; return 0; }
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>
using namespace std;
int main() {
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 << ',';
}
}

1005 Spell It Right

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<string>
using namespace std;
int num[110];
string word[11] = { "zero","one","two","three","four","five","six","seven","eight","nine","ten" };
int main() {
string s;
cin >> s;
int big = 0;
for (int i = 0; i < s.size(); i++) {
int index = 0;
int add = 1;//进位符
int b = (s[i] - '0');//加数
while (add > 0) {//进位
add = num[index] + b;
b = 1;
num[index] = add % 10;
add = add / 10;
index++;
}
//99999999999999999999
if (index > big)big = index;
}
for (int i = big-1; i >= 0; i--) {
cout << word[num[i]];
if (i != 0) cout << " ";
}
}

1035 Password

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int N;
vector<string>acc;
vector<string>pas;
char word[4] = { '1', '0', 'l', 'O' };
char replace[4] = { '@','%','L','o' };
bool change(string& s) {
bool flag = false;
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < 4; j++) {
if (s[i] == word[j]) {
s[i] = replace[j];
flag = true;
break;
}
}
}
return flag;
}
int main() {
cin >> N;
string account, pass;
for (int i = 0; i < N; i++) {
cin >> account >> pass;
if (change(pass)) {
acc.push_back(account);
pas.push_back(pass);
}
}
if (acc.size() == 0) {
if (N == 1) {
printf("There is 1 account and no account is modified");
}
else
printf("%s%d%s", "There are ", N, " accounts and no account is modified");
return 0;
}
cout << acc.size() << endl;
for (int i = 0; i < acc.size(); i++) {
printf("%s %s\n", acc[i].c_str(), pas[i].c_str());
}
}

1061 Dating

又是输出格式的问题“%02d”

注意是要字母还是数字还是大小写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<string>
using namespace std;
string week[7] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
int main() {
string s1,s2,s3,s4;
cin >> s1 >> s2 >> s3 >> s4;
int i = 0;
for (; i < min(s1.size(), s2.size()); i++) {
if (s1[i] == s2[i]&&s1[i] >= 'A' && s1[i] <= 'G') {
cout << week[s1[i] - 'A'];
i++;
break;
}
}
int hour=0;
for (; i < min(s1.size(), s2.size()); i++) {
if (s1[i] == s2[i]) {
if (s1[i] >= '0' && s1[i] <= '9') {
hour= s1[i] - '0';
break;
}
if (s1[i] >= 'A' && s1[i] <= 'N') {
hour = s1[i] - 'A';
hour += 10;
break;
}
}
}
cout << " ";
printf("%02d\n", hour);
cout <<":";
for (int i = 0; i < min(s3.size(), s4.size()); i++) {
if (s3[i] == s4[i]&&((s3[i]>='A'&&s3[i]<='Z')||(s3[i]>='a'&&s3[i]<='z'))) {
printf("%02d\n", i);
break;
}
}
}

1073 Scientific Notation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
cin >> s;
int pos = s.find('E');
string c = s.substr(pos + 1, 1);//符号
int mi = atoi(s.substr(pos + 2, s.size() - pos - 2).c_str());
if (s[0] == '-') cout << s[0];
if (mi == 0) {
cout << s.substr(1, pos - 1);
return 0;
}
if (c == "+") {
if (s[1] != '0')
cout << s[1];
int len = 0;
int i = 3;
while (len < mi && i < pos) {
cout << s[i];
len++;
i++;
}
if (i < pos) {
cout << "." << s.substr(i, pos - i);
}
while (len < mi) {
cout << 0;
len++;
}
}
else if (c == "-") {//小数
cout << "0.";
int len = 1;
while (len < mi) {
cout << 0;
len++;
}
cout << s[1];
cout << s.substr(3, pos - 3);
}
}

1077 Kuchiguse

拿了17分,测试点3没过,🐙🗡帮我看一下

更新:居然是因为可以跨词

​ 如”aaa bb和ca bb”的答案是“a bb”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
int N;
cin >> N;
getchar();
string s;
vector<string> speech;
for (int i = 0; i < N; i++) {
getline(cin,s);
speech.push_back(s);
}
int index = 1;
bool flag = true;
while (speech[0].size() >= index) {
char c = speech[0][speech[0].size() - index];
for (int i = 0; i < N; i++) {
int j = speech[i].size() - index;
if (j <0) { flag = false; break; }
//cout << "i"<<i<<" " << j<<endl;
char temp = speech[i][speech[i].size() - index];
if (c != temp ) {
flag = false;
break;
}
}
if (!flag) break;
index++;
}
if (index == 1) cout << "nai" << endl;
else cout << speech[0].substr(speech[0].size() - index+1, index-1);

}

1082 Read Number in Chinese

没做出来

1093 Count PAT’s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<int>P, T;
int main() {
string s;
cin >> s;
int pnum = 0, tnum = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'P') pnum++;
else if (s[i] == 'T') tnum++;
else if (s[i] == 'A') {
P.push_back(pnum);
T.push_back(tnum);
}
}
int num = 0;
for (int i = 0; i < P.size(); i++) {
num = (num+P[i] * (tnum - T[i]) )% 1000000007;
}
cout << num % 1000000007 << endl;
}

1108 Finding Average

只有18分,测试2没过

更新:因为只有一个有效数字时,输出也要保留2位精度。。而不是直接输出就行了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<string>
using namespace std;
int N;
int main() {
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
}
else if (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";
else if (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);
}
}

利用sscanf和sprintf,AC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main() {
int n, cnt = 0;
char a[50], b[50];
double temp = 0.0, sum = 0.0;
cin >> n;
for(int i = 0; i < n; i++) {
scanf("%s", a);
sscanf(a, "%lf", &temp);
sprintf(b, "%.2f",temp);
int flag = 0;
for(int j = 0; j < strlen(a); j++)
if(a[j] != b[j]) flag = 1;
if(flag || temp < -1000 || temp > 1000) {
printf("ERROR: %s is not a legal number\n", a);
continue;
} else {
sum += temp;
cnt++;
}
}
if(cnt == 1)
printf("The average of 1 number is %.2f", sum);
else if(cnt > 1)
printf("The average of %d numbers is %.2f", cnt, sum / cnt);
else
printf("The average of 0 numbers is Undefined");
return 0;
}

1140 Look-and-say Sequence

很easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<vector>
using namespace std;
int D, N;
int main() {
cin >> D >> N;
vector<int>A, B, temp;
int i = 1;
A.push_back(D);
while (i<N) {
int cnt = 0, num = A[0];
for (int i = 0; i < A.size(); i++) {
if (num == A[i]) {
cnt++;
}
else {
B.push_back(num);
B.push_back(cnt);
num = A[i];
cnt = 1;
}
}
B.push_back(num);
B.push_back(cnt);
A = B;
/* for (int i = 0; i < A.size(); i++) {
cout << A[i];
}*/
//cout << endl;
B.clear();
i++;
}
for (int i = 0; i < A.size(); i++){
cout << A[i];
}
}

1152 Google Recruitment

判断素数,注意应该输出字符串而不是取出来的整数,因为0031输出时不应该忽略前导0

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<string>
#include<math.h>
#include<vector>
using namespace std;
int N, len;
string s;
bool check(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
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);
return 0;
}
}
cout << 404;
}

16分:打表会在测试点6运行错误,因为开不了那么大的数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<string>
#include<math.h>
#include<vector>
using namespace std;
int N, len;
string s;
vector<bool>prime;
int main() {
cin >> N >> len;
cin >> s;
int biggest = pow(10, len + 1);
prime.resize(biggest);
fill(prime.begin(), prime.end(), true);
for (int i = 2; i < biggest; i++) {
if (prime[i]) {//素数
for (int j = i + i; j < biggest; j += i) {
prime[j] = false;
}
}
}
for (int i = 0; i <= N - len; i++) {
int n = stoi(s.substr(i, len));
if (prime[n]) {
cout << s.substr(i, len);
return 0;
}
}
cout << 404;
}

1019 General Palindromic Number

参考十进制转二进制的方法,不断取模整除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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 << " ";
}
}
 Comments