[toc]

P1428 小鱼比可爱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
const int N = 105;
int a[N];
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
{
int count = 0;
for (int j = 0; j < i; j++)
{
if (a[i] > a[j])
count++;
}
cout << count << " ";
}
}

P1427 小鱼的数字游戏

看到的优秀题解,方便你快速过一遍知识:一个红题带你了解绿(黄)题知识点 - 洛谷专栏 (luogu.com.cn)

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 <stack>
using namespace std;
const int N = 105;
int a[N];
stack<int> st;
int main()
{
int tmp;
while (1)
{
cin >> tmp;
if (tmp == 0)
break;
st.push(tmp);
}
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
}

P5727 【深基5.例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
#include <iostream>
#include <stack>
using namespace std;
const int N = 10005;
int a[N];
stack<int> st;
int main()
{
int n;
cin >> n;
st.push(n);
while (n != 1)
{
if (n % 2 == 0)
{
n = n / 2;
}
else
{
n = n * 3 + 1;
}
st.push(n);
}
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
}

P1047 [NOIP2005 普及组] 校门外的树

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>

using namespace std;
const int N = 100005;
int a[N];

int main()
{
int n, m;
cin >> n >> m;
n += 1;
int l, r;
for (int i = 0; i < m; i++)
{
cin >> l >> r;
for (int j = l; j <= r; j++)
a[j] = 1;
}
int count=0;
for (int i = 0; i < n; i++)
{
if (a[i] == 0)
count++;

}
cout << count ;
}

P5728 【深基5.例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
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1005;
int a[N][3];

int main()
{
int n, count = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i][0] >> a[i][1] >> a[i][2];
}
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++) //注意+1,不然比较的是自己,会多n次
{
if (abs(a[i][0] - a[j][0]) <= 5 && abs(a[i][1] - a[j][1]) <= 5 &&
abs(a[i][2] - a[j][2]) <= 5 && abs(a[i][0] + a[i][1] + a[i][2] - a[j][0] - a[j][1] - a[j][2]) <= 10) //绝对值小于
count++;
}
}

cout << count;

}

P5729 【深基5.例7】工艺品制作

这题要注意x,y,z的范围。这里我采用边挖方块边计算的方法,可以省下一次三重循环遍历数组:

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 <algorithm>
using namespace std;
const int N = 25;
int a[N][N][N];

int main()
{
int w, x, h;
cin >> w >> x >> h;
int q;
cin >> q;

int x1, y1, z1;
int x2, y2, z2;
int count = 0;
for (int i = 0; i < q; i++)
{
cin >> x1 >> y1 >> z1;
cin >> x2 >> y2 >> z2;
for (int j = x1; j <= x2; j++)
{
for (int k = y1; k <= y2; k++)
{
for (int l = z1; l <= z2; l++)
{

if (a[j][k][l] == 0)
{
count++;
a[j][k][l] = -1;
}

}
}
}
}
/*
若两坐标点的位置大小不确定,还需要
for (int j = min(x1,x2); j <= max(x1,x2); j++)
for (int k = min(y1,y2); k <= max(y1,y2); k++)
for (int l = min(z1,z2); l <= (z1,z2); l++)

*/

cout << w*x*h-count;

}

P2550 [AHOI2001] 彩票摇奖

我们可以将能数组中中奖的号码的数字在数组的位置设置为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
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 35;
int a[N];
int b[10]; //中奖次数数组

int main()
{
int n;
cin >> n;
for (int i = 1; i <= 7; i++)
{
int tmp;
cin >> tmp;
a[tmp] = 1;
}
for (int i = 0; i < n; i++)
{
int count = 0;
int tmp;
for (int i = 1; i <= 7; i++)
{
cin >> tmp;
if (a[tmp] == 1)
count++;
}
b[count]++;
}
for (int i = 7; i >=1 ; i--)
{
cout << b[i] << " ";
}

}

P2615 [NOIP2015 提高组] 神奇的幻方

做的有点麻,经典行列分不清,然后还自作聪明把条件合并。下次要细心,把行列分清QwQ。这题的话,看题写判断就OK了。

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
60
61
62
63
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 45;
int a[N][N];
int main()
{
int n;
cin >> n;

int raw = 1, col = n/2 + 1; //表示k-1数字的位置,初始化为1的位置·
a[raw][col] = 1;
for (int i = 2; i <= n*n; i++)
{
if (raw == 1 && col != n )
{
raw = n ;
col = col + 1;
a[raw][col] = i;

}
else if (raw != 1 && col==n )
{
raw = raw - 1;
col = 1;
a[raw][col] = i;

}
else if(raw == 1 && col == n)
{
raw = raw + 1;
col = col ;
a[raw][col] = i;

}
else
{
if (a[raw - 1][col + 1] == 0) //右上
{
raw = raw - 1;
col = col + 1;
a[raw][col] = i;

}
else
{
raw = raw + 1;
col = col ;
a[raw][col] = i;
}
}

}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}

}

