当前位置: 首页 > news >正文

算法笔记上机训练实战指南刷题

算法笔记上机训练实战指南刷题记录

文章目录

  • 算法笔记上机训练实战指南刷题记录
    • 模拟
      • B1001 害死人不偿命的(3n+1)猜想
      • B1011 A+B 和 C
      • B1016 部分A+B
      • B1026 程序运行时间
      • B1046划拳
      • B1008数组元素循环右移问题
      • B1012 数字分类
      • B1018 锤子剪刀布
      • A1042 Shuffling Machine
    • 每天两题,持续更新中~

模拟

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

题号题目分数
B1001害死人不偿命的(3n+1)猜想15✔️
B1011A+B 和 C15✔️
B1016部分A+B15✔️
B1026程序运行时间15✔️
B1046划拳15✔️
B1008数组循环右移问题20✔️
B1012数字分类20✔️
B1018锤子剪刀布20✔️
A1042Shuffling Machine
A1046
B1065
B1010
B1002
B1009

B1001 害死人不偿命的(3n+1)猜想

#include <iostream>
using namespace std;
int n, cnt;int main()
{cin >> n;while(n != 1){if(n % 2)n = (3 * n + 1) / 2;elsen /= 2;cnt ++;}cout << cnt << endl;
}

B1011 A+B 和 C

⚠️ A+B可能爆INT。需要开long long。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

#include <iostream>
using namespace std;
int n;
long long a, b, c;int main()
{cin >> n;for(int i = 1; i <= n; i ++){cin >> a >> b >> c;cout << "Case #" << i << ": " << (a + b > c ? "true" : "false") << endl;}return 0;
}

B1016 部分A+B

思路1:

枚举DA,DB出现的次数cntDA, cntDB, 计算PA, PB, 输出PA+PB

A,B用字符串存取,枚举DA,DB次数可以遍历A,B字符串。

#include <iostream>
#include <cstring>
using namespace std;
string A, DA, B, DB;
int cntDA, cntDB;
long long resa, resb;
int main()
{cin >> A >> DA >> B >> DB;for(int i = 0; i < A.size(); i ++)cntDA += (A[i] == DA[0]);for(int i = 0; i < B.size(); i ++)cntDB += (B[i] == DB[0]);if(cntDA) resa = DA[0] - '0';if(cntDB) resb = DB[0] - '0';for(int i = 1; i < cntDA; i ++) resa = resa * 10 + (DA[0] - '0');for(int i = 1; i < cntDB; i ++) resb = resb * 10 + (DB[0] - '0');cout << resa + resb << endl;return 0;
}

思路2:

image-20250625195120977

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
LL PA, PB, A, DA, B, DB;;
int main()
{cin >> A >> DA >> B >> DB;while(A){int x = A % 10;if(x == DA) PA = PA * 10 + DA;A /= 10;}while(B){int x = B % 10;if(x == DB) PB = PB * 10 + DB;B /= 10;}cout << PA + PB << endl;return 0;
}

个人推荐思路2,纯粹的模拟做法。

B1026 程序运行时间

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

#include <iostream>
using namespace std;
int c1, c2, t;int main()
{cin >> c1 >> c2;// t = (c2 - c1 + 50) / 100;t = c2 - c1;if(t % 100 >= 50) t = t / 100 + 1;else t = t / 100;printf("%02d:%02d:%02d\n", t / 3600, t % 3600 / 60, t % 60);
}

B1046划拳

failA,failB记录各自输的次数

#include <iostream>
using namespace std;
int failA, failB;
int n;int main()
{cin >> n;while(n --){int a1, a2, b1, b2;cin >> a1 >> a2 >> b1 >> b2;if(a1 + b1 == a2 && a1 + b1 != b2) failB ++;if(a1 + b1 == b2 && a1 + b1 != a2) failA ++;}cout << failA << " " << failB << endl;return 0;
}

B1008数组元素循环右移问题

⚠️题目没有给定M的最大值,不能认为M<N。读入后需要令M=M%N。

直接输出N-M到N-1号元素,再输出0—N - M - 1号元素。

#include <iostream>
using namespace std;
const int N = 110;
int a[N];
int n, m;int main()
{scanf("%d %d", &n, &m);m %= n;for(int i = 0; i < n; i ++)scanf("%d", &a[i]);for(int i = n - m; i < n; i ++)cout << a[i] << " ";for(int i = 0; i < n - m; i ++)cout << a[i] << " \n"[ i == n - m - 1];return 0;
}

B1012 数字分类

