PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)

1060 Are They Equal (25 分)

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0 with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.

Input Specification:

Each input file contains one test case which gives three numbers N, A and B, where N (<) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 1, and that its total digit number is less than 100.

Output Specification:

For each test case, print in a line YES if the two numbers are treated equal, and then the number in the standard form 0.d[1]...d[N]*10^k (d[1]>0 unless the number is 0); or NO if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.

Note: Simple chopping is assumed without rounding.

Sample Input 1:

3 12300 12358.9

Sample Output 1:

YES 0.123*10^5

Sample Input 2:

3 120 128

Sample Output 2:

NO 0.120*10^3 0.128*10^3

题意:

给出两个数,问:将他们写成保留N (<100)位小数的科学计数法 0.d[1]...d[N]*10^k (d[1]>0 unless the number is 0); 后,是否相同。若相等,输出YES,并转换结果; 如果不相等,输出NO,并给出两个数的转换结果。

题解:

本题的思路难想而且情况判断非常复杂。

题目要求科学计数法时,两个数是否相等。因此只要判断科学技术法时的本体部分以及指数部分是否相等即可。

对于数据来说,要分为大于 1 与小于 1 来判断,要考虑各种情况下的数字,比如:0000, 000.00, 00123.4, 0.012, 0.00 等

一开始没有考虑太多的异常情况,拿了19分,第二天看了题解的注意点重做,还是只有21分,

第三天理了理思路,其实,只要关注,格式化好后的小数点要点在哪里,次数是多少就可以了,在此基础上,特判0,去掉前导零。比较的时候注意不要超限,输出不够0补齐。

自己编的测试样例:

2 0.0015 0000.001520000
YES 0.15*10^-2
3 010.25 0010.23
YES 0.102*10^2
5 0.1 00.100
YES 0.10000*10^0
10 000.0012 0000000.0012000000000
YES 0.1200000000*10^-2
3 0 0.000
YES 0.000*10^0

AC代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
char a[105];
char b[105];
int main(){
    cin>>n>>a>>b;
    int la=strlen(a);
    int lb=strlen(b);
    int sta=-1,stb=-1;
    int isa0=0,isb0=0;//特判是不是0

    //从第一个不是0的数开始
    for(int i=0;i<la;i++){
        if(a[i]!=‘.‘&&a[i]!=‘0‘){
            sta=i;
            break;
        }
    }
    for(int i=0;i<lb;i++){
        if(b[i]!=‘.‘&&b[i]!=‘0‘){
            stb=i;
            break;
        }
    }
    if(sta==-1) isa0=1;
    if(stb==-1) isb0=1;

    //小数点前有几位
    int ia=0,ib=0;
    int f=0;
    for(int i=0;i<la;i++){
        if(a[i]==‘.‘) break;
        if(a[i]!=‘0‘) f=1;
        if(f){
            ia++;
        }
    }
    f=0;
    for(int i=0;i<lb;i++){
        if(b[i]==‘.‘) break;
        if(b[i]!=‘0‘) f=1;
        if(f){
            ib++;
        }
    }
    //cout<<"sta:"<<sta<<" ia:"<<ia<<" ha:"<<ha<<endl;
    //cout<<"stb:"<<stb<<" ib:"<<ib<<" hb:"<<hb<<endl;

    //比较
    f=1;
    int pa=sta,pb=stb;
    if(isa0!=isb0 || ia!=ib) f=0;
    for(int i=0;i<n;i++){
        if(pa+i>=la||pb+i>=lb) break;
        if(a[pa]==‘.‘) pa++;
        if(b[pb]==‘.‘) pb++;
        //cout<<"pa "<<pa+i<<" a[pa] "<<a[pa+i]<<endl;
        //cout<<"pb "<<pa+i<<" b[pb] "<<b[pa+i]<<endl;
        if(a[pa+i]!=b[pb+i]){
            f=0;
            break;
        }
    }
    if(isa0==1&&isa0==isb0) f=1;//如果两个都是0

    //输出
    if(f){
        cout<<"YES 0.";
        if(isa0==1){//0的特判
            for(int i=1;i<=n;i++) cout<<"0";
            cout<<"*10^0";
        }else{
            pa=sta;
            for(int i=0;i<n;i++){
                if(pa+i>=la) {
                    cout<<"0";
                    continue;
                }
                if(a[pa+i]==‘.‘) pa++;
                if(pa+i>=la) cout<<"0";
                else cout<<a[pa+i];
            }
            cout<<"*10^";
            if(ia!=0) cout<<ia;//次数>=于0
            else{//次数<0
                int k=-1;
                for(int i=0;i<la;i++){
                    if(a[i]==‘.‘){
                        k=i;
                        break;
                    }
                }
                cout<<k-sta+1;//.的位置-开始的位置+1
            }
        }
    }else{//同上
        cout<<"NO 0.";
        if(isa0==1){
            for(int i=1;i<=n;i++) cout<<"0";
            cout<<"*10^0 ";
        }else{
            for(int i=0;i<n;i++){
                if(pa+i>=la) {
                    cout<<"0";
                    continue;
                }
                if(a[pa+i]==‘.‘) pa++;
                if(pa+i>=la) cout<<"0";
                else cout<<a[pa+i];
            }
            cout<<"*10^";
            if(ia!=0) cout<<ia<<" 0.";
            else{
                int k=-1;
                for(int i=0;i<la;i++){
                    if(a[i]==‘.‘){
                        k=i;
                        break;
                    }
                }
                cout<<k-sta+1<<" 0.";
            }
        }
        if(isb0==1){
            for(int i=1;i<=n;i++) cout<<"0";
            cout<<"*10^0";
        }else{
            for(int i=0;i<n;i++){
                if(pb+i>=lb) {
                    cout<<"0";
                    continue;
                }
                if(b[pb+i]==‘.‘) pb++;
                if(pb+i>=lb) cout<<"0";
                else cout<<b[pb+i];
            }
            cout<<"*10^";
            if(ib!=0) cout<<ib;
            else{
                int k=-1;
                for(int i=0;i<lb;i++){
                    if(b[i]==‘.‘){
                        k=i;
                        break;
                    }
                }
                cout<<k-stb+1;
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caiyishuai/p/11572214.html

时间: 2024-10-31 14:55:44

PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)的相关文章

PAT Advanced 1060 Are They Equal (25分)

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0 with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supp

PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. In

PAT 甲级 1083 List Grades (25 分)

1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval. I

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any oth

PAT 甲级 1036 Boys vs Girls (25 分)(简单题)

1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contai

PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, y

【PAT甲级】1101 Quick Sort (25 分)

题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次??.....这是为何 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[100007]; 5 int b[100007]; 6 int

【PAT甲级】1109 Group Photo (25分)(模拟)

题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序升序将学生从高到矮排列,将学生均分为N行输出,最先输出的一行人数包括除不尽的余数学生,每行中间(如果这一行学生人数为偶数则靠右的为中间)的学生最高,然后依次左边一位学生最高,右边一位学生最高,例如190, 188, 186, 175, 170->175, 188, 190, 186, 170. tric

【PAT甲级】1117 Eddington Number (25分)

题意: 输入一个正整数N(<=100000),接着输入N个非负整数.输出最大的整数E使得有至少E个整数大于E. AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[100007]; 5 int num[100007]; 6 map<int,int>mp; 7 int main(){ 8 ios::sync_with_