在题解中,有一种新的方案题解 P2615 【神奇的幻方】 - 洛谷专栏 (luogu.com.cn),可以总结一下从第一行中点开始,把每一个下面的数放到“右上角”,若右上角有数,则放到正下方。当i=2时,1的右上方为2(取余),当i=3时 ,2的右上方为3(取余),当i=4时,3的右上方已经被1占用,因此放入下方。

1
2
3
8 1 6
3 5 7
4 9 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
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 45;
int a[N][N];

int main()
{
int n;
cin >> n;
int raw = 1, col = n/2 + 1;

for (int i = 1; i <= n*n; i++)
{
a[raw][col] = i;
if (a[(raw - 2 + n) % n + 1][col % n + 1] == 0) //要避免数组下标取0
raw = (raw - 2 + n) % n + 1, col = col % n + 1;
else
raw = raw % n + 1;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}

}

P5730 【深基5.例10】显示屏

打表仙人(bushi),我们每四列输出一个字母,先把.所在下标赋值,最后为未初始化的下标赋X,对于最后一列不能有.的问题,我们直接不输出即可:

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
60
61
62
63
64
65
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1035;
char a[5][N];


int main()
{
int n;
cin >> n;
int col = 0; //表示该数字开始的列
for(int i = 0;i < n;i++)
{
char tmp;
cin >> tmp;

if (tmp == '0')
a[1][col + 1] = '.', a[2][col + 1] = '.', a[3][col + 1] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
if (tmp == '1')
a[0][col] = '.', a[1][col] = '.', a[2][col] = '.', a[3][col] = '.', a[4][col] = '.',
a[0][col + 1] = '.', a[1][col + 1] = '.', a[2][col + 1] = '.', a[3][col + 1] = '.', a[4][col + 1] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
if (tmp == '2')
a[1][col] = '.', a[1][col + 1] = '.', a[3][col + 1] = '.', a[3][col + 2] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
if (tmp == '3')
a[1][col] = '.', a[1][col + 1] = '.', a[3][col] = '.', a[3][col + 1] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
if (tmp == '4')
a[3][col] = '.', a[3][col + 1] = '.', a[4][col] = '.', a[4][col + 1] = '.', a[0][col+1] = '.', a[1][col + 1] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
if (tmp == '5')
a[1][col + 1] = '.', a[1][col + 2] = '.', a[3][col] = '.', a[3][col + 1] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
if (tmp == '6')
a[1][col + 1] = '.', a[1][col + 2] = '.', a[3][col + 1] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
if (tmp == '7')
a[1][col] = '.', a[2][col] = '.', a[3][col] = '.', a[4][col] = '.',
a[1][col + 1] = '.', a[2][col + 1] = '.', a[3][col + 1] = '.', a[4][col + 1] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
if (tmp == '8')
a[1][col + 1] = '.', a[3][col + 1] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
if (tmp == '9')
a[1][col + 1] = '.', a[3][col + 1] = '.', a[3][col] = '.',
a[0][col + 3] = '.', a[1][col + 3] = '.', a[2][col + 3] = '.', a[3][col + 3] = '.', a[4][col + 3] = '.';
col += 4;
}


for (int i = 0; i < 5; i++)
{
for (int j = 0; j < n * 4 - 1; j++)
{

if (a[i][j] == 0)
a[i][j] = 'X';
cout << a[i][j];
}
cout << endl;
}
}

也是看了一下题解题解 P5730 【【深基5.例10】显示屏】 - 洛谷专栏 (luogu.com.cn),感觉这种方法是最优解,能够拥有最快的打表速度。

1
2
3
4
5
6
7
8
9
10
string ans[10];
if(a[i]=='1')
{
ans[1]+="..X.";
ans[2]+="..X.";
ans[3]+="..X.";
ans[4]+="..X.";
ans[5]+="..X.";
}
//最后输出也是把最后的.截取掉

P5731 【深基5.习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
38
39
#include <iostream>
using namespace std;
const int N = 15;
int arr[N][N];

int main()
{
int n;
cin >> n;
int x = 1, y = 0, z = 1;
while (z <= n * n)
{
while (y < n && !arr[x][y+1]) {
y++;
arr[x][y] = z;
z++;
}
while (x < n && !arr[x+1][y]) {
x++;
arr[x][y] = z;
z++;
}
while (y >1 && !arr[x][y-1]) {
y--;
arr[x][y] = z;
z++;
}
while (x > 1 && !arr[x-1][y]) {
x--;
arr[x][y] = z;
z++;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++) printf("%3d", arr[i][j]);
printf("\n");
}
}

P2141 [NOIP2014 普及组] 珠心算测验

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>
using namespace std;
const int N = 20005;
bool a[N];
int b[105];

int main()
{
int n;
cin >> n;
int count = 0;
for (int i = 0; i < n; i++)
{
cin >> b[i];
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
a[b[i] + b[j]] = true;
}
}
for (int i = 0; i < n; i++)
{
if (a[b[i]] == true)
{
count++;
}
}
cout << count << endl;
}

P1161 开灯

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


int main()
{
int n;
cin >> n;
double a;
int b;
int ans = 0;
while (n--)
{
cin >> a >> b;
for (int i = 1; i <= b; i++)
{
ans ^= (int)floor(a * i);
}

}

cout << ans;
}

