【新手上路】洛谷入门4:数组题单题解
莫道桑榆晚,为霞尚满天。
·
莫道桑榆晚,为霞尚满天。
-
- [P1428 小鱼比可爱](https://www.luogu.com.cn/problem/P1428)
- [P1427 小鱼的数字游戏](https://www.luogu.com.cn/problem/P1427)
- [P5727 【深基5.例3】冰雹猜想](https://www.luogu.com.cn/problem/P5727)
- [P1047 [NOIP 2005 普及组] 校门外的树](https://www.luogu.com.cn/problem/P1047)
- [P5728 【深基5.例5】旗鼓相当的对手](https://www.luogu.com.cn/problem/P5728)
- [P5729 【深基5.例7】工艺品制作](https://www.luogu.com.cn/problem/P5729)
- [P2550 [AHOI2001] 彩票摇奖](https://www.luogu.com.cn/problem/P2550)
- [P2615 [NOIP 2015 提高组] 神奇的幻方](https://www.luogu.com.cn/problem/P2615)
- [P5730 【深基5.例10】显示屏](https://www.luogu.com.cn/problem/P5730)
- [P1554 梦中的统计](https://www.luogu.com.cn/problem/P1554)
- [P2141 [NOIP 2014 普及组] 珠心算测验](https://www.luogu.com.cn/problem/P2141)
- [P1614 爱与愁的心痛](https://www.luogu.com.cn/problem/P1614)
- [P2911 [USACO08OCT] Bovine Bones G](https://www.luogu.com.cn/problem/P2911)
- [P1161 开灯](https://www.luogu.com.cn/problem/P1161)
- [P5731 【深基5.习6】蛇形方阵](https://www.luogu.com.cn/problem/P5731)
- [P5732 【深基5.习7】杨辉三角](https://www.luogu.com.cn/problem/P5732)
- [P1789 【Mc生存】插火把](https://www.luogu.com.cn/problem/P1789)
- [P1319 压缩技术](https://www.luogu.com.cn/problem/P1319)
- [P1320 压缩技术(续集版)](https://www.luogu.com.cn/problem/P1320)
- [P1205 [USACO1.2] 方块转换 Transformations](https://www.luogu.com.cn/problem/P1205)
P1428 小鱼比可爱
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, a[101];
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
int cnt = 0;
for (int j = 1; j < i; j++)
{
if (a[j] < a[i])
++cnt;
}
cout << cnt << " ";
}
}
P1427 小鱼的数字游戏
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, a[101], cnt = 1, temp;
cin >> temp;
while (temp)
{
a[cnt++] = temp;
cin >> temp;
}
for (int i = cnt - 1; i > 0; --i)
{
cout << a[i] << " ";
}
}
P5727 【深基5.例3】冰雹猜想
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> pro;
pro.push_back(n);
while (n != 1)
{
if (n % 2)
{
n = n * 3 + 1;
}
else
{
n /= 2;
}
pro.push_back(n);
}
int l = pro.size();
for (int i = l - 1; i >= 0; --i)
{
cout << pro[i] << " ";
}
}
P1047 [NOIP 2005 普及组] 校门外的树
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int l, m;
cin >> l >> m;
vector<bool> left(l, true);
while (m--)
{
int u, v;
cin >> u >> v;
for (int i = u; i <= v; ++i)
{
left[i] = false;
}
}
int cnt = 0;
for (int i = 0; i <= l; ++i)
{
if (left[i] == true)
cnt++;
}
cout << cnt;
}
P5728 【深基5.例5】旗鼓相当的对手
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, s[1001][3];
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> s[i][0] >> s[i][1] >> s[i][2];
}
int cnt = 0;
for (int i = 0; i < n - 1; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (abs(s[i][0] - s[j][0]) <= 5 && abs(s[i][1] - s[j][1]) <= 5 && abs(s[i][2] - s[j][2]) <= 5 && abs(s[i][0] + s[i][1] + s[i][2] - s[j][0] - s[j][1] - s[j][2]) <= 10)
{
++cnt;
}
}
}
cout << cnt;
}
P5729 【深基5.例7】工艺品制作
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int w, x, h, q;
cin >> w >> x >> h >> q;
vector<vector<vector<bool>>> li(w, vector<vector<bool>>(x, vector<bool>(h, true)));
while (q--)
{
int x1, y1, z1, x2, y2, z2;
cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
for (int i = x1 - 1; i < x2; ++i)
{
for (int j = y1 - 1; j < y2; ++j)
{
for (int k = z1 - 1; k < z2; ++k)
{
li[i][j][k] = false;
}
}
}
}
int cnt = 0;
for (int i = 0; i < w; ++i)
{
for (int j = 0; j < x; ++j)
{
for (int k = 0; k < h; ++k)
{
if (li[i][j][k])
cnt++;
}
}
}
cout << cnt;
}
P2550 [AHOI2001] 彩票摇奖
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, out[7] = {0}, jiang[7];
cin >> n;
for (int i = 0; i < 7; ++i)
{
cin >> jiang[i];
}
int temp;
for (int i = 0; i < n; ++i)
{
int cnt = 0;
for (int j = 0; j < 7; ++j)
{
cin >> temp;
for (int i = 0; i < 7; ++i)
{
if (temp == jiang[i])
cnt++;
}
}
out[cnt - 1]++;
}
for (int i = 6; i >= 0; --i)
{
cout << out[i] << " ";
}
}
P2615 [NOIP 2015 提高组] 神奇的幻方
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, huan[40][40] = {0};
cin >> n;
int x = 1, y = n / 2 + 1;
huan[x][y] = 1;
for (int i = 2; i <= n * n; ++i)
{
if (x == 1 && y != n)
{
x = n;
y += 1;
}
else if (y == n && x != 1)
{
y = 1;
x -= 1;
}
else if (x == 1 && y == n)
{
x += 1;
}
else if (x != 1 && y != n)
{
if (huan[x - 1][y + 1] == 0)
{
x -= 1;
y += 1;
}
else
{
x += 1;
}
}
huan[x][y] = i;
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
cout << huan[i][j] << " ";
}
cout << endl;
}
}
P5730 【深基5.例10】显示屏
- 一道极其ex人的题
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
string s, ans[101];
cin >> s;
int l = s.size();
for (int i = 0; i < l; ++i)
{
if (s[i] == '0')
{
ans[0] += "XXX";
ans[1] += "X.X";
ans[2] += "X.X";
ans[3] += "X.X";
ans[4] += "XXX";
}
else if (s[i] == '1')
{
ans[0] += "..X";
ans[1] += "..X";
ans[2] += "..X";
ans[3] += "..X";
ans[4] += "..X";
}
else if (s[i] == '2')
{
ans[0] += "XXX";
ans[1] += "..X";
ans[2] += "XXX";
ans[3] += "X..";
ans[4] += "XXX";
}
else if (s[i] == '3')
{
ans[0] += "XXX";
ans[1] += "..X";
ans[2] += "XXX";
ans[3] += "..X";
ans[4] += "XXX";
}
else if (s[i] == '4')
{
ans[0] += "X.X";
ans[1] += "X.X";
ans[2] += "XXX";
ans[3] += "..X";
ans[4] += "..X";
}
else if (s[i] == '5')
{
ans[0] += "XXX";
ans[1] += "X..";
ans[2] += "XXX";
ans[3] += "..X";
ans[4] += "XXX";
}
else if (s[i] == '6')
{
ans[0] += "XXX";
ans[1] += "X..";
ans[2] += "XXX";
ans[3] += "X.X";
ans[4] += "XXX";
}
else if (s[i] == '7')
{
ans[0] += "XXX";
ans[1] += "..X";
ans[2] += "..X";
ans[3] += "..X";
ans[4] += "..X";
}
else if (s[i] == '8')
{
ans[0] += "XXX";
ans[1] += "X.X";
ans[2] += "XXX";
ans[3] += "X.X";
ans[4] += "XXX";
}
else if (s[i] == '9')
{
ans[0] += "XXX";
ans[1] += "X.X";
ans[2] += "XXX";
ans[3] += "..X";
ans[4] += "XXX";
}
if (i != l - 1)
{
ans[0] += ".";
ans[1] += ".";
ans[2] += ".";
ans[3] += ".";
ans[4] += ".";
}
}
cout << ans[0] << endl
<< ans[1] << endl
<< ans[2] << endl
<< ans[3] << endl
<< ans[4] << endl;
}
P1554 梦中的统计
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int times[10] = {0};
long long m, n;
cin >> m >> n;
for (long long i = m; i <= n; ++i)
{
string s = to_string(i);
int l = s.size();
for (int j = 0; j < l; ++j)
{
if (s[j] == '0')
{
times[0]++;
}
else if (s[j] == '1')
{
times[1]++;
}
else if (s[j] == '2')
{
times[2]++;
}
else if (s[j] == '3')
{
times[3]++;
}
else if (s[j] == '4')
{
times[4]++;
}
else if (s[j] == '5')
{
times[5]++;
}
else if (s[j] == '6')
{
times[6]++;
}
else if (s[j] == '7')
{
times[7]++;
}
else if (s[j] == '8')
{
times[8]++;
}
else if (s[j] == '9')
{
times[9]++;
}
}
}
for (int i = 0; i < 10; ++i)
{
cout << times[i] << " ";
}
}
P2141 [NOIP 2014 普及组] 珠心算测验
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, a[101], b[101];
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> a[i];
b[i] = a[i];
}
int cnt = 0;
for (int i = 0; i < n - 1; ++i)
{
for (int j = i + 1; j < n; ++j)
{
for (int k = 0; k < n; ++k)
{
if (a[i] + a[j] == b[k])
{
cnt++;
b[k] = 0; // 防止被再次计算
}
}
}
}
cout << cnt;
}
P1614 爱与愁的心痛
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, m, mad[30001];
cin >> n >> m;
for (int i = 0; i < n; ++i)
{
cin >> mad[i];
}
int minn = INT_MAX;
for (int i = 0; i < n - m + 1; ++i)
{
int total = 0;
for (int j = i; j < i + m; ++j)
{
total += mad[j];
}
minn = min(total, minn);
}
cout << minn;
}
P2911 [USACO08OCT] Bovine Bones G
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int s1, s2, s3, cnt[81] = {0};
cin >> s1 >> s2 >> s3;
for (int i = 1; i <= s1; ++i)
{
for (int j = 1; j <= s2; ++j)
{
for (int k = 1; k <= s3; ++k)
{
cnt[i + j + k]++;
}
}
}
int maxx = 0, index;
for (int i = s1 + s2 + s3; i > 0; --i)
{
if (cnt[i] >= maxx)
{
maxx = cnt[i];
index = i;
}
}
cout << index;
}
P1161 开灯
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, light[2000001] = {0}, t;
cin >> n;
double a;
for (int i = 0; i < n; ++i)
{
cin >> a >> t;
for (int j = 1; j <= t; ++j)
{
if (light[int(j * a)] == 0)
{
light[int(j * a)] = 1;
}
else
{
light[int(j * a)] = 0;
}
}
}
for (int i = 0;; ++i)
{
if (light[i])
{
cout << i;
break;
}
}
}
P5731 【深基5.习6】蛇形方阵
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, snake[11][11] = {0}, x = 1, y = 1, value = 1;
cin >> n;
while (value < n * n)
{
// 向右直至触界或有数
while (snake[x][y + 1] == 0 && y + 1 <= n)
{
snake[x][y++] = value++;
}
// 向下
while (snake[x + 1][y] == 0 && x + 1 <= n)
{
snake[x++][y] = value++;
}
// 向左
while (snake[x][y - 1] == 0 && y - 1 >= 1)
{
snake[x][y--] = value++;
}
// 向上
while (snake[x - 1][y] == 0 && x - 1 >= 1)
{
snake[x--][y] = value++;
}
}
snake[x][y] = value;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
printf("%3d", snake[i][j]);
}
cout << endl;
}
}
P5732 【深基5.习7】杨辉三角
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, yh[21][21] = {0};
cin >> n;
for (int i = 1; i <= n; ++i)
{
yh[i][1] = 1; // 每行第一个元素为0
for (int j = 2; j <= i; ++j) // 每行有i个元素
{
yh[i][j] = yh[i - 1][j] + yh[i - 1][j - 1]; // 行中每个元素为其上元素加
// 其上左边那个元素
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= i; ++j)
{
cout << yh[i][j] << " ";
}
cout << endl;
}
}
P1789 【Mc生存】插火把
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, m, k, gro[101][101] = {0};
cin >> n >> m >> k;
while (m--)
{
int x, y;
cin >> x >> y;
gro[x][y] = 1; // 表示有光或有物件
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
int index_x = x + i;
int index_y = y + j;
if (index_x <= n && index_y <= n && index_x >= 1 && index_y >= 1)
{
gro[index_x][index_y] = 1;
}
}
}
gro[min(x + 2, n)][y] = 1;
gro[max(x - 2, 1)][y] = 1;
gro[x][max(y - 2, 1)] = 1;
gro[x][min(y + 2, n)] = 1;
}
while (k--)
{
int x, y;
cin >> x >> y;
gro[x][y] = 1;
for (int i = -2; i <= 2; ++i)
{
for (int j = -2; j <= 2; ++j)
{
int index_x = x + i;
int index_y = y + j;
if (index_x <= n && index_y <= n && index_x >= 1 && index_y >= 1)
{
gro[index_x][index_y] = 1;
}
}
}
}
int cnt = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (gro[i][j] == 0)
{
cnt++;
}
}
}
cout << cnt;
}
P1319 压缩技术
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int temp, total = 0, cnt = 0;
vector<int> nn;
while (cin >> temp)
{
total += temp;
while (temp--)
{
nn.push_back(cnt % 2);
}
cnt++;
if (total >= n * n)
{
break;
}
}
int l = nn.size();
for (int i = 0; i < l; i += n)
{
for (int j = 0; j < n; ++j)
{
cout << nn[i + j];
}
cout << endl;
}
}
P1320 压缩技术(续集版)
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin, s);
int len = s.size();
for (int i = 0; i < len - 1; i++)
{
string ss;
getline(cin, ss);
s += ss;
}
cout << len << " ";
len = s.size();
int index_0 = 0, index_1 = 0;
char a = '1';
while (1)
{
if (a == '1')
{
index_1 = s.find(a, index_0);
if (index_1 == -1)
{
cout << len - index_0;
break;
}
else
{
cout << index_1 - index_0 << " ";
a ^= 1;
}
}
else
{
index_0 = s.find(a, index_1);
if (index_0 == -1)
{
cout << len - index_1;
break;
}
else
{
cout << index_0 - index_1 << " ";
a ^= 1;
}
}
}
}
P1205 [USACO1.2] 方块转换 Transformations
代码如下:
#include <bits/stdc++.h>
using namespace std;
bool _1(int n, vector<vector<char>> nn1, vector<vector<char>> nn2)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (nn2[i][j] != nn1[n - 1 - j][i])
{
return false;
}
}
}
return true;
}
bool _2(int n, vector<vector<char>> nn1, vector<vector<char>> nn2)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (nn2[i][j] != nn1[n - 1 - i][n - 1 - j])
{
return false;
}
}
}
return true;
}
bool _3(int n, vector<vector<char>> nn1, vector<vector<char>> nn2)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (nn2[i][j] != nn1[j][n - 1 - i])
{
return false;
}
}
}
return true;
}
bool _4(int n, vector<vector<char>> nn1, vector<vector<char>> nn2)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (nn2[i][j] != nn1[i][n-1-j])
{
return false;
}
}
}
return true;
}
vector<vector<char>> chuli(int n, vector<vector<char>> nn1)
{
vector<vector<char>> nn2(n, vector<char>(n));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
nn2[i][j] = nn1[j][i];
}
}
return nn2;
}
bool _6(int n, vector<vector<char>> nn1, vector<vector<char>> nn2)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (nn2[i][j] != nn1[i][j])
{
return false;
}
}
}
return true;
}
int main()
{
int n;
cin >> n;
vector<vector<char>> nn1(n, vector<char>(n));
vector<vector<char>> nn2(n, vector<char>(n));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cin >> nn1[i][j];
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cin >> nn2[i][j];
}
}
while (1)
{
if (_1(n, nn1, nn2))
{
cout << 1;
break;
}
else if (_2(n, nn1, nn2))
{
cout << 2;
break;
}
else if (_3(n, nn1, nn2))
{
cout << 3;
break;
}
else if (_4(n, nn1, nn2))
{
cout << 4;
break;
}
else if (_1(n, chuli(n, nn1), nn2) || _2(n, chuli(n, nn1), nn2) || _3(n, chuli(n, nn1), nn2))
{
cout << 5;
break;
}
else if (_6(n, nn1, nn2))
{
cout << 6;
break;
}
else
{
cout << 7;
break;
}
}
}
更多推荐



所有评论(0)