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 ...
随机推荐
- MQTT 协议学习:003-MQTT通信流程介绍
背景 有关博文:通信报文的构成 . 上一讲说到可变头与消息体要结合不同的报文类型才能够进行分析(实际上,官方的文档的介绍顺序就是这样的) 那么,我们就来具体看看有关的报文类型. 在此之前 我们捋一捋完 ...
- CentOS7 部署 Jupyter Notebook
socket.error: [Errno 99] Cannot assign requested address 修改这几处: 1.hostname 参考:https://www.cnblogs.co ...
- delphi中json转dataset
unit uJSONDB; interface uses SysUtils, Classes, Variants, DB, DBClient, SuperObject, Dialogs; type T ...
- 提升Windows系统舒适度软件
1.Geek Uninstaller 卸载软件 2.PotPlayer 无广告播放器
- sql 报错问题
SQLServer数据库密码已过期问题 处理
- Problem I: Ingenious Lottery Tickets
Problem I: Ingenious Lottery Tickets Your friend Superstitious Stanley is always getting himself int ...
- Problem J. Joseph’s Problem 约瑟夫问题--余数之和
链接:https://vjudge.net/problem/UVA-1363 题意:给出n k,当 i 属于 1~n 时 ,求解 n% i 的和 n 和 k 的范围都是 1 到 10^9; 商相同 ...
- Windows Server 2008 R2 ntoskrnl.exe 引起蓝屏故障,重新启动
前不久在HP ProLiant DL360 G6的服务器上面安装了Windows Server 2008 R2,系统一到晚上凌晨就出现蓝屏.重启现象,并且在 C:\Windows\Minidump 目 ...
- mjpg-streamer视频服务器移植
相关软件下载地址:http://pan.baidu.com/s/16yo8Y JPEG库的移植 对于内核linux-2.6.35,不能再使用servfox,如果要使用的需要修改大量的代码.在此选用新的 ...
- python itertools 用法
1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存.使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环.- 无限迭代器 itertools包 ...