1 条题解

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

    C :

    #include <stdio.h>
    int main(void)
    {
    	int N;
    	int n1 = 1,n2 =2,n3,i;
    	while(scanf("%d",&N) != EOF)
    	{
    		if(N == 1)
    		{
    			printf("%d\n",n1);
    		}
    		else if(N == 2)
    		{
    			printf("%d\n",n2);
    		}
    		else
    		{
    			n1 = 1;
    			n2 = 2;
    			for(i=3;i<=N;i++)
    			{
    				n3 = n1 + n2;
    				n1 = n2;
    				n2 = n3;
    			}
    			printf("%d\n",n3);
    		}
    	}
    	return 0;
    }
    

    C++ :

    #include <stdio.h>
    int main()
    {
    	int n;
    	int a[]={1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946};
    	scanf("%d",&n);
    	while(n!=0)
    	{
    		printf("%d\n",a[n]);
    		n=0;
    		scanf("%d",&n);
    	}
    	return 0;
    }
    

    Pascal :

    var i,n:longint;
        a,b,c:qword;
    begin
      while not eof do
      begin
      readln(n);
        a:=1;b:=2;
        for i:=3 to n do
          begin
            c:=a+b;
            a:=b;
            b:=c;
          end;
       if n=1 then writeln(1);
       if n=2 then writeln(2);
       if n>=3 then writeln(c);
      end;
    end.
    

    Java :

    import java.io.PrintStream;
    import java.util.Scanner;
    
    public class Main {
    	static int  add(int n)
    	{
    	if(n==1||n==2) return n;
    	else return add(n-1)+add(n-2);
    	}
    	public static void main(String[] args )
    	{
    	int n,sum;
    	Scanner sc = new Scanner(System.in);
    	 PrintStream ps = new PrintStream(System.out);
    	while(sc.hasNext())
    	{ n= sc.nextInt();
    	  sum=add(n);
    	 ps.printf("%d\n",sum);
    	}
    		
    	}
    }
    
    
    • 1

    信息

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