Power

Time Limit: 1sec    Memory Limit:256MB
Description

 You should write a program to calculate the power of 3. 

For example, given a positive integer n, you should calculate 3^n. 
 
Input
The input contains a single positive integer n(0 < n <= 200)
 
Output

 An integer.

 
Sample Input
 Copy sample input to clipboard
48
Sample Output
79766443076872509863361


Problem Source: try





题解:此题数据庞大,即使用long long 存储,也必然溢出。这时候便可用【数组模拟】来过题。



本宝宝的代码:

06. #include<iostream>
07. using namespace std;
08.  
09. int main()
10. {
11. int a[1000]={0};
12. int b[1000]={0};
13. int c[1000]={0};
14. int N;
15. cin >>N;
16. a[0]=1;
17. int i,j;
18. for(i=1;i<=N;i++)
19. {
20. for(j=0;j<i;j++)
21. {
22. b[j]=(c[j]+(a[j]*3))%10;
23. c[j+1]=(c[j]+(a[j]*3))/10;
24. a[j]=b[j];
25. }
26. //cout <<"第" << i <<" 次循环:" <<a[24] <<a[23] <<a[22]
27. //<<a[21] <<a[20]
28. //<<a[19] <<a[18] <<a[17]
29. //<<a[16] <<a[15] <<a[14] <<a[13] <<a[12]
30. //<<a[11] <<a[10] <<a[9] <<a[8] <<a[7]
31. //<<a[6] <<a[5] <<a[4] <<a[3]
32. //<<a[2] <<a[1] <<a[0]<<endl;
33. }
34. int pos;
35. for(pos=N;pos>=1;pos--)
36. {
37. if(a[pos]!=0)
38. {
39. break;
40. }
41. }
42. for(int j=pos;j>=0;j--)
43. {
44. cout<<a[j];
45. }
46. cout <<endl;
47. return 0;
48. }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值