9102 计算2的N次方
Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others)
Total Submissions: 315 Accepted Submissions: 97
Problem Description
任意给定一个正整数N(N<=100),计算2的N次方的值。
Input
输入有多行,每行只有一个正整数N。
Output
输出2的N次方的值。
Sample Input
5
1
Sample Output
32
2
#include <stdio.h>
#include <string.h>
int main()
{
int n,carry,len;
char res[100],temp[100];
while(~scanf("%d",&n))
{
if(n==0) printf("1\n");
else
{
temp[0] = '1';
temp[1] = '\0';
memset(res,0,sizeof(res));
int p;
while(n--)
{
carry = 0;
len = strlen(temp);
int j = 0,i = 0;
for(;i<len;i++)
{
res[j++] = ((temp[i]-'0')*2+carry)>=10 ? ((temp[i]-'0')*2+carry-10+'0') : ((temp[i]-'0')*2+carry+'0');
carry = ((temp[i]-'0')*2+carry)>=10 ? 1 : 0;
}
if(carry==1)
{
res[j++] = '1';
res[j] = '\0';
}
else res[j] = '\0';
strcpy(temp,res);
res[j] = 0;
p = j;
}
res[p] = '\0';
for(int k = strlen(res)-1;k>=0;k--)
{
printf("%c",res[k]);
}
printf("\n");
}
}
return 0;
}