8.16训练课
xiezirui1104 · · 学习·文化课
| 题目 | 分数 |
|---|---|
| T1 | 100 |
| T2 | 10 |
| T3 | 70 |
| T4 | 20 |
| T5 | 40 |
| T6 | 20 |
| T7 | 76 |
T2 T2
无思路
题意:
一个
思路:
先用二维数组存
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[30][30];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int maxx = 0;
int minn = LLONG_MAX;
for(int i = 1 ; i <= 5 ; i++){
for(int j = 1 ; j <= 5 ; j++){
cin >> a[i][j];
minn = min(minn , a[i][j]);
maxx = max(maxx , a[j][i]);
}
}
int f = 0;
for(int i = 1 ; i <= 5 ; i++){
for(int j = 1 ; j <= 5 ; j++){
if(a[i][j] == maxx && a[i][j] == minn){
f = 1;
cout << a[i][j];
}
}
}
if(f == 0){
cout << "not found";
}
return 0;
}
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[10][10];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int maxx = 0;
for(int i = 1 ; i <= 5 ; i++){
for(int j = 1 ; j <= 5 ; j++){
cin >> a[i][j];
}
}
for(int h = 1 ; h <= 5 ; h++){
int maxx = 0,lie = 0;//去寻找一下列
for(int l = 1 ; l <= 5 ; l++){
if(a[h][l] >= maxx){//打擂台,找这一行的最大值
maxx = a[h][l];
lie = l;//最大值的那一个列就是
}
}
int minn = 1e6;
for(int i = 1 ; i <= 5 ; i++){
if(a[i][lie] < minn){//判断这一列的最小值
minn = a[i][lie];
}
}
if(maxx == minn){//如果为同一个数字
cout << h << " " << lie << " "<< maxx;
return 0;//题目保证,只有一个鞍点
}
}
cout << "not found";//没有结束代码就输出
return 0;
}
错因:
没有分开去找最大值的那一列,方法用的也是错误的
T3 T3
有思路
题意:
有
思路:
用一个虽然是超级桶罢了
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[100005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
}
int m;
cin >> m;
for(int j = 1 ; j <= m ; j++){
int pz,f = 0;
cin >> pz;
for(int i = 1 ; i <= n ; i++){
if(a[i] == pz){
f = 1;
cout << i << "\n";
}
}
if(f == 0){
cout << 0 << "\n";
}
}
return 0;
}
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<int,int>a;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
int x;
cin >> x;
a[x] = i;//用瓶子个数当下标,存入瓶子是第几行
}
int l;
cin >> l;
for(int i = 1 ; i <= l ; i++){
int m;
cin >> m;
cout << a[m] << "\n";//直接调取
}
return 0;
}
错因:
没有用
T4 [蓝桥杯青少年组省赛 2024] 出现奇数次的数
有思路
题意:
很简单,有
思路:
直接用桶,当然超时且报错,因为数可能是一个
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e8 + 5;
int tong[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
int x;
cin >> x;
tong[x]++;
if(tong[x] % 2 != 0 && tong[i] != 1){
cout << x;
break;
}
}
return 0;
}
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<int,int>ma;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
int x;
cin >> x;
ma[x]++;//桶
}
for(auto i:ma){
if(i.second % 2 == 1){//这个值是不是奇数
cout << i.first;//输出下标,数字存的是下标
return 0;
}
}
return 0;
}
错因:
没有用
T5 [常州市赛 2020] 重点单词
有思路
题意:
有
思路:
只要这个单词不跟后一个单词一样,就保存这个字母,最后压缩过来的再用
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
string tong[100005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
int ans = 0;
string s;
cin >> s;
string t = "";
for(int j = 0 ; j < s.size() ; j++){
if(s[j] != s[j-1]){
t += s[j];
}
}
cout << t << "\n";
}
return 0;
}
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<string,int>ma;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
string ans = "";
cin >> n;
for(int i = 1 ; i <= n ; i++){
string s;
cin >> s;
string t = "";
for(int j = 0 ; j < s.size() ; j++){
if(s[j] != s[j+1]){//不跟后一个一样就保存
t += s[j];
}
}
ma[t]++;
}
int maxx = 0;
for(auto i:ma){
if(i.second >= maxx){//打擂台
maxx = i.second;
ans = i.first;
}
}
cout << ans;
return 0;
}
错因:
没有用
T6 T6
无思路
题意:
有
思路:
一开始用的是结构体排序的方法,但是错了,所以用了双
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
struct st{
string xm;
int fs;
}a[1005];
int cmp(st s1,st s2){
return s2.fs < s1.fs;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> a[i].xm >> a[i].fs;
}
sort(a + 1 , a + n + 1,cmp);
for(int i = 1 ; i <= n ; i++){
cout << a[i].xm << " ";
}
return 0;
}
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<string,int>m1;//这个map存分数
map<int,string>m2;//这个map按分数存名字
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
string s;
int x;
cin >> s >> x;
m1[s] += x;//这个名字下标的值加上分数
}
for(auto i:m1){
m2[i.second]=i.first;//交换值
}
for(auto i:m2){
cout << i.second << " ";
}
return 0;
}
错因:
没有去用双
T7 A-B 数对
有思路
题意:
有
思路:
经过数学可知
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[200005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,c;
cin >> n >> c;
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
}
int cnt;
for(int i = 1 ; i <= n ; i++){
for(int j = i + 1 ; j <= n ; j++){
if(a[i] - a[j] == c || a[j] - a[i] == c){
cnt++;
}
}
}
cout << cnt;
return 0;
}
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<int,int>ma;
int a[200005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,c;
cin >> n >> c;
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
int b = a[i] - c;//使用公式求出B
ma[b]++;
}
int cnt = 0;
for(int i = 1 ; i <= n ; i++){
cnt += ma[a[i]];//假判断,如果这个没有结果,map会自动开0
}
cout << cnt;
return 0;
}
错因:
没有用
拜拜!感谢观看!