PTA甲级—数学
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;
}
}
PTA甲级—数学的更多相关文章
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- PTA甲级B1061 Dating
目录 B1061 Dating (20分) 题目原文 Input Specification: Output Specification: Sample Input: Sample Output: 生 ...
- PTA 甲级 1139
https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 其实这道题目不难,但是有很多坑点! 首先数据 ...
- PTA甲级—链表
1032 Sharing (25分) 回顾了下链表的基本使用,这题就是判断两个链表是否有交叉点. 我最开始的做法就是用cnt[]记录每个节点的入度,发现入度为2的节点即为答案.后来发现这里忽略了两个链 ...
- PTA甲级—STL使用
1051 Pop Sequence (25分) [stack] 简答的栈模拟题,只要把过程想清楚就能做出来. 扫描到某个元素时候,假如比栈顶元素还大,说明包括其本身的在内的数字都应该入栈.将栈顶元素和 ...
- PTA甲级—常用技巧与算法
散列 1078 Hashing (25 分) Quadratic probing (with positive increments only) is used to solve the collis ...
- PTA L3-023 计算图 (dfs+数学推导)
“计算图”(computational graph)是现代深度学习系统的基础执行引擎,提供了一种表示任意数学表达式的方法,例如用有向无环图表示的神经网络. 图中的节点表示基本操作或输入变量,边表示节点 ...
- PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...
- (数学) PTA 1005 继续(3n+1)猜想 (25 分)
1005 继续(3n+1)猜想 (25 分) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程 ...
随机推荐
- Codeforces Round #673 (Div. 2) C. k-Amazing Numbers (DP,思维)
题意:有一组数,分别用长度从\([1,n]\)的区间去取子数组,要求取到的所有子数组中必须有共同的数,如果满足条件数组共同的数中最小的数,否则输出\(-1\). 题解:我们先从后面确定每两个相同数之间 ...
- Kafka官方文档V2.7
1.开始 1.1 简介 什么是事件流? 事件流相当于人体的中枢神经系统的数字化.它是 "永远在线 "世界的技术基础,在这个世界里,业务越来越多地被软件定义和自动化,软件的用户更是软 ...
- anaconda + pyqt5 + pycharm 安装,测试
1. 安装sip pip install sip 2.安装pyqt5 pip install PyQt5 pip install PyQt5-tools -i http://pypi.douban.c ...
- 关于ucore实验一的资料查找
任务:阅读实验一makefile 搞清楚ucore.img是如何构建的 $@ $< $^ 这三个变量分别是什么意思 https://blog.csdn.net/YEYUANGEN/arti ...
- C++模板沉思录
0 论抽象--前言 故事要从一个看起来非常简单的功能开始: 请计算两个数的和. 如果你对Python很熟悉,你一定会觉得:"哇!这太简单了!",然后写出以下代码: def Plus ...
- jira 优先级过滤
jira 优先级过滤 priority project = DAP AND issuetype = 故障 AND priority = high AND resolution = Unresolved ...
- BB link
1 1 1 BB link: 1 1 demo: code: result: 1 1 1 1 1 1 1
- flutter & dart & vs code & bug
flutter & dart & vs code & bug Waiting for another flutter command to release the startu ...
- Axios all in one
Axios all in one https://github.com/axios/axios#example GET const axios = require('axios'); // Make ...
- git alias & zsh
git alias & zsh VPN & git work tree # git pull === gp ➜ .git git:(feature/select-seat-system ...