输入一个整数N(512 <= N <= 1024)
2的N次方的十进制结果
512
13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096
#include <string> #include <iostream> using namespace std; string NPowerOf2(int n){ string res("1"); for( int i =0;i < n; ++i){ int carry = 0, multi= 0; for(int j = res.size( )-1; j >= 0; --j){ int tmpNum = res[j] - '0'; multi= (tmpNum<<1) + carry; carry = multi/10; res[ j] = (multi%10) + '0'; } if( carry> 0) res.insert(res.begin( ), carry+'0'); } return res; } int main( ) { int n; while( cin>>n) cout<<NPowerOf2(n)<<endl; return 0; }
print(2 ** int(input()))
使用math/big库
package main
import (
"fmt"
"math/big"
)
func main() {
var (
input int64
i *big.Int
n *big.Int
)
i = big.NewInt(2)
fmt.Scan(&input)
n = big.NewInt(input) // 将输入的int64格式整数转为bigint类型
i.Exp(i, n, nil) // 计算i的n次幂
fmt.Println(i)
}
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); String ans = compute(n); System.out.println(ans); } } private static String compute(int n){ String ans = ""; if(n == 1) return "2"; String temp = compute(n/2); if(n%2==0){ ans = multiply(temp, temp); }else{ ans = multiply(multiply(temp, temp), "2"); } return ans; } private static String multiply(String str1, String str2){ int len1 = str1.length(); int len2 = str2.length(); int[] ans = new int[len1+len2]; for(int i=len1-1; i>=0; i--){ for(int j=len2-1; j>=0; j--){ int mul = (str1.charAt(i)-'0')*(str2.charAt(j)-'0'); int p1 = i+j, p2 = i+j+1; int sum = mul+ans[p2]; ans[p1] += sum/10; ans[p2] = sum%10; } } StringBuffer res = new StringBuffer(); for(int value:ans) if(!(res.length() == 0 && value == 0)) res.append(value); return (res.length() == 0)? "0": res.toString(); } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; string res="1"; for(int i=0;i<n;i++) { int carry = 0, multi= 0; for(int j=res.size()-1;j>=0;j--) { int tmpNum = res[j] - '0'; multi= (tmpNum<<1) + carry; carry = multi/10; res[ j] = (multi%10) + '0'; } if( carry> 0) res.insert(res.begin( ), carry+'0'); } cout<<res<<endl; return 0; }
import java.util.Scanner;
public class Main {
public static void main( String[] args ) {
Scanner sc = new Scanner( System.in );
while( sc.hasNextInt() ) {
int n = sc.nextInt();
StringBuilder res = new StringBuilder( "1" );
for( int i = 1; i <= n; i ++ ) {
int len = res.length(); int tmp = 0;
for( int j = len-1; j >= 0; j -- ) {
tmp = ( ( res.charAt(j)-'0' ) << 1 ) + tmp/10;
res.setCharAt( j, (char)(tmp%10+'0') );
}
if( tmp/10 == 1 ) res.insert( 0, '1' );
}
System.out.println( res );
}
}
}
#include <iostream> #include <cstring> using namespace std;int main() { int n; cin >> n; string s = "1"; for (int i = 0; i<n; ++i) { int carry = 0; int size = s.size(); for (int j=0;j<size; ++j) { int temp=s[j]-'0'; temp *= 2; temp += carry; carry = temp / 10; s[j]=temp%10+'0'; if (carry>0&&j==s.size()-1) { s.insert(s.end(), carry + '0'); carry = 0; } } } for (int k=s.size()-1;k>=0;k--) { cout << s[k]; } return 0; }
#include <iostream>#include <stdio.h>usingnamespacestd;intmain() {intn = 0;cin >> n;string s("1");intcarry = 0;for(inti=0; i<n; i++) {for(intj=s.size()-1; j>=0; j--) { //从后向前*2inttmp = s[j] - '0';tmp *= 2;tmp += carry;if(tmp >= 10) {carry = tmp/10; // 进位tmp = tmp%10;s[j] = tmp + '0';if(j == 0) {// 需要额外开个空间,比如512*2charc[2];sprintf(c, "%d", carry);s.insert(0, c);carry = 0;}} else{s[j] = tmp + '0';carry = 0;}}}cout << s << endl;return0;}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
int n = 0, temp = 0;
Scanner sc = new Scanner(System.in);
Boolean carry = false;//进位
list.add(1);//初始值为1
n = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = list.size() - 1; j >= 0; j--) {//从后往前计算
temp = list.get(j);
list.set(j, (temp * 2) % 10);//每一位乘以2
if (carry)//有进位的需要加一
list.set(j, (temp * 2) % 10 + 1);
if (temp < 5)//小于5的乘以2不需要进位为false
carry = false;
else if (j == 0) {//首位且大于5的
list.add(0, 1);//需进位,在开头插入1
carry = false;//插入1已进位需设为false
} else//非首位且大于5的需要进位为true
carry = true;
}
}
for (int i : list)
System.out.print(i);
}
}
#include<iostream> #include<string> using namespace std; int main(){ int n; cin>>n; string s("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096"); int flag=0; //进位标志 for(int i=0;i<n-512;i++){ for(int j=s.length()-1;j>=0;j--){ int num=((s[j]-'0')<<1)+flag; flag=num/10; s[j]=num%10+'0'; } if(flag>0) { s.insert(s.begin(),flag+'0'); flag=0; } } cout<<s<<endl; }
#include<bits/stdc++.h> using namespace std; int main(){ int N; cin >>N; vector<int>res(1000,0);//数组长度10000 res[0]=1;//第一位置1,2的0次方为1 int i,j,k; for(i=0;i<N;++i)//求2的N次方,循环N次 { for(j=0;j<1000;++j) { res[j]*=2;//vector中的每一位都*2 } for(k=0;k<1000;k++) { if(res[k]>9) { res[k+1]++;//检查是否有进位 进位它的下一位+1 res[k]=res[k]%10;//本位对10 取余 } } } i=1000; while(res[i]==0) i--;//倒序打印 for(;i>=0;i--) cout << res[i]; }
#include<vector> #include<iostream> using namespace std; int main(){ int N; cin>>N; int tmp=0; vector<int> string; string.push_back(1); for(int k=0;k<N;k++){ if(string[string.size()-1]>=5){ string.push_back(0);} for(int z=0;z<string.size();z++){ //if(string[z]!=0) string[z]=2*string[z]+tmp; tmp=string[z]/10; string[z]=string[z]%10; } } for(int show=string.size()-1;show>=0;show--){ cout<<string[show]; } }农民代码,C C++傻傻分不清楚。