当前位置:   article > 正文

ZISUOJ 2023秋季综合训练(八)

zisuoj

说明

 博主是ZISU在校学生,刚学C/C++才2个多月,为了记录自己的学习过程和分享思路,故写一些博客。当然我的代码许多时候不是最优解,欢迎大家评论留言一起学习。如果有友友想提交这些题试试自己的思路啥的,可以私我,因为外校友友应该是登陆不进咱们的平台的。

题目列表

 问题 A: 字符串加密

思路:数据读进来遍历即可。

注意:考虑到字符串会存在空格,所以不能用cin或scanf来读

参考题解:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. //问题 A: 字符串加密
  5. int ans = 0;
  6. char s[10005];gets(s);
  7. int len = strlen(s);
  8. for(int i = 0;i<len;i++){
  9. if(s[i] >= 'a' && s[i] <= 'z') ans += s[i]-'a';
  10. else if(s[i] >= '0' && s[i] <= '9') ans += (s[i]-'0')*i;
  11. else ans++;
  12. }
  13. cout << ans;
  14. return 0;
  15. }

问题 B: k步排序

思路:题目只要求我们判断三种状态,并不是让我们用k步排序实际去做。因此,我们只需要判断数组num[i]与下标i同时对k求模是否相等。若所有数都相等,则输出0即可以直接k步排序;若有且仅有两个数不相等,则输出1即需要做一次预处理才能用k步排序实现效果;否则其他情况均输出-1即不能通过k步排序实现效果或做一次预处理后仍不能通过k步排序实现效果。

参考题解:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. //问题 B: k步排序
  5. int t;cin >> t;
  6. while(t--){
  7. int n,k,count = 0;
  8. cin >> n >> k;
  9. int num[20010];
  10. for(int i = 1;i<=n;i++){
  11. cin >> num[i];
  12. if(num[i]%k!=i%k) count++;
  13. }
  14. if(!count) cout << 0 << endl;
  15. else if(count == 2) cout << 1 << endl;
  16. else cout << -1 << endl;
  17. }
  18. return 0;
  19. }

问题 C: 兔兔的快餐店

 

思路:暴力寻找同个时间出现最多的次数

参考代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. //问题 C: 兔兔的快餐店
  5. int n,index = 0,max = 0;cin >> n;
  6. int m[100005] = {0},count[2000] = {0},hour;
  7. while(n--){
  8. cin >> hour >> m[index];
  9. m[index] += hour*60;
  10. count[m[index]]++;
  11. index++;
  12. }
  13. for(int i = 0;i<2000;i++) if(count[i] > max && count[i]!=0) max = count[i];
  14. cout << max << endl;
  15. return 0;
  16. }

问题 D: 解锁

 

思路: 很明显得用深度搜索算法bfs,但是这里因为锁长度只有4,我们直接暴力寻找即可

参考代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. set<string> ban; //集合ban用于存放锁的引爆状态
  4. string init,res; //init为锁的初始状态,res为锁的解锁状态
  5. struct node
  6. {
  7. string s;
  8. int step;
  9. }t,t1;
  10. map<string,int> mp;//相当于bfs模板中的vis[]数组
  11. queue<node> q;
  12. void bfs(){
  13. t.s = init;t.step = 0;
  14. q.push(t);
  15. while(!q.empty()){
  16. t=q.front();q.pop();
  17. if(t.s==res){
  18. cout << t.step;
  19. return;
  20. }
  21. if(mp.count(t.s)) continue;
  22. mp[t.s]=1;
  23. if(!ban.count(t.s)){
  24. for(int i=0;i<t.s.length();i++){
  25. if(t.s[i]=='9'){ //字符'9'特殊处理
  26. t1.s = t.s;t1.s[i]--;t1.step = t.step + 1;
  27. q.push(t1);
  28. t1.s[i]='0';
  29. q.push(t1);
  30. }
  31. else if(t.s[i]=='0'){ //字符'0'特殊处理
  32. t1.s = t.s;t1.s[i]++;t1.step = t.step + 1;
  33. q.push(t1);
  34. t1.s[i]='9';
  35. q.push(t1);
  36. }
  37. else{
  38. t1.s = t.s;t1.s[i]--;t1.step = t.step + 1;
  39. q.push(t1);
  40. t1.s[i] += 2;
  41. q.push(t1);
  42. }
  43. }
  44. }
  45. }
  46. }
  47. int main(){
  48. //问题 D: 解锁
  49. cin >> init >> res;
  50. int w = init.length();
  51. string ss;
  52. cin >> ss;
  53. int t = 0;
  54. for(int i=0;i<ss.length();i++){
  55. if(ss[i]==',') {
  56. string s=ss.substr(t,i-t);
  57. ban.insert(s);
  58. t=i+1;
  59. }
  60. }
  61. ban.insert(ss.substr(t,ss.length()-1));
  62. bfs();
  63. return 0;
  64. }

