PAT (Advanced Level) 1060 Are They Equal

题解

  找出有效的字符串(t),第一个非零数字的位置(zero)和小数点的位置(point)。

    若 s = "0012.104654",N = 4 则 t = "12104654",zero = 2,point = 4,所以答案为 0.1210*10^(point-zero)

    若 s = "0000.00010465",N = 4 则 t = "10465",zero = 8,point = 4,所以答案为 0.1046*10^(point-zero+1)

    注意 s = "0.0000000"的情况,若 N = 3,则答案为0.000*10^0

代码

#include<bits/stdc++.h>
using namespace std;
string process(int N,string s,int & k);
int main()
{
    int i,N,k1,k2;
    string str1,str2;
    cin>>N>>str1>>str2;

    str1=process(N,str1,k1);
    str2=process(N,str2,k2);

    if(str1==str2 && k1==k2)    printf("YES %s*10^%d",str1.c_str(),k1);
    else    printf("NO %s*10^%d %s*10^%d",str1.c_str(),k1,str2.c_str(),k2);

    system("pause");
    return 0;
}
string process(int N,string s,int & k)
{
    int i,zero,point;
    string t;
    zero=point=-1;
    for(i=0;i<s.size();i++)
    {
        if(s[i]==‘.‘)   point=i;
        else if(s[i]==‘0‘ && zero==-1)  continue;
        else
        {
            if(zero==-1)    zero=i;
            t+=s[i];
        }
    }

    if(zero==-1)
    {
        t.insert(0,N,‘0‘);
        k=0;
        return "0."+t;
    }
    else
    {
        if(point==-1) point=i;
        if(t.size()<N) t.insert(t.size(),t.size()-N,‘0‘);
        if(zero>point)  k=point-zero+1;
        else    k=point-zero;
        return "0."+t.substr(0,N);
    }
}

原文地址:https://www.cnblogs.com/VividBinGo/p/12233093.html

时间: 2024-10-24 07:38:45

PAT (Advanced Level) 1060 Are They Equal的相关文章

PAT (Advanced Level) 1060. Are They Equal (25)

模拟题.坑点较多. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace

Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码 题目描述: 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.123*105 with simple chopping. Now given the number of significant digits on a machine and two flo

PAT Advanced Level 1053 Path of Equal Weight

1053 Path of Equal Weight (30)(30 分) Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf

PAT (Advanced Level) 1053. Path of Equal Weight (30)

简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; const int maxn=100+10;

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C