团队程序设计天梯赛-3.17字符串与数组基础排位赛总结 一时失言乱红尘 2023-10-03 18:34 45阅读 0赞 #### 文章目录 #### * 7-1 Time Difference (10 分) * * 题目 * 思路 * 代码 * 7-2 念数字 (10 分) * * 题目 * 思路 * 代码 * 7-3 String Builder (10 分) * * 题目 * 思路 * 代码 * 7-4 空心字母金字塔 (10 分) * * 题目 * 思路 * 知识点 * 代码 * 7-5 Wifi密码 (10 分) * * 题目 * 思路 * 代码一 * 代码二 * 7-6 科学计数法的值 (15 分) * * 题目 * 注意点 * 知识点 * 代码 * 7-7 PAT排名汇总 (20 分) * * 题目 * 知识点 * 代码 ## 7-1 Time Difference (10 分) ## ### 题目 ### What is the time difference between 10:30 and 11:45? Your program reads two time spots and prints the time difference between them, in terms of hours and minutes. Input Format Two time spots, in 24-hour, each is represented as two numbers, as “hour minute”. The second time spot is later than the first and both are within the same day. Output Format Two numbers represent the time difference. The first is the hours in the difference, while the second is the minutes. Sample Input 10 30 11 45 Sample Output 1 15 ### 思路 ### 转化为分钟后计算 ### 代码 ### #include<bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int res = c*60+d - (a*60+b); cout << res/60 << " " << res%60; } ## 7-2 念数字 (10 分) ## ### 题目 ### 输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出fu字。十个数字对应的拼音如下: 0: ling 1: yi 2: er 3: san 4: si 5: wu 6: liu 7: qi 8: ba 9: jiu 输入格式: 输入在一行中给出一个整数,如:1234。 提示:整数包括负数、零和正数。 输出格式: 在一行中输出这个整数对应的拼音,每个数字的拼音之间用空格分开,行末没有最后的空格。如 yi er san si。 输入样例: -600 输出样例: fu liu ling ling ### 思路 ### 按数字取对应下标即可 ### 代码 ### #include<bits/stdc++.h> using namespace std; int main() { string s[] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" }; string t; cin >> t; int i = 0; if(t[0]=='-') { cout << "fu "; i = 1; } for(; i<t.size(); i++) { cout << s[t[i]-48]; if(i!=t.size()-1) cout << " "; } return 0; } ## 7-3 String Builder (10 分) ## ### 题目 ### You are going to read four numbers: n, a, b and c, like this: 12 2 5 3 First, n is used to build up a string from 0 to n, like this: 0123456789101112 is a string build up for n=12. Then, in all the digits from index a to index b, count the appearence of c. For the string above, 2 5 is: 2345 Thus the appearence of 3 is 1. Input Format: Four positive numbers, n, a, b and c, where a<b<n<10000, and 0<=c<=9… Output Format: One number represnets the length of the generated string. One number represents the apprence of c. There is a space between the two numbers. Sample Input: 12 2 5 3 Sample Output: 16 1 ### 思路 ### 将数字to\_string后加到字符串后面即可,然后按每位进行比较 ### 代码 ### #include<bits/stdc++.h> using namespace std; int main() { int n, a, b; char c; cin >> n >> a >> b >> c; string s = ""; for(int i=0; i<=n; i++) s += to_string(i); int cnt = 0; for(int i=a; i<=b; i++) if(s[i]==c) cnt++; cout << s.size() << " " << cnt; return 0; } ## 7-4 空心字母金字塔 (10 分) ## ### 题目 ### 输入一个大写的英文字母,输出空心的字母金字塔。 输入格式: 一个大写英文字母。 输出格式: 一个空心的大写英文字母金字塔,其中第1层的“A”在第1行的第40列,列从1开始计数。 输入样例: E 输出样例: A B B C C D D EEEEEEEEE ### 思路 ### 将每行分为“左边空格+当前字符+中间空格+当前字符+换行”进行处理,注意第一行和最后一行要特殊处理 ### 知识点 ### string(int num,char c):用num个c字符组成字符串 ### 代码 ### #include<bits/stdc++.h> using namespace std; int main() { char x; cin >> x; char now = 'A'; int left = 39, mid = 0;//左边和中间的空格数 while(now!=x) { cout << string(left, ' ') << now;//左边空格+字符 if(mid>0) cout << string(mid, ' ') << now;//中间空格+字符 cout << endl; now++; left--; if(mid==0) mid = 1; else mid += 2; } cout << string(left, ' ') << now; if(mid>0) cout << string(mid, now) << now; cout << endl; return 0; } ## 7-5 Wifi密码 (10 分) ## ### 题目 ### 下面是微博上流传的一张照片:“各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1;B-2;C-3;D-4;请同学们自己作答,每两日一换。谢谢合作!!~”—— 老师们为了促进学生学习也是拼了…… 本题就要求你写程序把一系列题目的答案按照卷子上给出的对应关系翻译成 wifi 的密码。这里简单假设每道选择题都有 4 个选项,有且只有 1 个正确答案。 ![在这里插入图片描述][watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBAR0NUVFRUVFQ_size_19_color_FFFFFF_t_70_g_se_x_16] 输入格式: 输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行按照 编号-答案 的格式给出一道题的 4 个选项,T 表示正确选项,F 表示错误选项。选项间用空格分隔。 输出格式: 在一行中输出 wifi 密码。 输入样例: 8 A-T B-F C-F D-F C-T B-F A-F D-F A-F D-F C-F B-T B-T A-F C-F D-F B-F D-T A-F C-F A-T C-F B-F D-F D-T B-F C-F A-F C-T A-F B-F D-F 输出样例: 13224143 ### 思路 ### * 代码一思路:使用getline()将每行当作一个字符串进行输入(注意前面要getchar()),然后用find()找出T所在的位置,该位置坐标-2就是对应的字母所在位置 * 使用双重循环接收每题的每个选项的情况,如果对应答案为T则使用“s\[0\]-‘A’+1”输出其对应的数 ### 代码一 ### #include<iostream> #include<string> #include<algorithm> using namespace std; int main() { int n; cin >> n; getchar();//注意这个要有,因为后面有getline string res = ""; for(int i=0; i<n; i++) { string s; getline(cin, s); int pos = s.find('T'); cout << s[pos-2]-'A'+1; } return 0; } ### 代码二 ### #include<iostream> #include<string> #include<algorithm> using namespace std; int main() { int n; cin >> n; for(int i=0; i<n; i++) { string s; for(int j=0; j<4; j++) { cin >> s; if(s[2]=='T') cout << s[0]-'A'+1; } } return 0; } ## 7-6 科学计数法的值 (15 分) ## ### 题目 ### 科学计数法是一种数学专用术语。将一个数表示成 a×10的n次幂的形式,其中1≤|a|<10,n为整数,这种记数方法叫科学计数法。例如920000可以表示为9.2\*10^5 现在需要对输入的字符串进行分离,自动识别该科学计数法中的a和幂次,计算其表征的具体数值并输出该值。 例如,对于输入的复数字符串“9.210^5”,输出 The actual value for 9.210^5 is 920000 注意: 1、每组测试数据仅包括一个用于科学计数法的字符串。 2、输入字符串保证合法。 3、字符串长度不超过1000 4、幂次不超过200 输入示例: 9.2*10^5 输出示例: The actual value for 9.2*10^5 is 920000 ### 注意点 ### * 底数可能为负数 * 底数为小数时小数点在整数位的后一位(因为底数不超过十) * 先将底数的负号和小数点去掉再处理 * 指数分为大于0和小于0的情况 * 指数大于0时又分两种情况,第一种是指数大于底数后的位数,则直接在处理好后的底数字符串(无负号(负号有的话会单独输出)和小数点)后直,接加对应数字个0;第二种是指数小于底数后的位数则需算出小数点会在哪个位置,给处理好后的字符串补上小数点即可 * 小于0时在前面补“0.”后补对应数字个0后输出处理好的字符串 ### 知识点 ### s.substr():取字符串中的子串 s.find() s.erase() stoi() 使用printf()可能用到的:c\_str() [C++ string类详解][C_ string] ### 代码 ### #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; string old = s; string res = ""; if (s[0] == '-') res = "-", s = s.substr(1);// 先处理负号 int pos = s.find('*'); //double d = stod(s.substr(0, pos)); string t = s.substr(0, pos); if (t[1]=='.') t.erase(1, 1);//去掉t的小数点 pos = s.find('^'); int x = stoi(s.substr(pos + 1));//指数 if (x>=0) { if (t.size() > x + 1) res += t.substr(0, x + 1) + "." + t.substr(x+1); else res += t + string(x - t.size() + 1, '0'); } else { res += "0." + string(-x-1, '0') + t; } printf("The actual value for %s is %s", old.c_str(), res.c_str()); } ## 7-7 PAT排名汇总 (20 分) ## ### 题目 ### 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科学的评价计算机程序设计人才,为企业选拔人才提供参考标准(网址http://www.patest.cn)。 每次考试会在若干个不同的考点同时举行,每个考点用局域网,产生本考点的成绩。考试结束后,各个考点的成绩将即刻汇总成一张总的排名表。 现在就请你写一个程序自动归并各个考点的成绩并生成总排名表。 输入格式: 输入的第一行给出一个正整数N(≤100),代表考点总数。随后给出N个考点的成绩,格式为:首先一行给出正整数K(≤300),代表该考点的考生总数;随后K行,每行给出1个考生的信息,包括考号(由13位整数字组成)和得分(为\[0,100\]区间内的整数),中间用空格分隔。 输出格式: 首先在第一行里输出考生总数。随后输出汇总的排名表,每个考生的信息占一行,顺序为:考号、最终排名、考点编号、在该考点的排名。其中考点按输入给出的顺序从1到N编号。考生的输出须按最终排名的非递减顺序输出,获得相同分数的考生应有相同名次,并按考号的递增顺序输出。 输入样例: 2 5 1234567890001 95 1234567890005 100 1234567890003 95 1234567890002 77 1234567890004 85 4 1234567890013 65 1234567890011 25 1234567890014 100 1234567890012 85 输出样例: 9 1234567890005 1 1 1 1234567890014 1 2 1 1234567890001 3 1 2 1234567890003 3 1 2 1234567890004 5 1 4 1234567890012 5 2 2 1234567890002 7 1 5 1234567890013 8 2 3 1234567890011 9 2 4 ### 知识点 ### * 使用一个外部的cnt来记录每个考点的开始坐标!! * sort排序范围是左闭右开的!! * 对Rank函数设flag参数,为1时为外部总排名,为0时为对应考点内部排名 * 对于处理有相同成绩且需并列的情况,当前排名用==“now = i-l+1”==表示 * prinf(%s,…)printf使用%s格式化输出字符串时需使用.c\_str()函数 ### 代码 ### #include<bits/stdc++.h> using namespace std; struct node { string id; int s; int pos;//考点编号 int in;//内部排名 int out;//外部排名 }; bool cmp(node x, node y) { if(x.s==y.s) return x.id<y.id; else return x.s>y.s; } void Rank(node a[], int l, int r, int flag) { //对a[l:r) 赋予名次 , flag为0表示赋予考点内名次 int now = 1; for(int i=l; i<r; i++) { if(i==l || a[i].s==a[i-1].s) { if(flag==0) a[i].in = now; else a[i].out = now; } else { now = i-l+1; if(flag==0) a[i].in = now; else a[i].out = now; } } } int main() { int n; cin >> n; node a[30000]; int cnt = 0; for(int i=1; i<=n; i++) { int k; cin >> k; for(int j=0; j<k; j++) { cin >> a[cnt+j].id >> a[cnt+j].s; a[cnt+j].pos = i; //考点号 } sort(a+cnt, a+cnt+k, cmp);//考点内排序 Rank(a, cnt, cnt+k, 0);//考点内赋予名次 cnt += k; } sort(a, a+cnt, cmp);//全部人排序 Rank(a, 0, cnt, 1);//全部人赋予名次 cout << cnt << endl; for(int i=0; i<cnt; i++) printf("%s %d %d %d\n", a[i].id.c_str(), a[i].out, a[i].pos, a[i].in); } [watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBAR0NUVFRUVFQ_size_19_color_FFFFFF_t_70_g_se_x_16]: https://img-blog.csdnimg.cn/ad9a92ba455c4b2ab283fe73dff92c76.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR0NUVFRUVFQ=,size_19,color_FFFFFF,t_70,g_se,x_16 [C_ string]: http://c.biancheng.net/cpp/biancheng/view/3284.html
相关 团队程序设计天梯赛-4.9排位赛总结 文章目录 7-1 谁是赢家 (5 分) 题目描述 代码 7-2 有理数比较 (5 分) 题目描述 注意点 浅浅的花香味﹌/ 2023年10月03日 18:40/ 0 赞/ 46 阅读
相关 团队程序设计天梯赛-3.31排位赛总结 文章目录 7-1 逆序的三位数 (10 分) 题目描述 知识点 代码 7-2 九宫格输入法 (10 分) 港控/mmm°/ 2023年10月03日 18:38/ 0 赞/ 44 阅读
相关 团队程序设计天梯赛-3.19排位赛总结 文章目录 7-1 冠军魔术 (10 分) 题目 知识点 代码 7-2 单词长度 (10 分) 题目 水深无声/ 2023年10月03日 18:37/ 0 赞/ 50 阅读
相关 团队程序设计天梯赛-3.17字符串与数组基础排位赛总结 文章目录 7-1 Time Difference (10 分) 题目 思路 代码 7-2 念数字 (10 分) 一时失言乱红尘/ 2023年10月03日 18:34/ 0 赞/ 46 阅读
相关 团队程序设计天梯赛-3.12排位赛总结 文章目录 7-1 12-24小时制 (10 分) 题目 思路 代码 7-2 有理数加法 (10 分) 题目 淡淡的烟草味﹌/ 2023年10月03日 18:32/ 0 赞/ 46 阅读
相关 团队程序设计天梯赛-3.3排位赛总结 文章目录 7-1 心理阴影面积 (5 分) 题目 代码 7-2 世÷我=佬 (5 分) 题目 注意点 我会带着你远行/ 2023年10月03日 18:26/ 0 赞/ 48 阅读
相关 团队程序设计天梯赛-寒假训练1总结 文章目录 7-6 情人节 (15 分) 题目 知识点 代码 7-7 吃火锅 (15 分) 题目 灰太狼/ 2023年10月03日 18:26/ 0 赞/ 42 阅读
相关 团队程序设计天梯赛-1.21排位赛总结 文章目录 7-5 一帮一 (15 分) 题目 思路 知识点 代码 7-6 考试座位号 (15 分) 小灰灰/ 2023年10月03日 18:24/ 0 赞/ 86 阅读
相关 团队程序设计天梯赛-2.24排位赛总结 文章目录 7-3 就不告诉你 (10 分) 题目: 知识点 代码: 7-4 一元多项式求导 (10 分) 今天药忘吃喽~/ 2023年10月03日 18:23/ 0 赞/ 104 阅读
相关 PAT团队程序设计天梯赛L1-035 情人节 题目要求 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0 £神魔★判官ぃ/ 2022年04月03日 16:52/ 0 赞/ 322 阅读
还没有评论,来说两句吧...