REQg


  • 首页

  • 标签

  • 分类

  • 归档

  • 搜索

《算法竞赛入门经典》 第3章 数组和字符串

发表于 2019-07-19 | 分类于 《算法竞赛入门经典》
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 3.1 逆序输出
#define maxn 105
int a[maxn];

int x, n = 0;
while (scanf("%d", &x) == 1) {
a[n++] = x;
}
for (int i = n - 1; i >= 1; i--) {
printf("%d ", a[i]);
}
printf("%d\n", a[0]);


//测试的时候可以改为这样,方便中断程序
while (scanf("%d", &x) == 1 && x != -1)

1 2 3 4 5 6 -1
6 5 4 3 2 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 3.2 开灯问题
#define maxn 1010
int a[maxn];

int n, k, first = 1;
memset(a, 0, sizeof(a));
scanf("%d%d", &n, &k);
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= n; j++) {
if (j % i == 0) a[j] = !a[j];
}
}
for (int i = 1; i <= n; i++) {
if (a[i]) {
if (first) first = 0;
else printf(" ");
printf("%d", i);
}
}
printf("\n");

7 3
1 5 6 7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 蛇形填数
int a[20][20];
int n, x, y, tot = 0;
scanf("%d", &n);
memset(a, 0, sizeof(a));
tot = a[x = 0][y = n - 1] = 1;
while (tot < n * n) {
while (x + 1< n && !a[x + 1][y]) a[++x][y] = ++tot;
while (y - 1 >= 0 && !a[x][y - 1]) a[x][--y] = ++tot;
while (x - 1 >= 0 && !a[x - 1][y]) a[--x][y] = ++tot;
while (y + 1 < n && !a[x][y + 1]) a[x][++y] = ++tot;
}
for (x = 0; x < n; x++) {
for (y = 0; y < n; y++)
printf("%3d", a[x][y]);
printf("\n");
}

4
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 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
// 3.4 竖式问题
int cnt = 0;
char s[20], buf[99];
scanf("%s", s);
for (int abc = 111; abc <= 999; abc++) {
for (int de = 11; de <= 99; de++) {
int x = abc * (de % 10), y = abc * (de / 10), z = abc * de;
sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);
int ok = 1;
for (int i = 0; i < strlen(buf); i++) {
if (strchr(s, buf[i]) == NULL) ok = 0;
}
if (ok) {
printf("<%d>\n", ++cnt);
printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n", abc, de, x, y, z);
}
}
}
printf("The number of solution = %d\n", cnt);


2357
<1>
775
X 33
-----
2325
2325
-----
25575

The number of solution = 1

竞赛题目选讲

3-1 UVa272 Tex Quotes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() {
int c, q = 1;
while ((c = getchar()) != EOF) {
if (c == '"') {
printf("%s", q ? "``" : "''");
q = !q;
} else {
printf("%c", c);
}
}

return 0;
}

"To be or not to be," quoth the Bard, "that is the question".
``To be or not to be,'' quoth the Bard, ``that is the question''.

3-2 UVa10082 WERTYU

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}
}

O S, GOMR YPFSU/
I AM FINE TODAY.

for (i = 1; s[i] && s[i] != c; i++);

c是键盘上打不出的字符(c不在s字符数组里),直接输出c。

else putchar(c);

如果c = s[i],找出c在键盘中的位置,s的下标,然后输出前一个字符.

if (s[i]) putchar(s[i - 1]);

3-3 UVa401 Palindromes

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
const char* res = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";
const char* msg[] = {"not a palindrome","a regular palindrome","a mirrored string","a mirrored palindrome"};
char r(char ch) {
if (isalpha(ch)) return res[ch - 'A'];
return res[ch - '0' + 25];
}

int main() {
char s[300];
while (scanf("%s", s) == 1) {
int len = strlen(s);
int p = 1, m = 1;
for (int i = 0; i < (len + 1 / 2); i++) {
if (s[i] != s[len - i - 1]) p = 0; // 不是回文串
if (r(s[i]) != s[len - i - 1]) m = 0; // 不是镜像串
}
printf("%s -- is %s.\n\n", s, msg[m * 2 + p]);
}
}

NOTAPALINDROME
NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI
ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS
2A3MEAS -- is a mirrored string.

ATOYOTA
ATOYOTA -- is a mirrored palindrome.

3-4 UVa340 Master-Mind Hints

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
#define maxn 1010

int main() {
int n, a[maxn], b[maxn];
int kase = 0;
while (scanf("%d", &n) == 1 && n) {
printf("Game %d:\n", ++kase);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (; ;) {
int A = 0, B = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
if (a[i] == b[i]) A++;
}
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++;
}
if (c1 < c2) B += c1;
else B += c2;
}
printf(" (%d,%d)\n", A, B - A);
}
}
return 0;
}

