poj1001这题坑太多,套路很深,宝宝心里苦

宝宝心里苦,宝宝高中做OI到现在都四年多了,宝宝还要被字符串折磨,宝宝还要想好多好多细节,宝宝看着自己全对的的测试数据看到WA是懵逼的,宝宝还TM能看到超时也是蛮醉的,宝宝写完这道题今天都不想撸码了,宝宝要看片片。。。

其实宝宝看到自己160k,0ms AC还是很开心的嗯。。。


先说下想起来的这里面的坑:

1.指数可以是1,但是这时候输出结果要去掉底数中不必要的0

即 1.0000 1->1 而不是 1.0000

2.宝宝用的指数降幂的方法,就是各种平方,平方的平方这种,但是考虑到最高25次其实效果应该不是很明显,GG

3.宝宝傻了,宝宝降幂的时候一直搞错,所以大家如果也用这种方法的话,如果脑子算不过来,像宝宝一样,最好还是举个例子自己代一下,比如2的5次方啊什么的,效果明显

4.把字符串倒来倒去就会乱,一定一定一定要记清楚。。。


宝宝的思路就是把数当作字符串按手算的方法做乘法,比较重要的是要把字符串倒序这样方便处理进位

然后用指数降幂的方法减少乘法的次数,比如2^12一般说要进行11次乘法,那我看作(2^2*2)^2^2就只需要4次乘法,虽然最高25次效果不明显,但还是有优化的

然后处理数字的不必要的零可以在计算前处理也可以在计算后处理,计算器按处理的话会减少很多浪费,但是宝宝一开始没有想到,后来就懒得改的


然后宝宝对自己的AC还是很开心,160K0MS还是很棒的好么

代码如下

#include <iostream>
#include <string>
//#include <ctime>
using namespace std;

string invt(string str){
	string str_inv = str;
	int str_len = str.length();
	for (int str_i = 0; str_i < str_len; str_i ++)
		str_inv[str_i] = str[str_len-1-str_i];
	return str_inv;
}
	
//字符串x,y相乘的计算函数
string multi(string x_m, string y_m){
	int x_l = x_m.length();
	int x_p = x_m.find('.');//表示有几位小数
	if (x_p!=string::npos){
		x_m = x_m.erase(x_p,1);
		x_l -= 1;
	}
	else x_p = 0;
	int y_l = y_m.length();
	int y_p = y_m.find('.');//表示有几位小数
	if (y_p!=string::npos){
		y_m = y_m.erase(y_p,1);
		y_l -= 1;
	}
	else y_p = 0;
	
	int i,j;
	int temp;
	string ans(x_l+y_l+1,'0');
	for (i = 0; i < x_l; i ++)
		for (j = 0; j < y_l; j ++){
			temp = (x_m[i]-'0')*(y_m[j]-'0');
			ans[i+j] += temp%10;
			ans[i+j+1] += temp/10;
		}
	for (i = 0; i < x_l+y_l; i ++){
		ans[i+1] += (ans[i]-'0')/10;
		ans[i] = (ans[i]-'0')%10+'0';
	}
	if (x_p+y_p){
		string str1 = ans.substr(0,x_p+y_p);
		string str2 = ans.substr(x_p+y_p);
		ans = str1+'.'+str2;
	}
	if (x_p+y_p){
		while(ans[0]=='0')
			ans = ans.substr(1);
	}
	if (ans[0]=='.')
		ans = ans.substr(1);
	i = ans.length();
	while (ans[i-1]=='0'){
		ans = ans.erase(i-1);
		i = ans.length();
	}
	
	return ans;
}

string power(string x_p, int exp){
	//指数降次函数
	if (exp == 1)
		return multi(x_p,"1");
	if (exp == 2)
		return multi(x_p,x_p);
	else if (!exp%2)
		return power(power(x_p,2),exp/2);
	else
		return multi(power(x_p,exp-1),x_p);
}

int main()
{
	string s;
	int n;
	//clock_t start, end;
	while (cin >> s >> n){
		//start = clock();
		cout << invt(power(invt(s),n)) << endl;
		//end = clock();
		//cout << end-start << endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25. 输入说明 The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9. 输出说明 The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 If you don't know how to determine wheather encounted the end of input: s is a string and n is an integer C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work */ { ... } 来源 East Central North America 1988 北大OJ平台(代理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值