POJ 1001.Exponentiation

题目: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的情况

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值