1170:计算2的N次方
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 4537 通过数: 2372
【题目描述】
任意给定一个正整数N(N≤100),计算2的n次方的值。
【输入】
输入一个正整数N。
【输出】
输出2的N次方的值。
【输入样例】
5
【输出样例】
32
【来源】
No
#include<bits/stdc++.h>
using namespace std;
int a[1000]={0};
int main(){
a[999]=1;
int n,b=0;
cin>>n;
for(int i=1;i<=n;++i){
int j=999,temp;
while(j>=0){
temp=a[j]*2+b;
a[j]=temp%10;
b=temp/10;
j--;
}
}
int index;
for(index=0;index<1000;index++)
if(a[index]){
break;
}
for(;index<1000;index++)
cout<<a[index];
cout<<endl;
return 0;
}
模拟高精度乘法,一把给A掉了
#include<bits/stdc++.h>
using namespace std;
int c[111];
int main(){
c[1]=1;
int n;
cin>>n;
while(n--){
int temp=0;
int i;
for(i=1;i<=100;i++){
c[i]=2*c[i]+temp;
temp=c[i]/10;
c[i]=c[i]%10;
}
//c[i]=temp;
}
int index=0;
for(int i=100;i>0;i--){
if(c[i]){
index=i;
break;
}
}
for(int i=index;i>0;i--)
cout<<c[i];
cout<<endl;
return 0;
}
走一波优化,height记录最高位
#include<bits/stdc++.h>
using namespace std;
int c[111];
int main(){
c[1]=1;
int n;
cin>>n;
int height=1;
while(n--){
int temp=0;
int i;
for(i=1;i<=height;i++){
c[i]=2*c[i]+temp;
temp=c[i]/10;
c[i]=c[i]%10;
}
while(temp){
c[++height]=temp%10;
temp/=10;
}
}
for(int k=height;k>0;k--)
cout<<c[k];
return 0;
}