【题解】 —算法竞赛入门经典第二版 【第三章例题】【6/6】

例题 3-1

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
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;

int main()
{
int flag=1;
int c;
while((c = getchar()) != EOF)
{
if(c == '"')
{
if(flag)
cout << "``";
else
cout << "''";
flag = !flag;
}
else
printf("%c", c);
}
return 0;
}

例题 3-2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
int main()
{
int i, c;
while((c = getchar()) != EOF)
{
for(i = 1; s[i] && s[i] != c; ++i);
if(s[i])
putchar(s[i-1]);
else
putchar(c);
}
return 0;
}

例题 3-3

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
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
const char* res[] = {"not a palindrome", "a regular palindrome", "a mirrored string", "a mirrored palindrome"};

char r(char ch)
{
if(ch >= 'A' && ch <= 'Z')
return rev[ch - 'A'];
else
return rev[ch - '0' + 25];
}
int main()
{
char s[30];
while(~scanf("%s", s))
{
int len = strlen(s);
int p = 1, m = 1;
for(int i = 0; i < (len + 1) / 2; ++i)
{
if(s[i] != s[len - 1 - i]) p = 0;
if(r(s[i]) != s[len-1-i]) m = 0;
}
printf("%s -- is %s.\n\n", s, res[m * 2 + p]);
}
return 0;
}

例题 3-4

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
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 1010;

int n, cnt1, cnt2;
int a[MAX], b[MAX];
int main()
{
for(int cas = 1; scanf("%d",&n) && n; ++cas)
{
printf("Game %d:\n", cas);
for(int i = 0; i < n; ++i)
scanf("%d",&a[i]);
while(true)
{
cnt1 = cnt2 = 0;
for(int i = 0; i < n; ++i)
{
scanf("%d",&b[i]);
if(a[i] == b[i])
++cnt1;
}
if(b[0] == 0) break;
for(int d = 1; d <= 9; ++d)
{
int c1 = 0, c2 = 0;
for(int i = 0; i < n; ++i)
{
if(a[i] == d) ++c1;
if(b[i] == d) ++c2;
}
cnt2 += min(c1, c2);
}
printf(" (%d,%d)\n", cnt1, cnt2 - cnt1);
}
}
return 0;
}

例题 3-5

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
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 100010;

int t, n;
int res[MAX];
void init()
{
for(int i = 1; i < MAX; ++i)
{
int x = i, y = i;
while(x)
{
y += x % 10;
x /= 10;
}
if(res[y] == 0 || i < res[y]) res[y] = i;
}
}
int main()
{
init();
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n", res[n]);
}
return 0;
}

例题 3-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
31
32
33
34
35
36
37
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int les(char* s, int p, int q)
{
int n = strlen(s);
for(int i = 0; i < n; ++i)
{
if(s[(p+i)%n] != s[(q+i)%n])
return s[(p+i)%n] < s[(q+i)%n];
}
return 0;
}
int main()
{
char s[105];
int n, ans, len;
while(~scanf("%d",&n))
{
while(n--)
{
ans = 0;
scanf("%s",s);
len = strlen(s);
for(int i = 0; i < len; ++i)
if(les(s, i, ans))
ans = i;
for(int i = 0; i < len; ++i)
cout << s[(ans+i)%len];
cout << endl;
}
}
return 0;
}
Donate comment here
0%