2026.3.15oj总结
1.据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文。你的工作就是去这些牛制造的奇观(最棒的回文)。在寻找回文时不用理睬那些标点符号、空格(但应该保留下来以便做为答案输出),只用考虑字母A-Z和a-z。要你寻找的最长的回文的文章是一个不超过20,000个字符的字符串。我们将保证最长的回文不会超过2,000个字符(在除去标点符号、空格之前)。总结读取输入可能会有换行输入cin.get(c)逐个字符读取。规范处理把原来的字符串全部转化为小写字符串进行回文判断最后输出的起始位置输出的是原来的字符串所以原来的字符串得先存一个副本判断出回文时需要存储规范字符串的位置最后输出的起始位置注意有可能是符号得先进行判断。#includeiostream#includealgorithm#include vector#includestringusing namespace std;bool isHuiwen(string s) {string s1s;reverse(s1.begin(), s1.end());return s s1;}int main() {string s;char c;while (cin.get(c)) {s s c;}string s1 s;vectorstring res;for (int i 0;i s1.length();i) {//大写全部转换小写if (s1[i] A s1[i] Z){s1[i] s1[i] 32;}}int maxlen 0;int maxbegin 0;int maxend 0;for (int i 0;i s1.length();i) {for (int j i;j s1.length();j) {string s2;for (int k i;k j;k) {if (s1[k] a s1[k] z) {s2 s2 s1[k];//提取小写字母}}if (s2.length() 0 isHuiwen(s2)) {if (s2.length() maxlen) {maxlen s2.length();maxbegin i;maxend j;}}}}cout maxlen endl;int start maxbegin;while (start maxend !isalpha(s[start])){start;}int end maxend;while (end start !isalpha(s[end])) {end--;}for (int i start;i end;i) {cout s[i];}cout endl2.大整数相加问题描述I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A B.总结进位相加从后往前进行进位相加相加出来的字符串是反的得翻转#includeiostream#includealgorithmusing namespace std;string Sum(string s1, string s2) {string res;int i s1.length() - 1;//进位从后面开始int j s2.length() - 1;int carry 0;//进位while (i 0 || j 0||carry0) {int sum carry;if (i 0) {sum sum (s1[i] - 0);//字符转为数字i--;//向前相加}if (j 0) {sum sum (s2[j] - 0);j--;}carry sum / 10;res.push_back(sum % 10 0);//数字转化为字符}reverse(res.begin(), res.end());return res;}int main() {int N;cin N;int a 1;bool firstcase true;while (N--) {if (!firstcase) {cout endl;}firstcase false;string s1,s2;cin s1 s2;string sum Sum(s1, s2);cout Case a : endl;cout s1 s2 sum endl;a;}}3.16进制加法问题描述某天、小晨在路上背着单词突遇一外星人外星人对小晨很感兴趣为了考验小晨的智商就向小晨提问简单加法由于外星人使用16进制所以小晨必须用16进制回答。#includeiostream#includevector#includestringusing namespace std;int ShiliutoShi(string s) {int decimal 0;for (int i 0;i s.length();i) {//先把十六进制的字符都转换为数字。char c s[i];int digit 0;if (c 0 c 9) {digit c - 0;}else if (c a c f) {digit c - a 10;}decimal decimal * 16 digit;}return decimal;}string ShitoShiliu(int n) {string digit ;if (n 0) return 0;while (n 0) {int remainder n % 16;if (remainder 10) {digit char(0 remainder) digit;}else {digit char(a remainder - 10) digit;}n n / 16;}return digit;}int main() {int T;cin T;while (T--) {string s1, s2;cin s1 s2;int sum ShiliutoShi(s1) ShiliutoShi(s2);string s ShitoShiliu(sum);cout s endl;}}4.纯粹素数跟据一个正整数n求出从1,000开始从小到大的第n个纯粹素数。总结#includeiostream#includevector#includestring#includecmathusing namespace std;bool isZhishu(int n) {if (n 2)return false;if (n 2)return true;if (n % 2 0) return false;for (int i 3;i sqrt(n);i) {if (n % i 0) return false;}return true;}int removeHig(int n) {int total 0;//位数int start n;while (n 0) {//统计位数total;n n / 10;}int weight pow(10, total - 1);return start % weight;}bool isPure(int n) {int current n;while (current 0) {if (!isZhishu(current)) {return false;}currentremoveHig(current);}return true;}int main() {int n;vectorint arr;while (cin n) {for (int i 1000;i 100000;i) {if (isPure(i)) {arr.push_back(i);}}cout arr[n-1] endl;}}5.考试排名题描述C编程考试使用的实时提交系统具有即时获得成绩排名的特点。它的功能是怎么实现的呢我们做好了题目的解答提交之后要么“AC”要么错误不管怎样错法总是给你记上一笔表明你曾经有过一次错误提交因而当你一旦提交该题“AC”后就要与你算一算帐了总共该题错误提交了几回。虽然你在题数上大步地跃上了一个台阶但是在耗时上要摊上你共花去的时间。特别是曾经有过的错误提交每次都要摊上一定的单位时间分。这样一来你在做出的题数上可能领先别人很多但是在做出同样题数的人群中你可能会在耗时上处于排名的劣势。例如某次考试一共8题ABCDEFGH每个人做的题都在对应的题号下有个数量标记负数表示该学生在该题上有过的错误提交次数但到现在还没有AC正数表示AC所耗的时间如果正数a跟上一对括号里面有个整数b那就表示该学生提交该题AC了耗去了时间a同时曾经错误提交了b次如果b为0则没有括号及b因此对于下述输入数据若每次错误提交的罚分为20分即每错误提交一次在总耗时时增加20分钟则其排名从高到低应该是这样的Josephus 5 376John 4 284Alice 4 352Smith 3 167Bob 2 325Bush 0 0总结#includeiostream#includestring#includealgorithm#includevector#include iomanipusing namespace std;struct Student {string name;int t;int time;};bool compare(Student a, Student b) {if (a.t !b.t) {return a.t b.t;}if (a.time ! b.time) {return a.time b.time;}return a.name b.name;}int main() {int n, m;cin n m;vectorStudent students;string name;while (cin name) {Student s;s.name name;s.t 0;s.time 0;for (int i 0;i n;i) {string status;cin status;if (status 0) {continue;}int pos status.find(();if (pos -1) {//没有括号int num stoi(status);if (num 0) {//此时是整数表示AC了s.t;s.time s.time num;}//负数表示还没有AC不算入}//有括号else{int time stoi(status.substr(0, pos));int wrong stoi(status.substr(pos 1, status.length() - pos - 2));s.t;s.time s.time time wrong * m;}}students.push_back(s);}sort(students.begin(),students.end(),compare);for (int i 0;i students.size();i) {cout left setw(10) students[i].name right setw(2) students[i].t right setw(4) students[i].time endl;}}6.统计候选人的票数问题描述设有3个候选人zhang、li、wang候选人姓名不区分大小写10个选民选民每次输入一个得票的候选人的名字若选民输错候选人姓名则按废票处理。选民投票结束后程序自动显示各候选人的得票结果和废票信息。要求用结构体数组candidate表示3个候选人的姓名和得票结果。总结统一为小写统计唱票统计每次遇到一个人的就看到和candinates[j].的哪一个对应如果有票数加1没有错票加1结构体初始化这样可以保证candinates[0].namecandinates[0].num和题目是相对应的#includeiostream#includestringusing namespace std;struct Candinate {string name;int num;};string rever(string s) {for (int i 0;i s.length();i) {if (s[i] A s[i] Z) {s[i] s[i] 32;}}return s;}int main() {Candinate candinates[3] {{li,0},{zhang,0},{wang,0}};int wrong 0;for (int i 0;i 10;i) {string ticket;cin ticket;ticket rever(ticket);bool valid false;for (int j 0;j 3;j) {if (ticket candinates[j].name){candinates[j].num;valid true;}}if (!valid) {wrong;}}for (int i 0;i 3;i) {cout candinates[i].name : candinates[i].num endl;}cout Wrong election: wrong;}7.密码分析问题描述密码分析学中常常需要统计字符出现的频度。给定若干行短文要求按字符出现的频度由高到低输出当两个字符出现的频度相同时按字符大小的顺序输出。注意只需要统计英文字母的频度非英文字母一律忽略。#includeiostream#includestring#includevector#include algorithmusing namespace std;struct Candinate {string name;int num;};struct alaph {char c;int num;};string rever(string s) {for (int i 0;i s.length();i) {if (s[i] a s[i] z) {s[i] s[i] -32;}}return s;}bool cmp(alaph a, alaph b) {if (a.num ! b.num) {return a.num b.num;}return a.c b.c;}int main() {string s;bool firstcase true;while (getline(cin, s)) {if (!firstcase){cout endl;}firstcase false;s rever(s);vectoralaph arr(26);for (int i 0;i 26;i) {arr[i].c A i;//把AB....存入arr中间并且把次数都置为0arr[i].num 0;}for (int i 0;i s.length();i) {if (s[i] A s[i] Z) {int index s[i] - A;arr[index].num;}}sort(arr.begin(), arr.end(), cmp);for (int i 0;i arr.size();i) {if (arr[i].num 0) {cout arr[i].c arr[i].num endl;}}}}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2416146.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!