例题记录
Together
题目解析
输入n个数,你可以将这些数分别+1,-1或者保持不变,尽可能多的将这些数变成同一个数x,输出x的个数。
算法思路
每个数都有3种情况,那么只需要将所有情况得到的数,每一个的个数存储到一个数组中,然后找出数量最多的那个数有多少个就行了。因为我们只考虑个数而不考虑具体是哪个数字,为了避免越界,可以把这三个操作改成:不变,+1,+2。只要在定义数组的时候多给两个位置就行了。
代码实现
python
n = int(input())
nums = list(map(int, input().split()))
ha=[0]*100002
for i in nums:ha[i]+=1ha[i+1]+=1ha[i+2]+=1
print(max(ha))
C++
#include <bits/stdc++.h>
using namespace std;
int main(){int n;cin>>n;vector<int> ha(100002,0);for(int i=0;i<n;i++){int x;cin>>x;ha[x]++;ha[x+1]++;ha[x+2]++;}cout<<*max_element(ha.begin(),ha.end());
}
刽子手游戏
题目解析
这一题看似简单其实有很多坑,我也被卡了好久才ac。首先题目的意思是,输入回合数,一个答案单词,和一个猜测单词,如果猜测的单词里存在答案单词里的所有字母则判定为赢,如果有一个字母是答案单词中没有的,那么记猜错一次,一旦猜错7次判定为输,如果猜对了一部分判定不输不赢。有一个很难注意到的坑是,猜测单词是从前往后一个个字母开始判定,一旦判定了输或赢直接结束,例如答案单词是abc,猜测单词是fffffffabc,虽然猜测单词里包含了答案单词里的所有字母,但是猜测单词里的前7个字母浪费了7次猜错的机会,则判定为输。
算法思路
输入回合数n,答案单词x1,猜测单词x2,用一个长度26,初始值为0的数组arr记录下答案单词中每个字母的出现次数。变量rest记录还剩下需要猜测的字母个数,当rest==0则判定为赢。变量chance记录剩余猜错的机会,当chance==0则判定为输,其余情况为不输不赢。从头往后遍历猜测单词x2中的每个字母,有下面三种情况
如果rest或chance其中一者为0说明已经判断出了这一轮的结果,那么结束遍历。
如果arr这个字母对应位置上的数字不是0或者-1,说明这个字母在答案单词x1中存在,更新rest的值为减去答案单词x1中这个字母的所有个数,然后修改arr中这个字母位置对应位置上的数字为-1,这样可以跳过之后在猜测单词x2中再次出现改字母的情况。
如果arr这个字母对应位置上的数字是0,说明这个字母在答案单词x1中不存在,chance的值减1。
代码实现
#include <bits/stdc++.h>
using namespace std;
int main()
{int n = 0;while (1){int chance = 7;cin >> n;if (n == -1)break;string x1, x2;cin >> x1 >> x2;int arr[26] = {0};int rest = x1.length();for (int i = 0; i < x1.length(); i++){arr[x1[i] - 'a']++;}for (int i = 0; i < x2.length(); i++){if (rest == 0 || chance == 0)break;if (arr[x2[i] - 'a'] != -1 && arr[x2[i] - 'a'] != 0){rest -= arr[x2[i] - 'a'];arr[x2[i] - 'a'] = -1;}else if (arr[x2[i] - 'a'] == 0)chance--;}cout << "Round " << n << endl;if (rest == 0)cout << "You win." << endl;else if (chance == 0)cout << "You lose." << endl;elsecout << "You chickened out." << endl;}return 0;
}
古老的密码
题目解析
每次读取两个字符串,判断其中一个字符串重新排序后能否通过一定的字母映射得到新的字符串,并使新的字符串与另一个字符串相等,如果可以输出“YES”,否则输出"NO"。
算法思路
打乱过后的字符串如果能够满足每一个字符与原字符串一一对应那就是对的,这么一来只需要分别用一个长度26的数组记录下两个字符串中每个字母的出现次数,再分别将这两个数组排序,如果排序过后的两个数组一模一样,就输出“YES”,否则输出“NO”。可以采用equal函数用来比较两个长度相等的数组是否一模一样,用法如下
- equal(first1,last1,first2):
- 如果迭代器区间[first1,last1)和迭代器区间[first2,first2+(last1 - first1))上的元素相等返回true,否则返回false。
代码实现
#include <bits/stdc++.h>
using namespace std;
int main(){string s1,s2;while(cin>>s1>>s2){int cnt[26]={0};int cnt1[26]={0};for(int i=0;i<s1.length();i++){cnt[s1[i]-'A']++;cnt1[s2[i]-'A']++;}sort(cnt+0,cnt+26);sort(cnt1+0,cnt1+26);int flag=0;if (equal(cnt, cnt + 26, cnt1)) cout << "YES" << endl;else cout << "NO" << endl;}
}
回文质数
题目解析
在输入的范围内寻找质数回文数,注意是闭区间。
算法思路
这道题我一开始是用python写的,直接在整个范围内遍历每个数,先判断是不是质数,再转成字符串通过if str==str[::-1]判断是不是回文,超时也是意料之内。之后用了打表的方法过的,就是现在自己的编译器里把5到1亿里面所有的回文质数跑出来,放到一个数组里,然后从头遍历,如果在输入的范围内就输出。之后我拿C++又做了一遍,做出了以下优化。写了两个函数,一个是优化版的质数判断函数,一个是用数学方法计算判断回文的函数,在遍历的时候也做了优化,由于回文质数不可能是偶数,所以可以直接少遍历一半,这样就过啦。
代码实现
python打表
ans=[0,2,3,5,7,11,101,131,151,181,191,313,353,373,383,727,757,787,797,919,929,10301,10501,10601,11311,11411,12421,12721,12821,13331,13831,13931,14341,14741,15451,15551,16061,16361,16561,16661,17471,17971,18181,18481,19391,19891,19991,30103,30203,30403,30703,30803,31013,31513,32323,32423,33533,34543,34843,35053,35153,35353,35753,36263,36563,37273,37573,38083,38183,38783,39293,70207,70507,70607,71317,71917,72227,72727,73037,73237,73637,74047,74747,75557,76367,76667,77377,77477,77977,78487,78787,78887,79397,79697,79997,90709,91019,93139,93239,93739,94049,94349,94649,94849,94949,95959,96269,96469,96769,97379,97579,97879,98389,98689,1003001,1008001,1022201,1028201,1035301,1043401,1055501,1062601,1065601,1074701,1082801,1085801,1092901,1093901,1114111,1117111,1120211,1123211,1126211,1129211,1134311,1145411,1150511,1153511,1160611,1163611,1175711,1177711,1178711,1180811,1183811,1186811,1190911,1193911,1196911,1201021,1208021,1212121,1215121,1218121,1221221,1235321,1242421,1243421,1245421,1250521,1253521,1257521,1262621,1268621,1273721,1276721,1278721,1280821,1281821,1286821,1287821,1300031,1303031,1311131,1317131,1327231,1328231,1333331,1335331,1338331,1343431,1360631,1362631,1363631,1371731,1374731,1390931,1407041,1409041,1411141,1412141,1422241,1437341,1444441,1447441,1452541,1456541,1461641,1463641,1464641,1469641,1486841,1489841,1490941,1496941,1508051,1513151,1520251,1532351,1535351,1542451,1548451,1550551,1551551,1556551,1557551,1565651,1572751,1579751,1580851,1583851,1589851,1594951,1597951,1598951,1600061,1609061,1611161,1616161,1628261,1630361,1633361,1640461,1643461,1646461,1654561,1657561,1658561,1660661,1670761,1684861,1685861,1688861,1695961,1703071,1707071,1712171,1714171,1730371,1734371,1737371,1748471,1755571,1761671,1764671,1777771,1793971,1802081,1805081,1820281,1823281,1824281,1826281,1829281,1831381,1832381,1842481,1851581,1853581,1856581,1865681,1876781,1878781,1879781,1880881,1881881,1883881,1884881,1895981,1903091,1908091,1909091,1917191,1924291,1930391,1936391,1941491,1951591,1952591,1957591,1958591,1963691,1968691,1969691,1970791,1976791,1981891,1982891,1984891,1987891,1988891,1993991,1995991,1998991,3001003,3002003,3007003,3016103,3026203,3064603,3065603,3072703,3073703,3075703,3083803,3089803,3091903,3095903,3103013,3106013,3127213,3135313,3140413,3155513,3158513,3160613,3166613,3181813,3187813,3193913,3196913,3198913,3211123,3212123,3218123,3222223,3223223,3228223,3233323,3236323,3241423,3245423,3252523,3256523,3258523,3260623,3267623,3272723,3283823,3285823,3286823,3288823,3291923,3293923,3304033,3305033,3307033,3310133,3315133,3319133,3321233,3329233,3331333,3337333,3343433,3353533,3362633,3364633,3365633,3368633,3380833,3391933,3392933,3400043,3411143,3417143,3424243,3425243,3427243,3439343,3441443,3443443,3444443,3447443,3449443,3452543,3460643,3466643,3470743,3479743,3485843,3487843,3503053,3515153,3517153,3528253,3541453,3553553,3558553,3563653,3569653,3586853,3589853,3590953,3591953,3594953,3601063,3607063,3618163,3621263,3627263,3635363,3643463,3646463,3670763,3673763,3680863,3689863,3698963,3708073,3709073,3716173,3717173,3721273,3722273,3728273,3732373,3743473,3746473,3762673,3763673,3765673,3768673,3769673,3773773,3774773,3781873,3784873,3792973,3793973,3799973,3804083,3806083,3812183,3814183,3826283,3829283,3836383,3842483,3853583,3858583,3863683,3864683,3867683,3869683,3871783,3878783,3893983,3899983,3913193,3916193,3918193,3924293,3927293,3931393,3938393,3942493,3946493,3948493,3964693,3970793,3983893,3991993,3994993,3997993,3998993,7014107,7035307,7036307,7041407,7046407,7057507,7065607,7069607,7073707,7079707,7082807,7084807,7087807,7093907,7096907,7100017,7114117,7115117,7118117,7129217,7134317,7136317,7141417,7145417,7155517,7156517,7158517,7159517,7177717,7190917,7194917,7215127,7226227,7246427,7249427,7250527,7256527,7257527,7261627,7267627,7276727,7278727,7291927,7300037,7302037,7310137,7314137,7324237,7327237,7347437,7352537,7354537,7362637,7365637,7381837,7388837,7392937,7401047,7403047,7409047,7415147,7434347,7436347,7439347,7452547,7461647,7466647,7472747,7475747,7485847,7486847,7489847,7493947,7507057,7508057,7518157,7519157,7521257,7527257,7540457,7562657,7564657,7576757,7586857,7592957,7594957,7600067,7611167,7619167,7622267,7630367,7632367,7644467,7654567,7662667,7665667,7666667,7668667,7669667,7674767,7681867,7690967,7693967,7696967,7715177,7718177,7722277,7729277,7733377,7742477,7747477,7750577,7758577,7764677,7772777,7774777,7778777,7782877,7783877,7791977,7794977,7807087,7819187,7820287,7821287,7831387,7832387,7838387,7843487,7850587,7856587,7865687,7867687,7868687,7873787,7884887,7891987,7897987,7913197,7916197,7930397,7933397,7935397,7938397,7941497,7943497,7949497,7957597,7958597,7960697,7977797,7984897,7985897,7987897,7996997,9002009,9015109,9024209,9037309,9042409,9043409,9045409,9046409,9049409,9067609,9073709,9076709,9078709,9091909,9095909,9103019,9109019,9110119,9127219,9128219,9136319,9149419,9169619,9173719,9174719,9179719,9185819,9196919,9199919,9200029,9209029,9212129,9217129,9222229,9223229,9230329,9231329,9255529,9269629,9271729,9277729,9280829,9286829,9289829,9318139,9320239,9324239,9329239,9332339,9338339,9351539,9357539,9375739,9384839,9397939,9400049,9414149,9419149,9433349,9439349,9440449,9446449,9451549,9470749,9477749,9492949,9493949,9495949,9504059,9514159,9526259,9529259,9547459,9556559,9558559,9561659,9577759,9583859,9585859,9586859,9601069,9602069,9604069,9610169,9620269,9624269,9626269,9632369,9634369,9645469,9650569,9657569,9670769,9686869,9700079,9709079,9711179,9714179,9724279,9727279,9732379,9733379,9743479,9749479,9752579,9754579,9758579,9762679,9770779,9776779,9779779,9781879,9782879,9787879,9788879,9795979,9801089,9807089,9809089,9817189,9818189,9820289,9822289,9836389,9837389,9845489,9852589,9871789,9888889,9889889,9896989,9902099,9907099,9908099,9916199,9918199,9919199,9921299,9923299,9926299,9927299,9931399,9932399,9935399,9938399,9957599,9965699,9978799,9980899,9981899,9989899]
a,b=map(int,input().split())
for i in range(1,782):if a<=ans[i]<=b:print(ans[i])
C++
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int v[N];bool is_prime(int n) {if (n <= 1) return false;if (n <= 3) return true;if (n % 2 == 0 || n % 3 == 0) return false;for (int i = 5; i * i <= n; i += 6) {if (n % i == 0 || n % (i + 2) == 0) return false;}return true;
}
int is_huiwen(int n){int sum=0,n1=n;while(n1){sum=sum*10+n1%10;n1/=10;}return sum==n;}int main(){int a,b;cin>>a>>b;int idx=1;if(a%2==0) a++;for(int i=a;i<=b;i+=2){//奇数的回文数if(is_huiwen(i)){if(is_prime(i)){cout<<i<<'\n';}}}return 0;}
高考组题
题目解析
在保证输入顺序的前提下,输出平均值最大的前两组数据的编号
算法思路
用结构体来记录每一组的编号以及平均值,然还用sort函数对结构体排序,值得注意的比较函数的写法。
代码实现
#include <bits/stdc++.h>
using namespace std;
struct data{double ave;int id;
};
bool cmp(data e1,data e2){//比较函数if(e1.ave!=e2.ave) return e1.ave>e2.ave;return e1.id<e2.id;
}
int main(){int n,k;cin>>n>>k;struct data gr[n];for(int i=0;i<n;i++){double sum=0;for(int j=0;j<k;j++){int x;cin>>x;sum+=x;}gr[i].id=i+1;gr[i].ave=sum/k;}sort(gr+0,gr+n,cmp);cout<<gr[0].id<<endl<<gr[1].id;
}
演唱会
题目解析
输入每个同学对n首歌分别的打分,最后选出分数高的前m首歌的编号做成歌单。如果那一条打分数据的索引和b相同,那么找出那一条打分数据中得分最高的歌曲编号,按照题意,如果这首歌在歌单中,那么把这首歌提到歌单最前,如果不在则把歌单最后一首改成这首歌。
算法思路
直接看注释吧
代码实现
#include <bits/stdc++.h>using namespace std;struct song{int id,v;//结构体内的id和v分别记录歌编号和总分数friend bool operator<(const song &a,const song &b){return a.v > b.v;}};int main(){ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);//缩时int n,m,a,b;cin >> n >> m >> a >> b;vector<song>s(n);//最终歌单s int id=-1,v=0;//id和v分别记录她喜爱的歌编号和分数for(int i = 0;i < a;i ++){for(int j = 0;j < n;j++){int x;cin >> x;s[j].v+=x,s[j].id = j + 1;//把这首歌的总分增加,给id赋值if(i+1 == b && x > v){//如果这行是她打的分,而且当前这首歌比已记录的最高分还要高v = x,id = j + 1;}}}sort(s.begin(),s.end());int ok = 0;for(int i = 0;i < m;i ++){//如果她喜爱的歌在歌单里ok变为1if(s[i].id == id){ok = 1;break;}}if(ok){//如果歌单里有她喜欢的歌cout << id <<" ";//先输出她这首for(int i =0 ;i <m;i++){//跳过歌单里原有的她喜欢的歌,按原顺序输出其他的歌if(s[i].id != id)cout << s[i].id <<" "; }}else{//如果歌单里没她喜欢的歌for(int i =0 ;i < m - 1;i++){//正常输出前面的cout << s[i].id <<" "; }cout << id;//最后一首改成她喜欢的歌}return 0;}