PTA甲级—数学
2024-09-24 21:37:00
1.简单数学
1008 Elevator (20分)
模拟题
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f
using namespace std;
const int maxn = 1e4+100;
int n, sum, now, tmp;
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &tmp);
int num = tmp-now;
sum += num > 0 ? num*6 : num*(-4);
now = tmp;
}
sum += 5*n;
printf("%d", sum);
}
1049 Counting Ones (30 分)
1的个数等于各个位出现1的次数的累加和,因此我们计算各个位上对答案的贡献即可。
那么关键的问题就在如何计算某一位上的贡献,当然就是求这一位为1时数的个数,如下:
(具体为什么是这样可以在脑子或者纸上演算一下)
我一开始的思路是对的,但是那会想不出来具体的做法,考试时暴力骗个分还是可以的。另外上机指南中提到可以对程序进行边界测试,确实应当如此,以后多使用几种方法来测试自己的代码
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int maxn = 2e6+100;
int n, res;
int main(){
scanf("%d", &n);
int left = n, right, a = 1, now;
while(left){
now = left%10, right = n-left*a, left /= 10;
if(now==0) res += left*a;
else if(now==1) res += left*a+right+1;
else res += (left+1)*a;
a *= 10;
}
printf("%d", res);
}
Reference:
https://blog.csdn.net/CV_Jason/article/details/85112495
1069 The Black Hole of Numbers (20 分)
按照题目要求模拟即可,不过要注意要考虑全面:
1.所有输出都要求4位数
2.考虑特殊样例如6174、2222、1情况下的处理。如输入为1的时候要补充前导0;输入为6174的时候要输出一行而不能直接退出
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int maxn = 1e5+100;
string a, b, c, last, s;
int tmp;
int main(){
cin >> c;
while(1){
for(int i = 0; i < 4-c.length(); i++) s += "0";
s += c, a = b = s, last = c;
sort(a.begin(), a.end()), sort(b.rbegin(), b.rend());
c = to_string(stoi(b) - stoi(a)), s.clear();
printf("%04d - %04d = %04d\n", stoi(b), stoi(a), stoi(c));
if(c=="6174"||c=="0") break;
}
}
最新文章
- C++开始前篇,深入编译链接(补充1)
- LSD-SLAM深入学习(2)-算法解析
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
- javascript保留两位小数
- PHPExcel上传sae遇到: -1:fail to get xml content
- MySQL各个版本区别
- ubuntu完美卸载JDK
- Android(java)学习笔记61:多线程程序的引入
- 出现错误ActivityManager: Warning: Activity not started, its current task has been
- EasyUI datagrid自适应问题解决
- 关于Daydream VR的最直白的介绍
- iOS6 自动布局 入门–Auto Layout(转)
- C#正则提取HTML中img的url值
- DHTML【11】--DOM
- Python数据结构-元祖
- Redhat Linux下的python版本号升级
- Spingmvc项目注册登录图片验证码(比较灵活的验证码)
- express+handlebars 快速搭建网站前后台
- thinkphp 操作xml格式
- lvm再次学习
热门文章
- Codeforces Round #575 (Div. 3) B. Odd Sum Segments 、C	 Robot Breakout
- Codeforces Global Round 11 B. Chess Cheater (贪心,结构体排序)
- PowerShell随笔9--- call
- CF1466-D. 13th Labour of Heracles
- spring再学习之配置详解
- HDU 5608 function(莫比乌斯反演 + 杜教筛)题解
- CS144学习(2)TCP协议实现
- 使用 js 实现十大排序算法: 希尔排序
- how to updating Node.js and npm
- React PureComponent All In One