PAT甲级1060——Are They Equal

#include<iostream>
#include<string>
using namespace std;
int getExp(string &num,int n){
	int e=0;
	int i=0,j=0;
	while(num[i]=='0')	num.erase(i,1);      
	if(i<num.size()&&num[i]=='.'){      //0-1间(不含1);
		num.erase(i,1);
		while(i<num.size()&&num[i]=='0'){
			--e;
			num.erase(i,1);
		}
		while(j<n&&i<num.size()){
			++i;++j;
		}
	}else if(i<num.size()){     //大于1;
        int tmp=i;
		while(tmp<num.size()&&num[tmp]!='.'){  //获取指数;
            ++e;
            ++tmp;
        }
        if(tmp<num.size()) num.erase(tmp,1);//删除小数点;
        while(j<n&&i<num.size()){
            ++i;++j;
        }
	}
	//特判0000形式;
	if(num.size()==0){
		num="";
		e=0;
	}else if(i<num.size()) num.erase(i);        //记得删除多余的数字
	while(j<n){                   //数字位数不足补0;
			num+='0';
			++j;
	}
	return e;
}
int main(){
	int N;
	scanf("%d",&N);
	string A,B;
	cin>>A>>B;
	int e1,e2;
	string n1,n2;
	e1=getExp(A,N);
	e2=getExp(B,N);
	if(A==B&&e1==e2)
		printf("YES 0.%s*10^%d",A.c_str(),e1);
	else printf("NO 0.%s*10^%d 0.%s*10^%d",A.c_str(),e1,B.c_str(),e2);
	system("pause");
	return 0;
}

开始时用一个新的字符串保存有效位,出现了内存超限的问题,所以改用在原字符串上做修改的方法,期间采用erase()删除无用的数字。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值