9102 计算2的N次方

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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值