PAT甲级1060 Are They Equal【模拟】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872
题意:
给定两个数,表示成0.xxxxx*10^k次这样的科学记数法之后,判断小数点后的n位是否相同
思路:
分类大讨论。细节要考虑清楚。因为最多是100位所以要用字符串处理。
首先我们要知道这两个数的小数点在什么位置(digita,digtb),如果没有小数点就默认在最后一位。
然后我们还需要知道有没有前导零(firsta,firstb),比如0.001,这样的数小数点也是要挪的。
所以*10^k中的k应该是$digita - firsta$,如果结果是负数还应该加一。
然后是输出前面的东西。要特别判断一下他们是不是0.
把输出的小数点之后的数字重新存储一下,如果不到n位就在后面补0
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n;
char a[], b[]; int main()
{
scanf("%d", &n);
scanf("%s%s", a, b);
int tmpa[], tmpb[];
int diga = -, digb = -;
int lena = strlen(a), lenb = strlen(b);
int da = , db = ;
int firsta = -, firstb = -;
for(int i = ; i < lena; i++){
if(a[i] == '.'){
diga = i;
continue;
}
if(firsta == - && a[i] != ''){
firsta = i;
}
if(diga != - && firsta != -)break;
}
if(diga == -)diga = lena;
if(firsta != -){
diga -= firsta;
if(diga < )diga++;
}
// if(diga < 0){
// while(da < abs(diga)){
// tmpa[da++] = 0;
// }
// }
for(int i = max(firsta, ); i < lena; i++){
if(a[i] == '.'){
continue;
}
tmpa[da++] = a[i] - '';
}
while(da < n){
tmpa[da++] = ;
} for(int i = ; i < lenb; i++){
if(b[i] == '.'){
digb = i;
continue;
}
if(firstb == - && b[i] != ''){
firstb = i;
}
}
if(digb == -){
digb = lenb;
}
if(firstb != -){
digb -= firstb;
if(digb < )digb++;
}
// if(digb < 0){
// while(db < abs(digb)){
// tmpb[db++] = 0;
// }
// }
for(int i = max(, firstb); i < lenb; i++){
if(b[i] == '.')continue;
tmpb[db++] = b[i] - '';
}
while(db < n){
tmpb[db++] = ;
} //cout<<firsta<<endl<<firstb<<endl;
//cout<<diga<<endl<<digb<<endl; if(diga != digb && (firsta != - || firstb != -) && (firsta < n || firstb < n)){
printf("NO 0.");
if(firsta == - || firsta >= n){
for(int i = ; i < n; i++){
printf("");
}
printf("*10^0 0.");
}
else{
for(int i = ; i < n; i++){
printf("%d", tmpa[i]);
}
printf("*10^%d 0.", diga);
}
if(firstb == - || firstb >= n){
for(int i = ; i < n; i++){
printf("");
}
printf("*10^0\n");
}
else{
for(int i = ; i < n; i++){
printf("%d", tmpb[i]);
}
printf("*10^%d\n", digb);
}
}
else{
bool flag = true;
for(int i = ; i < n; i++){
if(tmpa[i] != tmpb[i]){
flag = false;
break;
}
}
if(firsta == - && firstb == -)flag = true;
if(flag){
printf("YES 0.");
if(firsta == -){
for(int i = ; i < n; i++){
printf("");
}
printf("*10^0\n");
}
else{
for(int i = ; i < n; i++){
printf("%d", tmpa[i]);
}
printf("*10^%d\n", diga);
}
}
else{
printf("NO 0.");
if(firsta == -){
for(int i = ; i < n; i++){
printf("");
}
printf("*10^0 0.");
}
else{
for(int i = ; i < n; i++){
printf("%d", tmpa[i]);
}
printf("*10^%d 0.", diga);
}
if(firstb == -){
for(int i = ; i < n; i++){
printf("");
}
printf("*10^0\n");
}
else{
for(int i = ; i < n; i++){
printf("%d", tmpb[i]);
}
printf("*10^%d\n", digb);
}
}
} return ;
}
PAT甲级1060 Are They Equal【模拟】的更多相关文章
- PAT 甲级 1060 Are They Equal
1060. Are They Equal (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue If a ma ...
- PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)
1060 Are They Equal (25 分) If a machine can save only 3 significant digits, the float numbers 1230 ...
- 【PAT】1060 Are They Equal (25)(25 分)
1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ...
- PAT甲级1026 Table Tennis【模拟好题】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805472333250560 题意: 有k张乒乓球桌,有的是vip桌 ...
- PAT甲级1080 Graduate Admission【模拟】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805387268571136 题意: 模拟高考志愿录取. 考生根据总 ...
- PAT 甲级 1026 Table Tennis(模拟)
1026. Table Tennis (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A table ...
- pat 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)
1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigne ...
- PAT甲级——A1060 Are They Equal
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...
随机推荐
- Ubuntu16.04下安装搭配Python3.6相关配置软件方法
1 安装Python3.6.4 此处推荐直接安装Anaconda3,来实现Python3.6.4的环境配置. Anaconda3下载链接:https://www.anaconda.com/downlo ...
- SoapUI Pro Project Solution Collection-XML assert
in soapui the XML object used here is from org.w3c.dom package so you need to read this article car ...
- [AaronYang]那天有个小孩跟我说Js-NodeJS[AY0]-EJS
按照自己的思路学习Node.Js 随心出发.EJS是Node.js中express框架中使用的一个模版引擎,当然还有Jade 我的学习就靠网上查资料,没有买书系统学,自己整理,如果有用了哪位大神的代码 ...
- leetcode笔记:3Sum Closest
一.题目描写叙述 二.解题技巧 该题与3Sum的要求类似.不同的是要求选出的组合的和与目标值target最接近而不一定相等.但实际上,与3Sum的算法流程思路类似,先是进行排序.然后顺序选择数组A中的 ...
- Maven使用详解,非常详细
本文转:http://blog.csdn.net/u010425776/article/details/52027706 什么是Maven? 如今我们构建一个项目需要用到很多第三方的类库,如写一个使用 ...
- sublime text 3 安装ES6插件
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- MXNET:权重衰减
权重衰减是应对过拟合问题的常用方法. \(L_2\)范数正则化 在深度学习中,我们常使用L2范数正则化,也就是在模型原先损失函数基础上添加L2范数惩罚项,从而得到训练所需要最小化的函数. L2范数惩罚 ...
- 创建shell脚本
1.写一个脚本 a) 用touch命令创建一个文件:touch my_script b) 用vim编辑器打开my_script文件:vi my_script c) 用vim编辑器编辑my_script ...
- Java知多少(3) 就业方向
Java的就业前景如何,看培训班就知道了,以Java培训为主的达内,已经上市. 根据IDC的统计,在所有软件开发类人才的需求中,对JAVA工程师的需求曾达到全部需求量的50%以上.而且,JAVA工程师 ...