第五章 数 组¶
第一节 一维数组¶
2034:【例5.1】反序输出¶
提示
题目告诉你有n个数输入,但没有告诉你n是几
烦人不烦人,这里只能用while(cin)
读入的数据,依次存放到数组里
这里面,如何体现“依次”呢?
就是a[idx++] = x,这样的表达
代码
2035:【例5.2】平移数据¶
提示
向左移动,转圈移动
这种收尾相连、转圈移动,是非常常见的编程场景
这个问题简单一点,我们完全可以,for循环下标从2开始,枚举到n
最后输出一个a[1]来解决。
其实,这样做,并没有实现数据存储上的移动,只是利用下标来完成。
当然,我们也可以真的让数据抬抬屁股,向前移动一个位置。
代码
代码2
2036:【例5.3】开关门¶
提示
注意看这句话,“第一个服务员把所有的房间门都打开了”
这句话是有含义的,暗示了,刚开始的时候,
房间的门,是关着的。
1号服务员,把1的倍数,全部处理一遍
2号服务员,把2的倍数,全部处理一遍
i号服务器,把i的倍数,全部处理一遍
最后看,哪些门是打开的
提示2
如何表示门呢?
int door[1010];
默认是0,表示门是关着的
开门就是0->1,关门就是1->0
代码
提示3
仔细观察,最后的输出,你会发现,这些数字,是有规律的
提示4
规律就是,输出的都是完全平方数
为什么?
因为,门刚开始的时候是关着的,做偶数次操作,依然保持关着不变;做奇数次操作,才会变成开着的
谁能操作一个门呢,只有是这个门编号的约数,才能操作
那么,问题就转变为,哪些数,约数的个数,是奇数
我们约数都是成对出现的,只有完全平方数,有一个特殊的完全平方根
代码2
2037:【例5.4】约瑟夫问题¶
提示
约瑟夫问题,是一个经典的数学问题,我听说有相关的数学公式,但我并不会那些
我只会用代码来表达
就是用代码,模拟整个过程。
比较好写的代码,是用一个队列,表达整个数数和出队的过程。
首先,队列,有队头和队尾,从队尾入队,从队头出队,先记住这个逻辑规则。
然后,你就学会了,一个数据结构,队列。
接着,我们来看如何利用队列这个数据结构,模拟这个出队的过程。
我们先设一个变量,来维护123的报数,只要报到m,就恢复成1
然后,队头出队,报数,如果报的不是m,重新入队;如果报的是m,直接输出他的编号,不让其重新入队,相当于踢掉它。
提示2
如何用代码表达队列?
https://www.zqiceberg.com/CPP/STL/#queueint
请访问这个链接,学习队列的基本操作,模仿使用
提示3
这个题目,老师提供多种方法的实现
以此来告诉大家,写代码,有很多种写法
你熟练使用其中一种工具,或者一维数组,或者队列,都可以解决问题
就是实现的过程,有的麻烦饶一点,有的利用数据结构的特点,使得代码简洁易实现。
代码-使用队列
代码-使用一维数组-模拟01
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
bool vis[N];
int n, m;
int cnt;
vector<int> A;
int main(){
cin >> n >> m;
int i = 0;
while (cnt < n) {
int num = 0;
while (num < m - 1) {
while (vis[i]) i = (i + 1) % n;
num++;
i = (i + 1) % n;
}
while (vis[i]) i = (i + 1) % n;
A.push_back(i + 1);
vis[i] = true;
cnt++;
i = (i + 1) % n;
}
for (auto x : A) cout << x << ' ';
return 0;
}
代码-使用一维数组-模拟02
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int a[N];
int n, m, p;
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++) a[i] = i + 1;
int j = 0;
while (n){
j = (j + m - 1) % n;
printf("%d ", a[j]);
for (int i = j + 1; i < n; i++) a[i - 1] = a[i];
n--;
}
puts("");
return 0;
}
代码-使用双链表
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m;
int L[N], R[N];
void remove(int k)
{
L[R[k]] = L[k];
R[L[k]] = R[k];
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++){
R[i] = (i + 1) % n;
L[i] = (i - 1 + n) % n;
}
int k = 0;
for (int i = 0; i < n; i++){
for (int j = 1; j < m; j++) k = R[k];
printf("%d ", k+1);
remove(k);
k = R[k]; //下一个人
}
puts("");
return 0;
}
2038:【例5.5】最大数位置¶
代码
2039:【例5.6】冒泡排序¶
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N], n;
int main(){
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < n - i; j++)
if (a[j] < a[j + 1]) swap(a[j], a[j + 1]);
for (int i = 0; i < n; i++) cout << a[i] << '\n';
return 0;
}
2040:【例5.7】筛选法找质数¶
代码
// 埃式筛法
#include <bits/stdc++.h>
using namespace std;
const int N = 1010, M = 510;
int primes[M], idx, n;
bool st[N];
int main(){
cin >> n;
for (int i = 2; i <= n; i++){
if (!st[i]){
primes[idx++] = i;
for (int j = i + i; j <= n; j += i) st[j] = true;
}
}
for (int i = 0; i < idx; i++) cout << primes[i] << '\n';
return 0;
}
代码02
// 线性筛法
#include <bits/stdc++.h>
using namespace std;
const int N = 1010, M = 510;
int primes[M], idx, n;
bool st[N];
int main(){
cin >> n;
for (int i = 2; i <= n; i++){
if (!st[i]) primes[idx++] = i;
for (int j = 0; primes[j] <= n / i; j++){
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
for (int i = 0; i < idx; i++) cout << primes[i] << '\n';
return 0;
}
1102:与指定数字相同的数的个数¶
代码
代码02
1103:陶陶摘苹果¶
代码
1104:计算书费¶
代码
1105:数组逆序重存放¶
代码
1106:年龄与疾病¶
代码
// 输出0-18, 19-35, 36-60, 61以上,年龄的比例
#include <iostream>
using namespace std;
int a, b, c, d;
int n;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
// 判断这个人的年龄,属于哪个分类的
if (x >= 0 && x <= 18) a++;
if (x >= 19 && x <= 35) b++;
if (x >= 36 && x <= 60) c++;
if (x >= 61) d++;
}
printf("%.2lf%%\n", 1.0 * a / n * 100);
printf("%.2lf%%\n", 1.0 * b / n * 100);
printf("%.2lf%%\n", 1.0 * c / n * 100);
printf("%.2lf%%\n", 1.0 * d / n * 100);
return 0;
}
1107:校门外的树¶
代码
#include <iostream>
using namespace std;
const int N=10010;
int tree[N]; //tree[i] = 0代表有树
int L, M;
int cnt;
int main() {
cin >> L >> M;
while (M--) {
int l, r;
cin >> l >> r;
for (int i = l; i <= r; i++) tree[i] = 1;
}
for (int i = 0; i <= L; i++)
if (tree[i] == 0) cnt++;
cout << cnt << '\n';
return 0;
}
代码02
1108:向量点积计算¶
代码
1109:开关灯¶
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
bool light[N]; //默认是false,代表开启状态
int n, m;
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++)
for (int j = i; j <= n; j += i)
light[j] = !light[j];
bool flag = false;
for (int j = 1; j <= n; j++)
if (light[j]) {
if (flag) cout << ',';
cout << j;
flag = true;
}
return 0;
}
1110:查找特定的值¶
代码
代码02
1111:不高兴的津津¶
代码
1112:最大值和最小值的差¶
代码
1113:不与最大数相同的数字之和¶
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
int n;
int maxn = -2e9;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
maxn = max(maxn, a[i]);
}
long long sum = 0;
for (int i = 0; i < n; i++)
if (a[i] != maxn) sum += a[i];
cout << sum << endl;
return 0;
}
1114:白细胞计数¶
提示
这个题目有坑点,“不包括已扣除的两个样本”
有可能有多个最大,多个最小,所以仅靠数值去掉最大最小来判断,就会造成计算错误
比如,n个相同的数
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 310;
double q = -1.0, w = 10000.0;
double e, r, a[N];
int n;
int x, y;
int main() {
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) {
if(a[i]>q) q = a[i]; //q是最大值
if(a[i]<w) w = a[i];
}
for(int i=1; i<=n; i++) e+=a[i];
e = e - q - w;
r = e/(n-2);
double m = -1.0; //维护最大的偏差
for (int i = 1; i <= n; i++) {
if (a[i] == q && !x) {
x = 1;
continue;
}
if (a[i] == w && !y) {
y = 1;
continue;
}
m = max(m, fabs(a[i] - r));
}
printf("%.2lf %.2lf\n", r, m);
return 0;
}
代码02
// 会排序之后,掐头去尾,就可以了
#include <bits/stdc++.h>
using namespace std;
double a[310];
int n;
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n); // a[0], a[n - 1]
double sum = 0.0;
for (int i = 1; i <= n - 2; i++) sum += a[i];
double ave = sum / (n - 2);
double ret = 0;
for (int i = 1; i <= (n - 2); i++)
ret = max(ret, fabs(a[i] - ave));
printf("%.2lf %.2lf\n", ave, ret);
return 0;
}
1115:直方图¶
代码
#include <iostream>
using namespace std;
const int N = 10010;
int n, cnt[N], a[N], maxn = -1;
int main() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
maxn = max(maxn, a[i]);
}
for (int i = 0; i <= maxn; i++) {
for (int j = 1; j <= n; j++)
if (a[j] == i) cnt[i]++;
}
for (int i = 0; i <= maxn; i++) cout << cnt[i] << '\n';
return 0;
}
1116:最长平台¶
代码
代码
// 这种写法很经典,要理解,掌握,会写
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N], n;
int maxn;
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
int j = i;
while (j < n && a[j] == a[i]) j++;
maxn = max(maxn, j - i);
i = j - 1;
}
cout << maxn << '\n';
return 0;
}
1117:整数去重¶
提示
用st数组维护,这个数值是否出现过。没出现,就输出,并标记这个数值已经出现
代码
1118:铺地毯¶
提示
这道题目,不能真的去模拟把每张地毯都两层循环铺一遍,标记一下
那样的话,如果这个地毯很大,标记数组就得开很大,会炸数组
需要换个角度,把所有地毯的信息,存起来,这样存,二维数组的第二维,只需要开4
然后再重新遍历一遍地毯,n <= 10000, 看目标坐标是否被当前地毯覆盖
代码
#include<bits/stdc++.h>
using namespace std;
const int N =10010;
int a[5][N],n,x,y,cnt;
int main() {
cin >>n;
for(int i=1; i<=n; i++) {
cin >> a[1][i] >> a[2][i] >> a[3][i] >> a[4][i] ;
}
cin >> x >> y;
for(int i=1; i<=n; i++) {
if(a[1][i]<=x && a[2][i]<=y && (a[3][i]+a[1][i])>=x && (a[4][i]+a[2][i])>=y) {
cnt=i;
}
}
if(cnt>0) cout <<cnt;
else cout << -1;
return 0;
}
第二节 二维数组¶
2041:【例5.9】新矩阵¶
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N][N], n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (i == j || (i + j) == (1 + n)) a[i][j] += 10;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) cout << a[i][j] << ' ';
puts("");
}
return 0;
}
2042:【例5.10】稀疏矩阵¶
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int a[N][N], n, m;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j]) {
printf("%d %d %d\n", i, j, a[i][j]);
}
return 0;
}
2043:【例5.11】杨辉三角形¶
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N][N];
int n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
if (j == 1 || j == i) a[i][j] = 1;
else a[i][j] = a[i-1][j] + a[i-1][j-1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) cout << a[i][j] << ' ';
puts("");
}
return 0;
}
2044:【例5.12】回文字串¶
提示
此题样例数据给的有问题,按照题面去做即可。
字符串不包括最后的那个点
输出的No是No不是No。
abba.
这是一个回文
代码
代码02
代码03
2045:【例5.13】蛇形填数¶
提示
此题需要反复练习
代码
#include <iostream>
#include <cstdio>
using namespace std;
int a[25][25], n;
int idx;
int main() {
cin >> n;
int i = 1, j = n;
while (idx < n * n) {
while (i <= n && !a[i][j]) a[i++][j] = ++idx;
i--, j--;
while (j >= 1 && !a[i][j]) a[i][j--] = ++idx;
j++, i--;
while (i >= 1 && !a[i][j]) a[i--][j] = ++idx;
i++, j++;
while (j <= n && !a[i][j]) a[i][j++] = ++idx;
i++, j--;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) cout << a[i][j] << ' ';
puts("");
}
return 0;
}
代码02
#include<bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N][N], n;
int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; // delta
int idx = 1;
int main(){
cin >> n;
int i = 1, j = n;
while (idx < n * n){
for (int k = 0; k < 4; k++){
while (true){
a[i][j] = idx++;
i += dx[k], j += dy[k];
if (i > n || i < 1 || j > n || j < 1 || a[i][j]){
i -= dx[k], j -= dy[k], idx--;
break;
}
}
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++) cout << a[i][j] << ' ';
puts("");
}
return 0;
}
代码03
#include <bits/stdc++.h>
using namespace std;
int a[25][25];
int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int delta_x[] = {-1, -1, 1, 1}, delta_y[] = {-1, 1, 1, -1};
int main() {
int n;
cin >> n;
int idx = 0, i = 1, j = n;
int k = 0;
while (idx < n * n) {
// printf("---%d %d %d\n", i, j, idx + 1);
a[i][j] = ++idx;
i += dx[k], j += dy[k];
if (i > n || i < 1 || j > n || j < 1 || a[i][j]) {
i += delta_x[k], j += delta_y[k];
k = (k + 1) % 4;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) cout << a[i][j] << ' ';
puts("");
}
return 0;
}
1119:矩阵交换行¶
代码
#include <iostream>
using namespace std;
int a[10][10], n, m;
int main() {
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)
cin >> a[i][j];
cin >> n >> m;
for (int j = 1; j <= 5; j++)
swap(a[m][j], a[n][j]);
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++)
cout << a[i][j] << ' ';
puts("");
}
return 0;
}
1120:同行列对角线的格¶
提示
此时,也是要反复练习
观察在一条直线上的点,行列坐标有什么规律
还有要注意遍历的方向问题,最后一个是从左下到右上,就从下到上遍历,就刚好
还有这种控制输出格式的情况,我比较喜欢用printf
代码
#include <bits/stdc++.h>
using namespace std;
int n, x, y;
int main() {
cin >> n >> x >> y;
for (int j = 1; j <= n; j++) printf("(%d,%d) ", x, j);
puts("");
for (int i = 1; i <= n; i++) printf("(%d,%d) ", i, y);
puts("");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i - j == x - y) printf("(%d,%d) ", i, j);
puts("");
for (int i = n; i >= 1; i--)
for (int j = 1; j <= n; j++)
if (i + j == x + y) printf("(%d,%d) ", i, j);
puts("");
return 0;
}
1121:计算矩阵边缘元素之和¶
代码
// 第1行,第n行
// 第1列,第m列
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N][N];
int n, m;
int main() {
cin >> n >> m;
int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int x;
cin >> x;
if (i == 1 || j == 1 || i == n || j == m) sum += x;
}
cout << sum << endl;
return 0;
}
1122:计算鞍点¶
提示
这个代码,体现了一个思想,先预处理出来一些信息
然后再重新遍历一遍,就好搞了
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int a[N][N], row[N], col[N];
int main() {
memset(row, -1, sizeof row);
memset(col, 0x3f, sizeof col);
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++) {
cin >> a[i][j];
row[i] = max(row[i], a[i][j]);
col[j] = min(col[j], a[i][j]);
}
bool flag = false;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (a[i][j] == row[i] && a[i][j] == col[j]) {
cout << i << ' ' << j << ' ' << a[i][j] << '\n';
flag = true;
break;
}
}
if (flag) break;
}
if (!flag) cout << "not found" << '\n';
return 0;
}
1123:图像相似度¶
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N][N], b[N][N];
int n, m;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
int cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int x;
cin >> x;
if (x == a[i][j]) cnt++;
}
double res = cnt * 1.0 / (n * m);
printf("%.2lf\n", res * 100);
return 0;
}
1124:矩阵加法¶
代码
#include <bits/stdc++.h>
using namespace std;
int a[110][110];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int x;
cin >> x;
a[i][j] += x;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cout << a[i][j] << ' ';
puts("");
}
return 0;
}
1125:矩阵乘法¶
提示
矩阵乘法的规则,很可能看不懂
这个计算逻辑,虽说是高中课本中的内容,但在海淀区五年级期末考试中,作为附加题出现
这是真事,有一回一个学生考完试和我说的,说自己附加题做出来的,正常的答题不会...
这个矩阵乘法,你拿一个具体的例子,第一个矩阵的这一行,依次去乘第二个矩阵的这一列
得到的和,放到目标矩阵的位置上
在写代码上,就是多层循环,每层循环的含义,对应上,就好理解了
代码
#include <iostream>
using namespace std;
const int N = 110;
int a[N][N], b[N][N], c[N][N];
int n, m, k;
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> a[i][j];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) cin >> b[i][j];
// 前面两层循环,枚举目标矩阵的每一个位置
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
for (int p = 1; p <= m; p++)
c[i][j] += a[i][p] * b[p][j]; // a的第i行,依次乘b的第j列
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) cout << c[i][j] << ' ';
puts("");
}
return 0;
}
1126:矩阵转置¶
代码
1127:图像旋转¶
代码
1128:图像模糊处理¶
代码
#include <iostream>
#include <cmath>
using namespace std;
const int N = 110;
int a[N][N], b[N][N], n, m;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (i == 1 || i == n || j == 1 || j == m) b[i][j] = a[i][j];
else b[i][j] = round((a[i][j] + a[i-1][j] + a[i+1][j] + a[i][j-1] + a[i][j+1]) / 5.0);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cout << b[i][j] << ' ';
puts("");
}
return 0;
}
第三节 字符类型和字符数组¶
2046:【例5.15】替换字母¶
代码
2047:【例5.16】过滤空格¶
提示
过滤多余的空格,并不是把所有空格都删掉
代码
代码02
2048:【例5.18】串排序¶
代码
2049:【例5.19】字符串判等¶
提示
读进来 getline() 空格干掉,统一转成小写
代码
#include <bits/stdc++.h>
using namespace std;
string s1, s2, a, b;
int main(){
getline(cin, s1);
getline(cin, s2);
for (int i = 0, len = s1.size(); i < len; i++)
if (s1[i] != ' ') {
if (s1[i] >= 'A' && s1[i] <= 'Z') s1[i] += 32;
a += s1[i];
}
for (int i = 0, len = s2.size(); i < len; i++)
if (s2[i] != ' ') {
if (s2[i] >= 'A' && s2[i] <= 'Z') s2[i] += 32;
b += s2[i];
}
if (a == b) puts("YES");
else puts("NO");
return 0;
}
2050:【例5.20】字串包含¶
提示
使用.find()函数
代码
1839:【05NOIP提高组】谁拿了最多奖学金¶
代码
#include <iostream>
using namespace std;
int n;
string top;
int maxn = -1;
int sum;
int main(){
cin >> n;
while (n--){
string s;
int pingjun, pingyi;
char ganbu, xibu;
int lunwen;
cin >> s >> pingjun >> pingyi >> ganbu >> xibu >> lunwen;
int now = 0;
if (pingjun > 80 && lunwen >= 1) now += 8000;
if (pingjun > 85 && pingyi > 80) now += 4000;
if (pingjun > 90) now += 2000;
if (pingjun > 85 && xibu == 'Y') now += 1000;
if (pingyi > 80 && ganbu == 'Y') now += 850;
if (now > maxn){
maxn = now;
top = s;
}
sum += now;
}
cout << top << '\n' << maxn << '\n' << sum << '\n';
return 0;
}
1129:统计数字字符个数¶
代码
1130:找第一个只出现一次的字符¶
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 30, M = 1e5 + 10;
int cnt[N]; //0...25
char s[M];
int main(){
scanf("%s", s);
int len = strlen(s);
//O(n)
for (int i = 0; i < len; i++)
cnt[s[i] - 'a']++;
//O(n)
for (int i = 0; i < len; i++)
if (cnt[s[i] - 'a'] == 1){
cout << s[i] << '\n';
return 0;
}
cout << "no" << '\n';
return 0;
}
代码02
1131:基因相关性¶
代码
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
int main(){
double s;
cin >> s;
string s1, s2;
cin >> s1 >> s2;
int len = (int)s1.size(), cnt = 0;
for (int i = 0; i < len; i++)
if (s1[i] == s2[i]) cnt++;
if (1.0 * cnt / len >= s) cout << "yes" << endl;
else cout << "no" << endl;
return 0;
}
1132:石头剪子布¶
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
string s1, s2;
cin >> n;
while (n--)
{
cin >> s1 >> s2;
int ans;
if (s1 == "Rock" && s2 == "Scissors") ans = 1;
if (s1 == "Rock" && s2 == "Rock") ans = 0;
if (s1 == "Rock" && s2 == "Paper") ans = -1;
if (s1 == "Scissors" && s2 == "Paper") ans = 1;
if (s1 == "Scissors" && s2 == "Scissors") ans = 0;
if (s1 == "Scissors" && s2 == "Rock") ans = -1;
if (s1 == "Paper" && s2 == "Rock") ans = 1;
if (s1 == "Paper" && s2 == "Paper") ans = 0;
if (s1 == "Paper" && s2 == "Scissors") ans = -1;
if (ans == 1) cout << "Player1" << "\n";
else if (ans == 0) cout << "Tie" << "\n";
else cout << "Player2" << "\n";
}
return 0;
}
代码02
#include <bits/stdc++.h>
using namespace std;
string s1, s2;
int main(){
int n;
cin >> n;
while (n--){
int a = 0, b = 0;
cin >> s1 >> s2;
if (s1[0] == 'R'){
if (s2[0] == 'S') a++;
else if (s2[0] == 'P') b++;
}
if (s1[0] == 'P'){
if (s2[0] == 'R') a++;
else if (s2[0] == 'S') b++;
}
if (s1[0] == 'S'){
if (s2[0] == 'P') a++;
else if (s2[0] == 'R') b++;
}
if (a == b) cout << "Tie" << '\n';
if (a > b) cout << "Player1" << '\n';
if (a < b) cout << "Player2" << '\n';
}
return 0;
}
1133:输出亲朋字符串¶
代码
1134:合法C标识符查¶
代码
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt;
int main(){
cin >> s;
if(s[0]>='0'&&s[0]<='9') cnt++;
for(int i=0,len=s.size();i<len;i++) {
if (!((s[i]>='0'&&s[i]<='9')||(s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||s[i]=='_')) cnt++;
if (i == 0 && (s[i] >= '0' && s[i] <= '9')) cnt++;
}
if(cnt >= 1) cout <<"no";
else cout <<"yes";
return 0;
}
1135:配对碱基链¶
1136:密码翻译¶
代码
#include <bits/stdc++.h>
using namespace std;
string s;
int main(){
getline(cin, s);
for (int i = 0, len = s.size(); i < len; i++) {
if ((s[i] >= 'a' && s[i] <= 'y') || (s[i] >= 'A' && s[i] <= 'Y'))
s[i]++;
else if (s[i] == 'z') s[i] = 'a';
else if (s[i] == 'Z') s[i] = 'A';
}
cout << s << '\n';
return 0;
}
代码02
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin, s);
for (int i = 0, len = (int)s.size(); i < len; i++)
{
if (s[i] >= 'a' && s[i] <= 'z') s[i] = 'a' + ((s[i] - 'a') + 1) % 26;
if (s[i] >= 'A' && s[i] <= 'Z') s[i] = 'A' + ((s[i] - 'A') + 1) % 26;
}
cout << s << endl;
return 0;
}
1137:加密的病历单¶
代码
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
cin >> s;
int len = s.size();
for (int i = 0; i < len; i++){
char c = s[i];
if (c >= 'a' && c <= 'z'){
s[i] = 'a' + (c - 'a' + 3) % 26;
s[i] -= 'a' - 'A';
}
else{
s[i] = 'A' + (c - 'A' + 3) % 26;
s[i] += 'a' - 'A';
}
}
reverse(s.begin(), s.end());
cout << s << '\n';
return 0;
}
1138:将字符串中的小写字母转换成大写字母¶
代码
1139:整理药名¶
代码
1140:验证子串¶
提示
.find() 函数
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
string s, s2;
cin >> s >> s2;
if (s2.size() < s.size()){
if(s.find(s2) != string::npos) cout << s2 << ' ' << "is substring of" << ' ' << s;
else cout << "No substring" << '\n';
}
else{
if(s2.find(s) != string::npos) cout << s << ' ' << "is substring of" << ' ' << s2;
else cout << "No substring" << '\n';
}
return 0;
}
1141:删除单词后缀¶
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
int len = s.size();
if (s[len - 2] == 'e' && s[len - 1] == 'r') s.erase(len - 2);
if (s[len - 2] == 'l' && s[len - 1] == 'y') s.erase(len - 2);
if (s[len - 3] == 'i' && s[len - 2] == 'n' && s[len - 1] == 'g') s.erase(len - 3);
cout << s << endl;
return 0;
}
代码02
1142:单词的长度¶
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin, s);
bool flag = false;
for (int i = 0, len = s.size(); i < len; i++) {
while (s[i] == ' ') i++;
if (flag) printf(",");
flag = true;
int j = i;
int cnt = 0;
while (j < len && s[j] != ' ') j++;
printf("%d", j - i);
i = j - 1;
}
return 0;
}
代码02
1143:最长最短单词¶
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin, s);
int maxn = -1, minn = 110;
string maxt, mint;
for (int i = 0, len = s.size(); i < len; i++) {
if (s[i] == ' ' || s[i] == ',') continue;
string t;
int j = i;
while (j < len && s[j] != ' ' && s[j] != ',') t += s[j++];
if (j - i > maxn) maxn = j - i, maxt = t;
if (j - i < minn) minn = j - i, mint = t;
// j = i
i = j;
}
cout << maxt << endl << mint << endl;
return 0;
}
1144:单词翻转¶
代码
#include <bits/stdc++.h>
using namespace std;
string s;
int main(){
getline(cin, s);
for (int i = 0, len = s.size(); i < len; i++){
if (s[i] == ' '){
cout << s[i];
continue;
}
int j = i;
string temp;
while (j < len && s[j] != ' ') temp += s[j++];
reverse(temp.begin(), temp.end());
cout << temp;
i = j - 1;
}
return 0;
}
1145:字符串p型编码¶
代码
1146:判断字符串是否为回文¶
代码
代码02
1147:最高分数的学生姓名¶
代码
1148:连续出现的字符¶
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
int k;
string s;
cin >> k >> s;
bool flag = false;
for (int i = 0, len = (int)s.size(); i < len; i++) {
int j = i;
while (j < len && s[j] == s[i]) j++;
int cnt = j - i;
if (cnt >= k) {
flag = true;
cout << s[i] << endl;
break;
}
}
if (!flag) cout << "No" << endl;
return 0;
}
1149:最长单词2¶
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin, s);
int maxn = -1;
string ans;
for (int i = 0, len = (int)s.size(); i < len - 1; i++) {
int j = i;
string temp;
while (j < len - 1 && s[j] != ' ') {
temp += s[j];
j++;
}
if (j - i > maxn) {
maxn = j - i;
ans = temp;
}
}
cout << ans << endl;
return 0;
}
代码02
#include<bits/stdc++.h>
using namespace std;
string s;
string ans;
int maxn;
int main(){
while (cin >> s){
if (s[s.size() - 1] == '.'){
if (s.size() - 1 > maxn){
maxn = s.size() - 1;
s.pop_back();
ans = s;
}
}else{
if (s.size() > maxn){
maxn = s.size();
ans = s;
}
}
}
cout << ans << '\n';
return 0;
}