trick:
1、前导0
如:000001,000.000001
2、出现0时也要按照科学计数法输出
e.g. 4 00000.00000 0001
NO 0.0000*10^0 0.1*10^1
3、小于0.1的情况 0.00000001
#include<iostream> #include<cstring> #include<cstdio> using namespace std; char s1[105],s2[105]; char a1[105],a2[105]; int n; int main() { int n,i,l1,l2,p1=0,p2=0,flag=0,t,cnt,ls1,ls2; while(~scanf("%d %s %s",&n,s1,s2)) { p1=ls1=0; l1=strlen(s1); for (;ls1<l1;++ls1) { if(s1[ls1]=='.') p1=ls1;//点位置 else if (s1[ls1]!='0') break; } if(ls1==l1){//为0 // sprintf(a1,"0"); a1[0]='0',a1[1]='.',t=2; for (i=0;i<n;++i) a1[t++]='0'; sprintf(a1+t,"*10^0"); } else{ a1[0]='0',a1[1]='.',t=2; if (p1==0)//此为大于1的数 { for (p1=ls1;p1<l1&&s1[p1]!='.';++p1); for(i=ls1,cnt=0;i<l1&&cnt<n;++i) if(s1[i]!='.'){ a1[t++]=s1[i]; cnt++; } while(cnt<n){ a1[t++]='0'; cnt++; } sprintf(a1+t,"*10^%d",p1-ls1); } else{//此为小于1的数 for(i=ls1,cnt=0;i<l1&&cnt<n;++i) if(s1[i]!='.'){ a1[t++]=s1[i]; cnt++; } while(cnt<n){ a1[t++]='0'; cnt++; } sprintf(a1+t,"*10^%d",-(ls1-p1-1)); } } p2=ls2=0; l2=strlen(s2); for (;ls2<l2;++ls2) { if(s2[ls2]=='.') p2=ls2;//点位置 else if (s2[ls2]!='0') break; } if(ls2==l2){// a2[0]='0',a2[1]='.',t=2; for (i=0;i<n;++i) a2[t++]='0'; sprintf(a2+t,"*10^0"); } else{ a2[0]='0',a2[1]='.',t=2; if (p2==0)//此为大于1的数 { for (p2=ls2;p2<l2&&s2[p2]!='.';++p2); for(i=ls2,cnt=0;i<l2&&cnt<n;++i) if(s2[i]!='.'){ a2[t++]=s2[i]; cnt++; } while(cnt<n){ a2[t++]='0'; cnt++; } sprintf(a2+t,"*10^%d",p2-ls2); } else{//此为小于1的数 for(i=ls2,cnt=0;i<l2&&cnt<n;++i) if(s2[i]!='.'){ a2[t++]=s2[i]; cnt++; } while(cnt<n){ a2[t++]='0'; cnt++; } sprintf(a2+t,"*10^%d",-(ls2-p2-1)); } } if (strcmp(a1,a2)==0) printf("YES %s ",a1); else printf("NO %s %s ",a1,a2); } return 0; } //4 000.001001 10.01 //3 00000.00001 1.00001