1 条题解

  • 0
    @ 2025-4-7 21:28:48

    C :

    #include "stdio.h"  
    #include "math.h"  
    main()  
    {long n,sum=0,i=1,s=2;  
    scanf("%d",&n);  
    while(i<=n)  
    {sum=sum+s;s=s+2*pow(10,i);  
    i++;}  
    printf("%ld",sum);  
    }
    

    C++ :

    #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
    	int s[100],b[100],i,a,j=0,d=0;
    	cin>>a;
    	b[0]=0;
    	for(i=0;i<a;i++)
    	{
    		b[j]=2*pow(10,(float)i);
    		j++;
    	}
    	for(i=0;i<a;i++)
    	{
    			if(i==0)
    			{
    				s[i]=b[i];
    				continue;
    			}
    			s[i]=b[i]+s[i-1];
    	}
    	for(i=0;i<a;i++)
    	{
    		d+=s[i];
    	}
    	cout<<d<<endl;
    }
    

    Java :

    
    
    import java.util.Scanner;
    
    public class Main {
      private static Scanner s = new Scanner(System.in) ;
      
      public static void main(String[] args) {
    	 int n = s.nextInt() ;
    	 int sum = 0 ;
    	 int temp = 0 ;
    	 for (int i = 1; i <= n; i++) {
    		int t = 1 ;
    		for (int j = 1; j <= i; j++) {
    			
    			temp += 2*t ;
    			t*=10 ;
    		}
    		sum+=temp ;
    		temp = 0 ;
    	  }
    	 
    	 System.out.println(sum) ;
      }
    }
    
    

    Python :

    a = input()
    b = 0
    for i in range(1, a + 1):
        c = 2
        for j in range(1, i):
            c *= 10
            c += 2
        b+=c
    print b
    
    • 1

    C语言程序设计教程(第三版)课后习题6.3

    信息

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