PAT A1073 Scientific Notation

PAT A1073 Scientific Notation

PAT A1073 Scientific Notation_第1张图片

Sample Input 1:

+1.23400E-03

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000
wore meaning
fractional portion 分数部分
trailing adj. 末尾的
  • 思路1:
    字符串移位模拟:先取出数值子串和指数子串,将指数转化为数字
    1)左移x位:先输出0. 接着输出x-1个0,再把数值子串输出
    2)右移x位:先输出整数部分,跳过小数点,从第2位开始遍历
    a.x未越界(即x < sub.size()-2)先输出x位[2, x+2),输出.,再输出剩余的[x+2, sub.size())
    b.x越界了(即x>=sub.size()-2) 先把全部数值输出[2,sub.size()),再输出0

  • code1:

#include 
#include 
using namespace std;
string s;
void removeL(string s, int x){
	cout << "0.";
	for(int i = 1; i < x; ++i)
		cout << "0";
	for(int i = 0; i < s.size(); ++i)
		if(s[i] != '.') cout << s[i];
}
void removeR(string s, int x){
	cout << s[0];
	if(x < s.size()-2){
		for(int i = 2; i < x+2; ++i)
			cout << s[i];
		cout << '.';
		for(int i = x+2; i < s.size(); ++i)
			cout << s[i];
	}else{
		for(int i = 2; i < x+2; ++i){
			if(i < s.size())
				cout << s[i];
			else 
				cout << "0";
		}
	}
}
int main(){
	cin >> s;
	int e = s.find("E");
	string subV = s.substr(1, e-1);	//只取数值部位 
	string subE = s.substr(e+2);	//TIPS 1:substr()用法 
	int E = stoi(subE); 	//TIPS 2:stoi()用法 
	if(s[0] == '-') cout<< "-"; 	//TIPS 3:erase()用法 
	if(s[e+1] == '-') //左移
		removeL(subV, E);
	else 
		removeR(subV, E);
	return 0;
}
  • TIPS 1: substr()用法

1.substr(str, start, len)str.substr(start, len) :从str的start位置开始提取字符串

2.len:要提取字符串的长度,若length为以下任意条件之一时,返回start位置到串尾的所有字符:
1)不指定 | 2)为空 | 3)负数 | 4)大于start到串尾的长度

  • TIPS 2: stoi()用法

1.头文件#include
2.stoi(const string*) 传入一个string,返回对应的int
3.要包含c++11:在编译器设置里添加-std=c++11

  • TIPS 3: erase()用法

string& erase ( size_t pos = 0, size_t n = npos );
1.使用迭代器
1)s.erase(it); //删除it 2) s.erase(fiest, last); //删除[first, last)
2.使用位置
s.erase(pos, n) //从pos开始删除n个元素

  • T2 code:
#include 
#include 
using namespace std;

void Print(char sign, string v){
	if(sign == '-') printf("-");
	cout << v;
}
int main(){
	string s;
	cin >> s;
	int posE = s.find('E');
	char sign_v = s[0], sign_e = s[posE+1]; 
//	cout << sign_v << " " << sign_e <
	string v = s.substr(1, posE-1);
	int e = stoi(s.substr(posE + 2));
//	cout << v << " " << e;
	v.erase(1, 1);
	if(sign_e == '-'){
		while(e--) v.insert(0, "0") ;
		v.insert(1, ".");
	}else{
		int pos_point = 1 + e;
		if(pos_point < v.size()){
			v.insert(pos_point, ".");
		}else{
			int len = pos_point-v.size();
			for(int i = 0; i < len; ++i) v += '0';
		}	
	}
	Print(sign_v, v);
	return 0;
}
  • T3 code:
#include 
using namespace std;

void L(string s, int e)
{
    int len_frac = s.size() - 1;
    if(e < len_frac) s.insert(e+1, ".");
    else
    {
        string tmp(e - len_frac, '0');
        s += tmp;
    }
    cout << s;
}
void R(string s, int e)
{
    while(e != 0)
    {
        s.insert(0, "0");
        e++;
    }
    cout << "0." << s;
}
int main()
{
    string num;
    cin >> num;
    int pos_e = num.find('E');
    int e = stoi(num.substr(pos_e + 1));
    num.erase(pos_e);
    if(num[0] == '-') printf("-");
    num.erase(0, 1);
    num.erase(1, 1);
    if(e > 0) L(num, e);
    else R(num, e + 1);
    return 0;
}

  • T4 code:
#include 
using namespace std;
const int maxn = 10010;
char num[maxn];

void LeftMove(int e)
{
    printf("0.");
    while(--e) printf("0");
    printf("%s", num);
}
void RightMove(int e)
{
    int num_frac = strlen(num) - 1;
    if(e < num_frac)
    {
        for(int i = 0; i < strlen(num); ++i)
        {
            printf("%c", num[i]);
            if(i == e) printf(".");
        }
    }else
    {
        printf("%s", num);
        int d = e - num_frac;
        while(d--) printf("0");
    }
}
void Change(char s[])
{
    int idex = 0;
    for(int i = 1; i < strlen(s); ++i)
    {
        if(s[i] != '.') s[idex++] = s[i];
    }
    s[strlen(s)-2] = '\0';
}
int main()
{
    fgets(num, maxn, stdin);
    int len = strlen(num), e;
    char* pe;
    for(int i = len-1; i >= 0; --i)
    {
        if(num[i] == 'E')
        {
            pe = num + i + 1;
            num[i] = '\0';
            break;
        }
    }
//    sscanf(pe, "%d", &e);
    e = atoi(pe);
    if(num[0] == '-') printf("-");
    Change(num);
    if(e < 0)
    {
        LeftMove(-e);
    }else
    {
        RightMove(e);
    }
    return 0;
}

你可能感兴趣的:(PAT,A)