记录PTA甲级以及C++部分语法知识1060*-标准记数法的字符串处理

这道题我自己写的时候问题很多,很多情况都没有考虑,还是贴一下有机会改改错误:

另外,我好像发现printf不能用于输出string 对象。

#include<iostream>
#include<string>

using namespace std;

int main(){
	int N;
	char num[101];
	string A,B;
	scanf("%d",&N);
	scanf("%s",num);
	A=num;
	scanf("%s",num);
	B=num;

	while(A[0]=='0') A.erase(0,1);
	if(A[0]=='.') A="0"+A;
	while(B[0]=='0') B.erase(0,1);
	if(B[0]=='.') B="0"+B;



	int lena= (A.find(".")==-1)? A.length():A.find(".");
	int lenb= (B.find(".")==-1)? B.length():B.find(".");

	string a=A.substr(0,N);
	string b=B.substr(0,N);
	

	if(lena==lenb && a==b)
		cout<<"Yes 0."<<a<<"*10^"<<lena<<endl;
	else
		cout<<"NO 0."<<a<<"*10^"<<lena<<" 0."<<b<<"*10^"<<lenb<<endl;
	system("pause");
	return 0;
}

找了一版同样接收string,代码参考:https://blog.csdn.net/qq_34801341/article/details/81436792

可以满分,而且考虑到了所有想要考察的地方,但是逻辑上还有一个漏洞,比如说:3 123456 12.34这个输入给了Yes。

#include <string>
#include <iostream>
using namespace std;
int N;
void getK(string  &num,int &n){//标准化处理产生N位不含小数点的num和指数n
	while(num[0] == '0')  num.erase(num.begin());
	if(num.length() == 0){ //处理0
		num = "0";
		n = 0;
		return;
	}
	else if(num[0] == '.'){  //处理小于1的数
		num.erase(num.begin());
		n=0;
		while(num[0]=='0'){
			n--;//指数也可能是负的
			num.erase(num.begin());
		}
		if(num.length() == 0){//00.00的情况
			n=0;
			num = "0";
		}
		return;
	}
	else{    //处理大于1的数
		int local = num.find(".");
		if(local == string ::npos){  //如果这个数字没有小数部分
			n = num.length();
			return ;
		}
		else{   //处理混合型的数组
			n = local;
			num.erase(num.begin()+local);//去掉小数点
			return;
		}
	}
}
 
void getN(string &num){
	while(num.length()<N){
		num+="0";//长度小于N在其后加零
	}
}
bool compare(string &num1,string &num2){
	getN(num1);
	getN(num2);
	num1= num1.substr(0,N);
	num2 = num2.substr(0,N);
	if( num1 == num2 ){
		cout<<"YES ";
		return true;
	}else{
		cout<<"NO ";
		return false;
	}
}
 
int main(){
	
	string num1,num2;
	cin>>N>>num1>>num2;
	int e1,e2;
	getK(num1,e1);
	getK(num2,e2);
	if(compare(num1,num2)&&e1==e2){
		//cout<< "0."<<num1<<"*10^"<<e1<<" ";
		cout<< "0."<<num2<<"*10^"<<e2 <<endl;
	}else{
		cout<< "0."<<num1<<"*10^"<<e1<<" ";
		cout<< "0."<<num2<<"*10^"<<e2<<endl;
	}
 
 
	system("pause");
	return 0;
}

再记录一版超简洁的:https://blog.csdn.net/u014646950/article/details/47455685

#include<iostream>  
#include<string>
using namespace std;  
void getmachine(int*e, string *str,int N)
{
  int dot = (*str).find('.')  ; 
  if (dot != -1){//去除小数点
    str->erase(dot, 1);
      (*e) = dot; //指数
  }
  else(*e) = str->size(); //指数
  str->insert(0, "0.");//标准化
  dot = 2;

  while (str->size() > dot && (*str)[dot] == '0')
    dot++;
  dot -= 2;
  str->erase(2, dot);
  (*e) -= dot;//去除开头的0

  dot = str->size();
  if (dot == 2)(*e) = 0;//输入为0的情况

  N += 2;
  if (dot >  N)str->erase(N, dot - N +1);//去除多余数位
  else str->insert(dot, N - dot, '0');//不足则填充零
}
int main()
{      
  string str[2];
  int e[2];
  int N;
  cin >> N >> str[0] >> str[1];
  getmachine(&e[0], &str[0],N);
  getmachine(&e[1], &str[1],N);
  if (e[0] == e[1] && str[0] == str[1])cout << "YES " << str[0] << "*10^" << e[0] << endl;
  else cout << "NO " << str[0] << "*10^" << e[0] << " " << str[1] << "*10^" << e[1] << endl;
  system("pause");
  return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值