1 条题解

  • 0
    @ 2026-2-10 16:19:31

    C :

    #include<stdio.h>
    int main()
    {
    	float a,b,c,s=0;
    	int n,i;
    	scanf("%d",&n);
    	a=1;
    	b=2;
    	for(i=0;i<n;i++)
    	{
    		s=s+b/a;
    		c=a+b;
    		a=b;
    		b=c;
    	}
    	printf("s=%.2f\n",s);
    }
    

    C++ :

    #include<stdio.h>
    #include<math.h>
    int main()
    {
    	float a,b,c,s=0;
        int i,n;
        scanf("%d",&n);
        a=1;
        b=2;
        for(i=1;i<=n;i++)
       {
      	s=s+1.0*b/a;
      	c=a+b;
      	a=b;
      	b=c;
       }
       printf("s=%.2f\n",s);
    }
    
    

    Java :

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[][] a = new int[n][n];
            a[0][0] = 1;
            a[0][1] = 2;
            for (int i = 1 ; i<n;i++){
                a[i][0] =a[i-1][1];
                a[i][1]=a[i-1][0]+a[i-1][1];
            }
            float sum = 0;
            for (int i = 0 ; i < n ; i++){
                sum += chu(a[i][0],a[i][1]);
            }
            System.out.printf("s=%.2f",sum);
        }
        public static float chu(int a,int b){
            return (float)b/a;
        }
    }
    
    

    Python :

    # coding=utf-8
    #python2
    n = int(input())
    s = 0
    a = 2
    b = 1
    for i in range(1,n+1):
        s += float(a) / b
        t = b
        b = a
        a = t + b
    print('s=%.2f'%s)
    

    C# :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp2
    {
        internal class Program
        {
            static void Main()
            {
    
                int n = Convert.ToInt32(Console.ReadLine());
            
                double s =0;
                double a = 1;
                double b = 2;
                double c = a;
                double x = 0;
                for (int i = 1; i <= n; i++)
                {
                    x =b / a;
                    s = s + x;
                    c = a;
                    a = b;
                    b = c+b;
                    
                }
                Console.WriteLine("s="+Math.Round(s, 2).ToString("0.00"));
                Console.ReadKey();
    
            }
        }
    }
    

    信息

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