PAT:1060. Are They Equal (25) AC

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

int  n;

string deal(string s,int &e)          //【思维】1:吸收多余0:。2:找到“.”判断与1的大小。3:去除“.”的同时统计10的指数(正负与step2有关)
{                        //4:判断是否删完了,删完了表明数字是0。5:存入前n个数字,不足用0补。返回
  int k=0;
  while(s.size()>0 && s[0]==‘0‘)        //吸收.前面多余的0
    s.erase(s.begin());
  if(s[0]==‘.‘)                //Situation1:数字小于1
  {
    s.erase(s.begin());            //删除“.”
    while(s.size()>0 && s[0]==‘0‘)      //吸收.前面多余的0
    {
      s.erase(s.begin());
      --e;                //指数为负
    }
  }
  else                    //Situation2:数字大于1
  {
    while(k<s.size() && s[k]!=‘.‘)      //数小数点前有几位
    {
      ++k;
      ++e;
    }
    if(k<s.size())              //删除“.”
      s.erase(s.begin()+k);
  }
  if(s.size()==0)                //这个数为0
    e=0;
  k=0;                    //下标归零
  int num=0;
  string str;
  while(num<n)                //将前n位有效数字存入str
  {
    if(k<s.size())
      str+=s[k++];            //后面接上
    else
      str+=‘0‘;
    ++num;
  }
  return str;
}

int main()
{
  string s1,s2,s3,s4;
  cin>>n>>s1>>s2;
  int e1=0,e2=0;      //s1,s2的指数

  s3=deal(s1,e1);
  s4=deal(s2,e2);

  if(s3==s4 && e1==e2)
    cout<<"YES 0."<<s3<<"*10^"<<e1<<endl;
  else
    cout<<"NO 0."<<s3<<"*10^"<<e1<<" 0."<<s4<<"*10^"<<e2<<endl;
  return 0;
}
时间: 2024-08-09 06:34:33

PAT:1060. Are They Equal (25) AC的相关文章

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

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:1012. The Best Rank (25) AC

#include<stdio.h> #include<algorithm> using namespace std; struct Student { int mID; int grade[4]; //0对应平均A,1对应C,2对应M,3对应E }STU[2010]; char course[4]={'A','C','M','E'}; //所有的存储都对应ACME int Rank[10000000][4]={0}; //每个学号四个成绩对应的排名 int now=0; //排序的

PAT:1086. Tree Traversals Again (25) AC

#include<stdio.h> #include<string.h> #include<stack> using namespace std; const int MAX=50; int n,cnt=0; //n个节点,cnt在后序输出的时候控制空格数量用 int PRE[MAX],IN[MAX]; //先序,中序 int preI,inI; //先序,中序的下标 stack<int> S; //栈 struct node { int data; nod

PAT:1009. Product of Polynomials (25) AC

#include<stdio.h> #include<stdlib.h> #include<string.h> //[warning]double 输入%lf,输出%f struct arr { int exp; //指数 double cof; //系数 }arr[1005]; double ans[2010]; //下标是指数,内容是系数 int main() { memset(arr,0,sizeof(arr)); memset(ans,0,sizeof(ans)

PAT:1013. Battle Over Cities (25) AC

#include<stdio.h> #include<string.h> #include<vector> using namespace std; const int MAX=1010; int n,m,k; //城市数,高速公路数,查询次数 int DELETE; //要删除的点 vector<int> ADJ[MAX]; //邻接表 bool vis[MAX]; void DFS(int s) { if(DELETE==s) return; //表示该

PAT:1052. Linked List Sorting (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int address; int data; int next; bool tag; }Node[100066]; bool cmp(node a,node b) { if(a.tag!=b.tag) return a.tag>b.tag; else return a.data<

PAT:1010. 一元多项式求导 (25) AC

#include<stdio.h> #include<stdlib.h> #include<algorithm> using namespace std; int main() { int arr[2111]; fill(arr,arr+2111,0); int n=0,tmp; while(scanf("%d",&tmp)!=EOF) //存储系数和指数,下标从0开始偶数为系数,奇数为指数 arr[n++]=tmp; for(int i=0

PAT 1060. Are They Equal (25)

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.123*105 with simple chopping. Now given the number of significant digits on a machine a