题目:http://icpc.njust.edu.cn/Problem/Pku/1001/
AC代码(C++):
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <iomanip>
#include <string.h>
using namespace std;
int main(void) {
char sample[7];
int sn;
int cn = 0;
char rs[50][150];
int ptn[50];
for (int i = 0; i < 50; i++)ptn[i] = -1;
while (cin >> sample >> sn) {
reverse(sample, sample + 6);
int pt = 0;
for (; sample[pt] != '.'; pt++)if (pt == 5 && sample[pt] != '.') {
pt = 0;
break;
}
ptn[cn] = pt*sn - 1;
if (pt != 0)for (int i = pt; i < 6; i++)sample[i] = sample[i + 1];
for (int i = 0; i < 150; i++)rs[cn][i] = '\0';
if (sn == 0) {
cout << 1 << endl;
continue;
}
if (sn == 1) {
for (int j = 0; sample[j] != '\0'; j++) {
rs[cn][j] = sample[j];
if (sample[j + 1] == '\0')rs[cn][j + 1] = '\0';
}
}
for (int i = 1; i < sn; i++) {
char tr[6][150];
for (int j = 0; j < 150; j++)for (int k = 0; k < 6; k++)tr[k][j] = '\0';
if (i == 1)for (int j = 0; sample[j] != '\0'; j++) {
rs[cn][j] = sample[j];
if (sample[j + 1] == '\0')rs[cn][j + 1] = '\0';
}
for (int j = 0; sample[j] != '\0'; j++) {
int t = 0;
for (int k = 0; rs[cn][k] != '\0'; k++) {
tr[j][k] = ((sample[j] - '0')*(rs[cn][k] - '0') + t) % 10 + '0';
t = ((sample[j] - '0')*(rs[cn][k] - '0') + t) / 10;
if (rs[cn][k + 1] == '\0'&&t != 0)tr[j][k + 1] = t + '0';
}
}
int tt = 0;
for (int j = 0; j < 150; j++) {
if (tt != 0 ||
tr[0][j] != '\0' ||
(j >= 1 && tr[1][j - 1] != '\0') ||
(j >= 2 && tr[2][j - 2] != '\0') ||
(j >= 3 && tr[3][j - 3] != '\0') ||
(j >= 4 && tr[4][j - 4] != '\0') ||
(j >= 5 && tr[5][j - 5] != '\0'))rs[cn][j] = '0';
else break;
if (tr[0][j] != '\0') {
rs[cn][j] = (tr[0][j] - '0' + tt) % 10 + '0';
tt = (tr[0][j] - '0' + tt) / 10;
}
else {
rs[cn][j] = tt % 10 + '0';
tt = tt / 10;
}
if (j >= 1 && tr[1][j - 1] != '\0') {
int tmp = rs[cn][j] - '0' + tr[1][j - 1] - '0';
rs[cn][j] = tmp % 10 + '0';
tt += tmp / 10;
}
if (j >= 2 && tr[2][j - 2] != '\0') {
int tmp = rs[cn][j] - '0' + tr[2][j - 2] - '0';
rs[cn][j] = tmp % 10 + '0';
tt += tmp / 10;
}
if (j >= 3 && tr[3][j - 3] != '\0') {
int tmp = rs[cn][j] - '0' + tr[3][j - 3] - '0';
rs[cn][j] = tmp % 10 + '0';
tt += tmp / 10;
}
if (j >= 4 && tr[4][j - 4] != '\0') {
int tmp = rs[cn][j] - '0' + tr[4][j - 4] - '0';
rs[cn][j] = tmp % 10 + '0';
tt += tmp / 10;
}
if (j >= 5 && tr[5][j - 5] != '\0') {
int tmp = rs[cn][j] - '0' + tr[5][j - 5] - '0';
rs[cn][j] = tmp % 10 + '0';
tt += tmp / 10;
}
}
}
for (int j = 0; rs[cn][j] != '\0'; j++) {
if (rs[cn][j] != '0')break;
else if (j <= ptn[cn])rs[cn][j] = '*';
}
bool flag = false;
for (int j = 149; j >= 0; j--) {
if (j - 1 == ptn[cn] && flag == false && rs[cn][j] == '0')continue;
if (j == ptn[cn] && rs[cn][j - 1] != '\0'&&rs[cn][j - 1] != '*')cout << '.';
if (rs[cn][j] != '\0'&&rs[cn][j] != '*') {
cout << rs[cn][j];
flag = true;
}
}
cout << endl;
cn++;
}
//system("pause");
}
总结:1.对每个输入都要马上输出而不是最后一起输出
2.注意结果小于1去掉开头的0;去掉小数点后多余的0
3.注意指数等于0或1的情况