1 条题解

  • 0
    @ 2023-6-11 12:21:10

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    
    
    int main() {
    	int n,a[20],p;
    	cin>>n;
    	int i;
    	for(i = 0; i < n; i++) {
    		cin>>a[i];
    	}
    	for(i = 3;i < n;i++){
    		if(a[i] != a[i-1]+a[i-2]){
    			p = i+1;
    			break;
    		}
    	}
    	
    	for(i = p-1;i<n;i++){
    		a[i]=a[i-1]+a[i-2];
    	}
    	
    	cout<<p<<endl;
    	for(i = 0; i < n; i++) {
    		cout<<a[i]<<" ";
    	}
    }
    
    

    Java :

    
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int num1 = scanner.nextInt();
            int[] count = new int[80];
            int[] a = new int[num1];
            int[] b = new int[num1];
            for (int i = 0; i < num1; i++) {
                a[i] = scanner.nextInt();
            }
            b[0] = 1;
            b[1] = 1;
            for (int i = 2; i < num1; i++) {
                b[i] = b[i-1] + b[i-2];
            }
            for (int i = 0; i < num1; i++) {
                if(a[i] != b[i]){
                    System.out.println(i+1);
                    break;
                }
            }
            for (int i = 0; i < num1; i++) {
                System.out.print(b[i] + " ");
            }
    
        }public static void paiXu(int[] a,int length){
            int temp;
            for(int i = 0;i < length - 1;i++){
                for(int k = 0;k < length - i - 1;k++){
                    if(a[k] > a[k + 1]){
                        temp = a[k];
                        a[k] = a[k + 1];
                        a[k + 1] = temp;
                    }
                }
            }
        } public static void paiXud(int[] a){
            int temp;
            for(int i = 0;i < a.length - 1;i++){
                for(int k = 0;k < a.length - i - 1;k++){
                    if(a[k] < a[k + 1]){
                        temp = a[k];
                        a[k] = a[k + 1];
                        a[k + 1] = temp;
                    }
                }
            }
        } public static void paiXux(int[] a){
            int temp;
            for(int i = 0;i < a.length - 1;i++){
                for(int k = 0;k < a.length - i - 1;k++){
                    if(a[k] > a[k + 1]){
                        temp = a[k];
                        a[k] = a[k + 1];
                        a[k + 1] = temp;
                    }
                }
            }
        }
    
        public static int MAX(int[] a){
            int max = a[0];
            for (int i = 0; i < a.length; i++) {
                if(max < a[i]){
                    max = a[i];
                }
            }
            return max;
        }
    
        public static int MIN(int[] a){
            int min = a[0];
            for (int i = 0; i < a.length; i++) {
                if(min > a[i]){
                   min = a[i];
                }
            }
            return min;
        }
        public static int sum(int a){
            int x,s = 0;
            while(a != 0){
                x = a % 10;
                s = s + x;
                a /= 10;
            }
            return s;
        }
        public static boolean isPrime(int a){
            if(a <= 1)return false;
            else
            for(int i = 2;i <= Math.sqrt(a);i++){
                if(a % i == 0)return false;
            }return true;
        }
    
        public static long qiuYu(long i){
            long a;
            a = i % 10;
            return a;
        }
    }
    
    

    Python :

    n=int(input())
    a=list(map(int,input().split()))
    b1=a[0]
    b2=a[1]
    d=[b1,b2]
    for i in range(2,n):
        c=b1+b2
        d.append(c)
        b1=b2
        b2=c
        if (c!=a[i]):
            k=i
            break
    print(i+1)
    for i in range(k+1,n):
        c=b1+b2
        d.append(c)
        b1=b2
        b2=c
    for i in d:
        print(i,end=" ")
    
    • 1

    信息

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