1 条题解

  • 0
    @ 2023-6-11 12:16:30

    C :

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    #include<math.h>
    int a[10005],s[10005];
    //每次都计算 a数组*k 
    void ans(int k){ //123
    	int i;
    	//每一位都乘以k 
    	for(i=0;i<10000;i++){
    		a[i] = a[i]*k;
    	}
    	for(i=0;i<10000;i++){
    		a[i+1] = a[i+1] + a[i]/10;
    		a[i] = a[i]%10;
    	}
    }
    void add(){
    	int i;
    	for(i=0;i<10000;i++){
    		s[i] = s[i]+a[i];
    		s[i+1] = s[i+1] + s[i]/10;
    		s[i] = s[i]%10; 
    	}
    }
    int main(){
    	int n,i,j;
    	scanf("%d",&n);
    
    	for(i=1;i<=n;i++){
    		memset(a,0,sizeof(a)); 
    		a[0] = 1; 
    		for(j=1;j<=i;j++){
    			ans(2);
    		}
    		add();
    	}
    
    	int p=1000;
    	for(i=p;i>=0;i--){
    		if(s[i]!=0){
    			p = i;
    			break;
    		}
    	}
    	for(i=p;i>=0;i--){
    		printf("%d",s[i]);
    	}
    	return 0;
    }
    
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
    	int a[100][100] = {0};
    	int r[100] = {0};
    	int i,j,n;
    	cin>>n;
    	a[0][0] = 1;
    	int k = 1;
    	for(i = 1;i <= n;i++){
    		for(j = 0;j < k;j++){
    			a[i][j] = a[i - 1][j] * 2;
    		}
    		
    		for(j = 0;j < k;j++){
    			if(a[i][j] >= 10){
    				a[i][j + 1] = a[i][j + 1] + a[i][j] / 10;
    				a[i][j] = a[i][j] % 10; 
    			}
    		}
    		
    		if(a[i][k] > 0){
    			k++;
    		}
    	}
    	
    	//求和
    	for(i = 1;i <= n;i++){
    		for(j = 0;j < k;j++){
    			r[j] = r[j] + a[i][j];
    			
    			if(r[j] >= 10){
    				r[j + 1] += r[j] / 10;
    				r[j] = r[j] % 10;
    			}
    		}
    		
    		if(r[k] > 0){
    			k++;
    		}
    	}
    	
    	int p = 0;
    	for(i = k - 1;i >= 0;i--){
    		if(r[i] != 0){
    			p = i;
    			break;
    		}
    	} 
    
    	for(i = p;i >= 0;i--){
    		cout<<r[i];
    	} 
    }
    
    

    Python :

    print(2**int(input())*2-2)
    
    • 1

    【基础】求2+2*2+2*2*2+…+2*2*2*….*2

    信息

    ID
    2189
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者