#include <iostream>
using namespace std;
const int N = 1010;
int n;
int A1, A2, A3, A4, A5;
int cntA2, cntA4;
int a[N];int main()
{cin >> n;for(int i = 1; i <= n; i ++){cin >> a[i];if(a[i] % 10 == 0) A1 += a[i];if(a[i] % 5 == 1){cntA2 ++;if(cntA2 % 2 == 1) A2 += a[i];else A2 += -1 * a[i];}if(a[i] % 5 == 2) A3 ++;if(a[i] % 5 == 3) A4 += a[i], cntA4 ++;if(a[i] % 5 == 4) A5 = max(A5, a[i]);}double a4 = A4 * 1.0 / cntA4;printf("%d %d %d %.1lf %d", A1, A2, A3, a4, A5);printf("%d %d %d %.1lf %d", A1, A2, A3, a4, A5);printf("%d %d %d %.1lf %d", A1, A2, A3, a4, A5);printf("%d %d %d %.1lf %d", A1, A2, A3, a4, A5);printf("%d %d %d %.1lf %d", A1, A2, A3, a4, A5);return 0;
}

B1018 锤子剪刀布

题目要求输出甲乙获胜,平局,输了的次数,还要输出获胜最多的手势。

按照字典序 B布 C锤子 J剪刀 顺序布赢锤子,锤子赢剪刀,剪刀赢布,用mp数组存对应的手势,mp[0] = ‘B’,

times_A[3], times_B[3]分别存甲乙,胜场,平场,输场次数。hand_A[3]和hand_B[3]存甲乙布,锤子,剪刀胜利次数。

image-20250702202645273

#include <iostream>
using namespace std;char mp[3] = {'B', 'C', 'J'};
int n;
int times_A[3], times_B[3];
int hand_A[3], hand_B[3];
char c1, c2;
int k1, k2;int change(char c)
{if(c == 'B') return 0;if(c == 'C') return 1;if(c == 'J') return 2;
}int main()
{cin >> n;while(n --){cin >> c1 >> c2;k1 = change(c1);k2 = change(c2);if((k1 + 1) % 3 == k2)//甲获胜{times_A[0] ++;times_B[2] ++;hand_A[k1] ++;}else if(k1 == k2)//平局{times_A[1] ++;times_B[1] ++;}else{times_A[2] ++;times_B[0] ++;hand_B[k2] ++;}}printf("%d %d %d\n", times_A[0], times_A[1], times_A[2]);printf("%d %d %d\n", times_B[0], times_B[1], times_B[2]);int id1 = 0, id2 = 0;for(int i = 0; i < 3; i ++){if(hand_A[i] > hand_A[id1]) id1 = i;if(hand_B[i] > hand_B[id2]) id2 = i;}printf("%c %c\n", mp[id1], mp[id2]);return 0;
}

A1042 Shuffling Machine

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

#include <iostream>
using namespace std;
const int N = 60;
char mp[5] = {'S', 'H', 'C', 'D', 'J'};
int Start[N], End[N], Next[N];
int k;int main()
{cin >> k;for(int i = 1; i <= 54; i ++) Start[i] = i;for(int i = 1; i <= 54; i ++) cin >> Next[i];for(int step = 1; step <= k; step ++){for(int i = 1; i <= 54; i ++){End[Next[i]] = Start[i];}for(int i = 1; i <= 54; i ++){Start[i] = End[i];}}for(int i = 1; i <= 54; i ++){if(i != 1) printf(" ");Start[i] --;printf("%c%d", mp[Start[i] / 13], Start[i] % 13 + 1);}return 0;
}

每天两题,持续更新中~

另外:自制PAT做题倒计时插件自制 PTA(拼题A)平台 计时器浏览器插件

http://www.dtcms.com/a/265276.html

相关文章:

  • pytorch学习-9.多分类问题
  • WSL2 + Docker Desktop 环境中查看本地镜像
  • 基于SpringBoot的场地预定管理系统
  • Springboot开发常见注解一览
  • 记一次finallshell.exe打开无法应的处理
  • 【卡尔曼滤波第二期】一维无过程噪声的卡尔曼滤波
  • 红黑树:高效平衡的秘密
  • 声网支持弱网对抗保障直播不卡不花屏
  • Android Native 之 init初始化selinux机制
  • Beamer-LaTeX学习(教程批注版)【4】
  • (LangChain)RAG系统链路向量检索器之Retrievers(五)
  • 设计模式精讲 Day 23:访问者模式(Visitor Pattern)
  • Python 的内置函数 print
  • RT Thread Studio修改堆区大小的方法
  • Python 中 http.client 与 requests 的对比及适用场景
  • 数据结构20250620_数据结构考试
  • android核心技术摘要
  • 冲突检测美国服务器:原理剖析与实战解决方案
  • Zig 安装使用教程
  • HCIA-以太网链路聚合
  • Flask 安装使用教程
  • C#上位机串口接口
  • Hamilton系统特征线法下的非线性PDE解与爆破时间分析
  • C++ 完美转发(泛型模板函数)
  • ssh连接服务器,有了ip和private key,还需要其它吗
  • 技术学习_大语言模型
  • el-button/button置灰及原理
  • 应急响应类题练习——玄机第一章 应急响应- Linux入侵排查
  • 代码随想录算法训练营第四十三天|动态规划part10
  • 2025-6GESP六级编程题分析