PAT A1015-1016
A 1015 Reversible Primes
看清题意即可。给的数是十进制的,需要先判断是不是素数,然后按照给定进制转化成字符串后进行翻转,最后再转化为十进制并判断是否为素数。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std; int N, D;
char numStr[];
bool isPrime(int tmpNum)
{
if(tmpNum < )
return false;
int maxNum = sqrt(1.0*tmpNum);
for(int i = ; i <= maxNum; ++ i)
if(tmpNum % i == )
return false;
return true;
}
int strToNum()
{
int len = strlen(numStr), tmpNum = ;
for(int i = ; i < len; ++ i)
{
tmpNum = tmpNum*D + numStr[i]-'';
}
return tmpNum;
}
int main()
{
int tmpNum;
while(scanf("%d%d", &N, &D) != EOF && N >= )
{
if(!isPrime(N))
{
cout << "No" << endl;
continue;
}
for(int i = ; N > ; ++ i)
{
numStr[i] = N % D + '';
N /= D;
numStr[i+] = '\0';
}
tmpNum = strToNum();
if(!isPrime(tmpNum))
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return ;
}
A 1016 1016 Phone Bills
因为存在无效的记录,所以需要将他们先进行配对。处理方法是:按照名字和时间进行排序,然后筛选出所需要的记录再进行下一步处理。
超时问题:每一个有效时间段的话费计算需要尽量减少计算量。
以下是柳婼精简的代码:
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
struct node {
string name;
int status, month, time, day, hour, minute;
};
bool cmp(node a, node b) {
return a.name != b.name ? a.name < b.name : a.time < b.time;
}
double billFromZero(node call, int *rate) {
double total = rate[call.hour] * call.minute + rate[] * * call.day;
for (int i = ; i < call.hour; i++)
total += rate[i] * ;
return total / 100.0;
}
int main() {
int rate[] = {}, n;
for (int i = ; i < ; i++) {
scanf("%d", &rate[i]);
rate[] += rate[i];
}
scanf("%d", &n);
vector<node> data(n);
for (int i = ; i < n; i++) {
cin >> data[i].name;
scanf("%d:%d:%d:%d", &data[i].month, &data[i].day, &data[i].hour, &data[i].minute);
string temp;
cin >> temp;
data[i].status = (temp == "on-line") ? : ;
data[i].time = data[i].day * * + data[i].hour * + data[i].minute;
}
sort(data.begin(), data.end(), cmp);
map<string, vector<node> > custom;
for (int i = ; i < n; i++) {
if (data[i].name == data[i - ].name && data[i - ].status == && data[i].status == ) {
custom[data[i - ].name].push_back(data[i - ]);
custom[data[i].name].push_back(data[i]);
}
}
for (auto it : custom) {
vector<node> temp = it.second;
cout << it.first;
printf(" %02d\n", temp[].month);
double total = 0.0;
for (int i = ; i < temp.size(); i += ) {
double t = billFromZero(temp[i], rate) - billFromZero(temp[i - ], rate);
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", temp[i - ].day, temp[i - ].hour, temp[i - ].minute, temp[i].day, temp[i].hour, temp[i].minute, temp[i].time - temp[i - ].time, t);
total += t;
}
printf("Total amount: $%.2f\n", total);
}
return ;
}
PAT A1015-1016的更多相关文章
- PAT甲级1016. Phone Bills
PAT甲级1016. Phone Bills 题意: 长途电话公司按以下规定向客户收取费用: 长途电话费用每分钟一定数量,具体取决于通话时间.当客户开始连接长途电话时,将记录时间,并且客户挂断电话时也 ...
- PAT——乙级1016
乙级PAT的1016 乙级的题相对比较简单,我也是主要联系写代码的格式,而不是联系算法. 1016 部分A+B (15 point(s)) 正整数 A 的“DA(为 1 位整数)部分”定义为由 ...
- PAT A 1016. Phone Bills (25)【模拟】
题目:https://www.patest.cn/contests/pat-a-practise/1016 思路:用结构体存储,按照名字和日期排序,然后先判断是否有效,然后输出,时间加减直接暴力即可 ...
- PAT乙级 1016. 部分A+B (15) C语言实现
1016. 部分A+B (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 正整数A的“DA(为1位整数)部 ...
- PAT Basic 1016
1016 部分A+B (15 分) 正整数 A 的“DA(为 1 位整数)部分”定义为由 A 中所有 DA 组成的新整数 PA.例如:给定 A=3862767,DA=6,则 A ...
- PAT乙级 1016. 部分A+B (15)
题目传送:https://www.patest.cn/contests/pat-b-practise/1016 正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A ...
- PAT乙级1016
1016 部分A+B (15 分) 正整数 A 的“DA(为 1 位整数)部分”定义为由 A 中所有 DA 组成的新整数 PA.例如:给定 A=3862767,DA=6,则 ...
- 【PAT】1016 部分A+B(15 分)
1016 部分A+B(15 分) 正整数 A 的“DA(为 1 位整数)部分”定义为由 A 中所有 DA 组成的新整数 PA.例如:给定 A=3862767,DA=6,则 A ...
- pat甲级1016
1016 Phone Bills (25)(25 分) A long-distance telephone company charges its customers by the following ...
- PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following r ...
随机推荐
- open-source--攻防世界
题目直接给了源码,发现只要跳过条件就可以得到flag
- vue-router 一个十分简单的应用场景
时间:2018-03-28 关于vue-router: 这里只大致说一下构建过程和使用情况,将就看看!! 我使用的是vue-cli脚手架+webpack构建的项目 安装vue-cli脚手架 npm i ...
- Java笔记--反射机制
1.Reflection(反射)是被视为动态语言的关键,反射机制允许程序在执行期借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方法. 2.Java反射机制提供 ...
- 7.4 Varnish VCL的子程序
- 5.Linux解决Device eth0 does not seem to be present
Linux操作系统排除故障 导入vixualbox的虚拟机voa文件到另外一台电脑,需要检查如下信息 修改虚拟机软件网络设置 重启Linux操作系统 shutdown -h now reboot se ...
- delphi10.2 命令行编译x64脚本
Build.bat @echo off @echo delphi x64编译测试 @cd /d %~dp0 @echo 设置Delphi参数信息 @set SourcePath=%~dp0.\src ...
- 清除windows激活信息
1.管理员运行命令提示符 在命令提示符中输入 slmgr /upk---删除当前KMS密匙 出现"成功地卸载了产品密匙"后,继续依次执行下面两个命令 slmgr /ckms---此 ...
- python中groupby函数详解(非常容易懂)
一.groupby 能做什么? python中groupby函数主要的作用是进行数据的分组以及分组后地组内运算! 对于数据的分组和分组运算主要是指groupby函数的应用,具体函数的规则如下: df[ ...
- python 安装opendr 报错OSMesa
### 报错/ERROR: ```opendr/contexts/OSMesa/include/GL/glext.h:5794:21: note: expected ‘const GLchar ** ...
- 文件上传报错java.io.FileNotFoundException拒绝访问
局部代码如下: File tempFile = new File("G:/tempfileDir"+"/"+fileName); if(!tempFile.ex ...