PAT icon indicating copy to clipboard operation
PAT copied to clipboard

[Basic/C++/1024] bug report

Open lliding opened this issue 6 years ago • 1 comments

bug 出现在这里的第 17 到 25 行。有如下输入输出

+0.111111111E+5 (input 011111.1111 (output

题目要求:按普通数字表示法输出 A。但是上面输出的显然不是一个正常的普通数字。

PTA 官网可以编译通过,但是显然它没考虑到这个问题.......

可将这部分代码所做的输出全部做连接,最后用循环从第一个有效数字开始输出成这样的:

+0.111111111E+5 (input 11111.1111 (output

lliding avatar Apr 03 '19 09:04 lliding

这是原来的

cout << num[0];
int cnt, j;
for (j = 2, cnt = 0; j < num.length() && cnt < exponent; j++, cnt++) cout << num[j];
if (j == num.length()) {
    for (int k = 0; k < exponent - cnt; k++) cout << '0';
} else {
    cout << '.';
    for (int k = j; k < num.length(); k++) cout << num[k];
}

这是我修过的

string ns = "";
ns = ns + num[0];
int cnt, j;
for (j = 2, cnt = 0; j < num.length() && cnt < exponent; j++, cnt++) ns = ns + num[j];
if (j == num.length()) {
    for (int k = 0; k < exponent - cnt; k++) ns = ns + '0';
} else {
    ns = ns + '.';
    for (int k = j; k < num.length(); k++) ns = ns + num[k];
}
int flag = 0;
for (int i = 0; i < ns.length(); ++i) {
    if (ns[i] != '0') flag = 1;
    if (flag == 1) cout << ns[i];
}

lliding avatar Apr 03 '19 09:04 lliding