P5732 【深基5.习7】杨辉三角

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 <iostream>

using namespace std;
const int N = 25;
int arr[N][N];

int main()
{
int n;
cin >> n;
arr[0][0] = 1;
for (int i = 1; i < n; i++)
{
arr[i][i] = 1;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
arr[i][j] = arr[i - 1 ][j] + arr[i - 1][j - 1];
}

}


for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}

P1789 【Mc生存】插火把

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
#include <iostream>
using namespace std;
const int N = 105;
int arr[N][N];

int main() {
int Count = 0;
int num;
cin >> num;
int a, b;
cin >> a >> b;
while (a--) {
int x, y;
cin >> x >> y;
arr[x][y] = 1;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++) {
if (x + i >= 0 && y + j >= 0)
arr[x + i][y + j] = 1;
}
arr[x + 2][y] = 1;
arr[x][y + 2] = 1;

if (x - 2 >= 0)
arr[x - 2][y] = 1;
if (y - 2 >= 0)
arr[x][y - 2] = 1;
}
while (b--) {
int x, y;
cin >> x >> y;
for (int i = -2; i < 3; i++) {
for (int j = -2; j < 3; j++) {
if (x + i >= 0 && y + j >= 0)
arr[x + i][y + j] = 1;
}
}
}

for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num; j++) {
if (arr[i][j] == 0)
Count++;
}
}
cout << Count;
}

压缩技术

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
#include <iostream>
#include <assert.h>
using namespace std;
const int N =205;
int arr[N][N];

int main()
{

int num;
cin >> num;
int sum = num * num;
int count = 0;
int raw = 0, col = 0;
while (count < sum)
{
int a, b;
cin >> a >> b;
count += a + b;

while(a--)
{
arr[raw][col] = 0;
col++;
if (col == num )
{
col = 0;
raw++;
}
}

while(b--)
{
arr[raw][col] = 1;
col++;
if (col == num)
{
col = 0;
raw++;
}
}
}
for (int i = 0; i < num; i++)
{

for (int j = 0; j < num; j++)
cout << arr[i][j] ;
cout << endl;
}

return 0;
}

压缩技术[续集版]

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<cmath>
using namespace std;
const int N =40005;
int arr[N];

int main()
{


int total = 0, count = 0;

char tmp;
char flag = '0'; //注意赋值为字符而不是
int i = 0;
while (cin >> tmp)
{
total++;
if (tmp == flag)
count++;
else
{
arr[i++] = count;
flag = tmp;
count = 1;
}
}

cout << sqrt(total) << " ";

for(int j =0; j < i; j++)
{
cout << arr[j] << " ";

}
cout << count;

return 0;
}

P1205 [USACO1.2] 方块转换 Transformations

转180度即转两次90度,转270次即转三次90度,我们可以通过90度旋转函数调用来实现图片旋转。

旋转矩阵我们可以通过先转置矩阵,然后再将第i行与n-i行进行交换,得到顺时针旋转90度的矩阵。

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<iostream>
using namespace std;

const int N = 15;
char matrix[N][N];
char matrixcopy[N][N];
char matrix2[N][N];

int n;

// 转置矩阵
void transpose() {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
swap(matrix[i][j], matrix[j][i]);
}
}
}

// 水平翻转
void reverseRows() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n / 2; j++) {
swap(matrix[i][j], matrix[i][n - j - 1]);
}
}
}

// 顺时针旋转矩阵
void rotateClockwise() {
transpose();
reverseRows();
}

// 判断结果
bool JudgeRes() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] != matrix2[i][j]) {
return false;
}
}
}
return true;
}

// 初始化矩阵
void Init() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = matrixcopy[i][j];
}
}
}

// 检查旋转一次是否匹配
bool res1() {
rotateClockwise();
if (JudgeRes()) return true;
return false;
}

// 检查旋转两次是否匹配
bool res2() {
rotateClockwise();
if (JudgeRes()) return true;
return false;
}

// 检查旋转三次是否匹配
bool res3() {
rotateClockwise();
if (JudgeRes()) return true;
return false;
}

// 检查水平翻转是否匹配
bool res4() {
Init();
reverseRows();
if (JudgeRes()) return true;
return false;
}

// 检查旋转后水平翻转是否匹配
bool res5() {
bool flag;
res1();
if (JudgeRes()) return true;
res2();
if (JudgeRes()) return true;
res3();
if (JudgeRes()) return true;
return false;
}

// 检查原始矩阵是否匹配
bool res6() {
Init();
if (JudgeRes()) return true;
return false;
}

// 无操作,总是返回真
bool res7() {
return true;
}

// 判断函数
void judge() {
if (res1()) { cout << 1 << endl; return; }
if (res2()) { cout << 2 << endl; return; }
if (res3()) { cout << 3 << endl; return; }
if (res4()) { cout << 4 << endl; return; }
if (res5()) { cout << 5 << endl; return; }
if (res6()) { cout << 6 << endl; return; }
if (res7()) { cout << 7 << endl; return; }
}

int main() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrixcopy[i][j] = matrix[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix2[i][j];
}
}
judge();
return 0;
}