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中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程 ...
随机推荐
- hdu2818 Building Block
Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbere ...
- hdu5305 Friends
Problem Description There are n people and m pairs of friends. For every pair of friends, they can c ...
- 【cpp上】课后正误小题
State whether each of the following is true or false. If false, explain why. Assume the state ment u ...
- kubernetes进阶(一) kubectl工具使用详解
管理k8s核心资源的三种基本方法: 一.陈述式-主要依赖命令行工具 --可以满足90%以上的使用场景,但是缺点也很明显: 命令冗长,复杂,难以记忆 特定场景下,无法实现管理需求 对资源的增.删.查操 ...
- cobaltstrike的使用
0x01 介绍 Cobalt Strike是一款渗透测试神器,常被业界人称为CS神器.Cobalt Strike已经不再使用MSF而是作为单独的平台使用,它分为客户端与服务端,服务端是一个,客户端可以 ...
- 【情人节选帽子】TCS34725颜色传感器和Python图形界面编程(STM32 HAL库)
截图 描述: l STM32 HAL库编程 l 使用模拟IIC通信,方便程序移植 l Python界面编写,蘑菇头的帽子是什么颜色 l STM32 HAL库串口通信 l Python界面使用 ...
- 最新 Vue 源码学习笔记
最新 Vue 源码学习笔记 v2.x.x & v3.x.x 框架架构 核心算法 设计模式 编码风格 项目结构 为什么出现 解决了什么问题 有哪些应用场景 v2.x.x & v3.x.x ...
- Django : Security in Django
Security in Django https://docs.djangoproject.com/en/1.10/topics/security/ 1 Cross site scripting (X ...
- CSS Multiple Columns
CSS Multiple Columns CSS layout column-count column-gap column-rule-style column-rule-width column-r ...
- calendar time shaper
calendar time shaper const dateObj = { "id": 191837, "productId": 13602, "a ...