3-5 UVa1583 Digit Generator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define maxn 100005
int ans[maxn];

int main() {
int T, n;
memset(ans, 0, sizeof(ans));
for (int m = 1; m < maxn; m++) {
int x = m, y = m;
while (x > 0) {
y = y + x % 10;
x = x / 10;
}
if (ans[y] == 0 || m < ans[y]) ans[y] = m;
}
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
printf("%d\n", ans[n]);
}
}

3-6 UVa1584 Circular Sequence

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
#define maxn 105

int Less(const 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() {
int T;
char s[maxn];
scanf("%d", &T);
while (T--) {
scanf("%s", s);
int ans = 0;
int n = strlen(s);
for (int i = 1; i < n; i++) {
if (Less(s, i, ans)) ans = i;
}
for (int j = 0; j < n; j++) {
putchar(s[(j + ans) % n]);
}
putchar('\n');
}
}

《算法竞赛入门经典》 第2章 循环结构程序设计

发表于 2019-02-18 | 分类于 《算法竞赛入门经典》

主要讲了for和while两种循环,以及一些需要注意的小细节。

阅读全文 »

《算法竞赛入门经典》 第1章 程序设计入门

发表于 2019-02-17 | 分类于 《算法竞赛入门经典》

第一张讲了些很基本的C/C++常识。

书中一些小的例子。

阅读全文 »

The Trump Show, season two

发表于 2019-02-16 | 分类于 《经济学人》每周精读

JANUARY 5TH-11TH 2019



What to expect from the second half of Donald Trump’s first term
Thus far the president has been lucky. It may not last

DONALD TRUMP’S nerve-jangling presidential term began its second half with a federal-government shut down, seesawing markets and the ejection of reassuring cabinet members like Generals John Kelly and James Mattis. As Mr Trump’s opponents called this a disaster, his supporters lambasted their criticism as hysterical—wasn’t everybody saying a year ago that it was sinister to have so many generals in the cabinet?

阅读全文 »

Codeforces Beta Round #54 (Div. 2) A. Chat room

发表于 2019-01-30 | 分类于 Codeforces题解

http://codeforces.com/problemset/problem/58/A

题意:
如果一个字符串可从左往右一次提取出hello,则输出YES,否则NO。
满足要求的字符串是由hello在任意位置插入任意个字符。

代码:

阅读全文 »

Codeforces Beta Round #1 A. Theatre Square

发表于 2019-01-30 | 分类于 Codeforces题解

http://codeforces.com/problemset/problem/1/A

题意:
有个mn的巨型,用aa的矩形去填,不允许裁剪。
所以,能除尽就刚好那边放那么多块,不能得加一块。

输入的范围可能会超出int的范围,所以要用long long。

代码:

阅读全文 »

Codeforces Beta Round #44 (Div. 2) A. Triangular numbers

发表于 2019-01-30 | 分类于 Codeforces题解

http://codeforces.com/problemset/problem/47/A

题意:
输入n,如果存在整数满足i (i + 1) / 2 == n。输出YES,否则NO.
实则是用n个点是否能组成一个正三角形。顶点是1,边是n,每层递增1,点的总和为
(i + 1) / 2,就是等差数列求和。

代码:

阅读全文 »

Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table

发表于 2019-01-30 | 分类于 Codeforces题解

http://codeforces.com/problemset/problem/509/A

题意:
输入n*n,除了第一行为1,后面都满足
res[i][j] = res[i - 1][j] + res[i][j - 1];
求二维数组中的最大值。

代码:

阅读全文 »

Codeforces Round #131 (Div. 2) A. System of Equations

发表于 2019-01-30 | 分类于 Codeforces题解

http://codeforces.com/problemset/problem/214/A

题意:输入n,m,满足a a + b == n && a + b b == m,问有几种解。

n,m的范围为1到1000。

代码:

阅读全文 »

Codeforces Round #379 (Div. 2) A. Anton and Danik

发表于 2019-01-30 | 分类于 Codeforces题解

http://codeforces.com/problemset/problem/734/A

题意:2人进行比赛,告诉你每一场谁获胜。统计谁赢了

一般会用2个来计数,A赢几场和D赢几场,但其实没必要。
A-D等于A相对D赢的场次。A赢++否则–。

代码:

阅读全文 »
1234…8

REQg

78 日志
4 分类
22 标签
© 2020 REQg
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4
浙ICP备 - 19004121号