【PAT乙级】1024 科学计数法

1.题目描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述

2.题目分析

此题不是很难,知识分的情况比较多,分类讨论时细心点,不要少讨论某系情况。

3.代码演示

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
void a1(int x,int y,string str){ //缩小 
		cout << "0.";
		x--;
		while(x!=0){
			cout << '0';
			x--;
		}
		cout << str[1];
	    for(int i=3;i<=y-1;i++){
		    cout << str[i];
		}
}
void a2(int x,int y,string str){//扩大 
	int i1=0,i;;
	for(int i=3;i<y;i++){
		i1++; //记录小数点后面数的个数 
	}
	if(x>=i1){ //后面直接填0 
		cout << str[1];
		for(int i=3;i<y;i++) cout << str[i];
		int q1=x-i1;
		while(q1!=0){
			cout << 0;
			q1--;
		}
	}
	if(x<i1){//次时需要移动小数点 
	    cout << str[1];
		for(i=3;i<3+x;i++) cout << str[i];
		cout << "."; 
		for(int k=i;k<y;k++) cout << str[k];
	}
} 
void Print(int x,int y,string str){
    if(x==0){
	    for(int i=1;i<y;i++ ) cout << str[i]; //直接输出小数本身
    }
	if(str[y+1]=='-'&&x>0) a1(x,y,str);//缩小
	if(str[y+1]=='+'&&x>0) a2(x,y,str);//扩大
}
int main(){
	string s,s0;
	cin >> s;
	int k=0,s1=0,k1=-1;;
	for(int i=0;i< s.size();i++){
		if(s[i]=='E') {
			k=i;//k很关键,起到定位的作用
			break;
		} 
	}
	for(int j=k+2;j<=s.size();j++) s0[++k1]=s[j];
	int res = atoi(s0.c_str()); //atoi()将E后面的字符串转换整形
	if(s[0]=='+')  Print(res,k,s);//为正
    if(s[0]=='-'){ //为负 
	    cout << s[0];
        Print(res,k,s);
    }
	return 0;
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值