PAT-1024 科学计数法

来源:互联网 发布:java 参数 编辑:程序博客网 时间:2024/06/04 19:26

PAT有个没过,牛客都过了。本来以为是指数为0的问题,然而并不是。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <iomanip>#include <stdlib.h>using namespace std;#define MAX 10000+100int main(){    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    char str[MAX], zhishu[MAX], xiaoshu[MAX];    memset(xiaoshu, '\0', sizeof(xiaoshu));    memset(zhishu, '\0', sizeof(zhishu));    int i = 0, j = 0, f = 0, k = 0; //k是小数中小数点后个位数    cin >> str;    for (i = 1, j = 0; str[i] != 'E'; i++ )    {        xiaoshu[j++] = str[i];        k++;        if (str[i] == '.')            k = 0;    }    i++;    if (str[i] == '-')        f = 1;    for (i++, j = 0; str[i] != '\0'; i++)        zhishu[j++] = str[i];    //指数为负数    if (f == 1)    {        if ('-' == str[0])            cout << "-";        if (atoi(zhishu) == 0)        {            for (i = 0; xiaoshu[i] != '\0'; i++)                cout << xiaoshu[i];        }        else        {            cout << "0.";            for (i = 0; i < atoi(zhishu)-1; i++)                cout << "0";            for (i = 0; xiaoshu[i] != '\0'; i++)                if (xiaoshu[i] != '.')                    cout << xiaoshu[i];        }        cout << endl;    }    //指数为正    else    {        if ('-' == str[0])            cout << "-";        if (atoi(zhishu) == 0)        {            for (i = 0; xiaoshu[i] != '\0'; i++)                cout << xiaoshu[i];        }        else        {            for (i = 0; xiaoshu[i] != '\0'; i++)                if (xiaoshu[i] != '.')                    cout << xiaoshu[i];            for(i = 0; i < atoi(zhishu)-k; i++)                cout << "0";        }        cout << endl;    }    return 0;}


0 0
原创粉丝点击