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 ...
随机推荐
- dyld`__abort_with_payload:
dyld`__abort_with_payload: 0x1030422f0 <+0>: mov x16, #0x209 0x1030422f4 <+4>: svc ...
- Variable used in lambda expression should be final or effectively final
Lambda与匿名内部类在访问外部变量时,都不允许有修改变量的倾向,即若: final double a = 3.141592; double b = 3.141592; DoubleUnaryOpe ...
- Asp.Net MVC4中的全局过滤器
可以对整个项目进行全局监控. 新建一个MVC4项目,可以在global.asax文件中看到如下代码: FilterConfig.RegisterGlobalFilters(GlobalFilters ...
- NDK配置
NDK 配置 Android SDK中下载NDK, LLDB Android.mk 和 Application.mk 简单来说 Android.mk 用来描述需要生成哪些模块的 .so 文件 Appl ...
- numpy数组(4)-二维数组
python创建二维 list 的方法是在 list 里存放 list : l = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] numpy可以直接 ...
- IOS开发之Storyboard应用
制作一个Tab类型的应用 制作一个表格视图 原型表格单元 设计自定义的原型单元格 为原型单元格设置子类 故事版(Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明 ...
- 浅析tornado 中demo的 blog模块
#!/usr/bin/env python # # Copyright 2009 Facebook # # Licensed under the Apache License, Version 2.0 ...
- 关于Python打包运行的一些思路
需求 本地开发python django应用程序,然后放到生产环境运行.使用了tensorflow,手动安装包很麻烦.生产环境不能联网,不能使用 pip freeze. 思路: 使用docker,直接 ...
- django template if return false
如果if的参数不存在于context中就会返回false 参考:http://stackoverflow.com/questions/11107028/django-template-if-true- ...
- one-to-all及all-to-all网络通信模式
在这两种模式下,因为 占用的通信通道非常高,形成了一个一对多的通道 甚至是多对多的通道,导致现有的fattree网络结构负载太大.