问题 E: 相似数组

 

思路:读入数组a[],b[],对两个数组升序排序。判断如果a[]与b[]有相同数据则消去该若干对数据即都置0。对于剩下的元素,如果有值大于9的数据则对其做一次f()运算,每做一次f()运算则操作次数+1,再次判断是否有相同数据,如果有则消去该若干对数据。最后判断剩下的元素的值,如果等于1则跳过即不做任何处理,因为后续另外一个数组里其他的数字可以再进行一次f()运算后与这个值1抵消;剩下的每遇到不等于1且不等于0的数,操作次数+1,意味着它们可以通过一次f()变成1相等后消去,但是这次我们不需要做f()运算了。

参考题解:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int f(int x){ //求数字长度函数
  4. int temp = x,count = 0;
  5. while(temp != 0){
  6. count++;
  7. temp /= 10;
  8. }
  9. return count;
  10. }
  11. int main(){
  12. //问题 E: 相似数组
  13. int t;cin >> t;
  14. while(t--){
  15. int n,count = 0;cin >> n;
  16. int a[200010],b[200010];
  17. for(int i = 0;i<n;i++) cin >> a[i]; //读a[]数组
  18. for(int i = 0;i<n;i++) cin >> b[i]; //读b[]数组
  19. sort(a,a+n);sort(b,b+n); //排序,目的是加快后续找相同元素的速度
  20. for(int i = 0;i<n;i++){ //抵消两个数组中相同的元素即置0
  21. for(int j = 0;j<n;j++){
  22. if(a[i] == b[j]){
  23. a[i] = 0;b[j] = 0;break;
  24. }
  25. }
  26. }
  27. for(int i = 0;i<n;i++){ //如果两个数组中有大于9的数字,则count++,并且对该数字求长度
  28. if(a[i]!=0&&a[i]>9){
  29. count++;
  30. a[i] = f(a[i]);
  31. }
  32. if(b[i]!=0&&b[i]>9){
  33. count++;
  34. b[i] = f(b[i]);
  35. }
  36. }
  37. for(int i = 0;i<n;i++){ //再次消去两个数组中相同的数字即置0
  38. for(int j = 0;j<n;j++){
  39. if(a[i] == b[j]){
  40. a[i] = 0;b[j] = 0;break;
  41. }
  42. }
  43. }
  44. for(int i = 0;i<n;i++){ //最后找小于10且不等于0且不等于1的数字,找到一个就count++
  45. if(a[i] != 0&&a[i] != 1) count++;
  46. if(b[i] != 0&&b[i] != 1) count++;
  47. }
  48. cout << count << endl;
  49. }
  50. return 0;
  51. }

问题 F: 兔兔的平均值 

思路:观察X与Y的值,如果X与Y相等,那么对于数字来说无论选择X还是Y,最终的平均值之和还是相等的,于是结果先输入1保证字典序最小;如果X与Y不相等,两个集合中较小的那个集合应该选择更大的数来确保最终的平均值之和最大。

该题思路来自于bu-fan:https://www.cnblogs.com/bu-fan/p/17904710.html

参考题解:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int sum,id;
  5. }a[100005];
  6. int n,x,y;
  7. bool cmp1(node x,node y){
  8. return x.sum==y.sum?x.id<y.id:x.sum>y.sum;
  9. }
  10. bool cmp2(node x,node y){
  11. return x.sum==y.sum?x.id>y.id:x.sum>y.sum;
  12. }
  13. int main(){
  14. //问题 F: 兔兔的平均值
  15. cin >> n;
  16. cin >> x >> y;
  17. for(int i=1;i<=n;i++){
  18. cin >> a[i].sum;
  19. a[i].id=i;
  20. }
  21. vector<int> b(n+1);
  22. if(x==y){
  23. for(int i=1;i<=n;i++){
  24. if(i<=n/2) cout << "1 ";
  25. else cout << "2 ";
  26. }
  27. cout << endl;
  28. return 0;
  29. }
  30. else if(x<y){
  31. sort(a+1,a+1+n,cmp1);
  32. for(int i=1;i<=x;i++){
  33. b[a[i].id]=1;
  34. }
  35. for(int i=x+1;i<=n;i++) {
  36. b[a[i].id]=2;
  37. }
  38. }
  39. else{
  40. sort(a+1,a+1+n,cmp2);
  41. for(int i=1;i<=y;i++){
  42. b[a[i].id]=2;
  43. }
  44. for(int i=y+1;i<=n;i++) {
  45. b[a[i].id]=1;
  46. }
  47. }
  48. for(int i=1;i<=n;i++)
  49. cout << b[i] << " ";
  50. cout << endl;
  51. return 0;
  52. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/426270?site
推荐阅读
相关标签
  

闽ICP备14008679号