1 条题解

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

    C :

    #include<stdio.h>
    
    int x,y,z,n;
    int a[500],b[500],c[500];
    
    void input_data()
    {
        scanf("%d %d",&x,&y);
        if (x > y)
            {
                z = x;x = y;y = z;
            }
        n = y - x + 1;
    }
    
    void get_ans()
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        int la = 1,lb = 1,lc;
        a[1] = 1;b[1] = 1;
        if ( n == 1)
            {
                printf("1");
                return;
            }
        if (n == 2)
            {
                printf("1");
                return;
            }
        for (int i = 3;i <=n;i++)
            {
                int l;
                if (lb > la)
                    l = lb;
                        else
                            l = la;
                int x = 0;
                for (int j = 1;j <= l;j++)
                    {
                        c[j] = a[j] + b[j] + x;
                        x = c[j] / 10;
                        c[j] = c[j] % 10;
                    }
                while (x > 0)
                    {
                        l++;
                        c[l] += x;
                        x = c[l] / 10;
                        c[l] = c[l] % 10;
                    }
                lc = l;
                for (int j = 1;j <= lb;j++)
                    a[j] = b[j];
                la = lb;
                for (int j = 1;j <= lc;j++)
                    b[j] = c[j];
                lb = lc;
            }
        for (int j = lc;j >= 1;j--)
            printf("%d",c[j]);
    }
    
    int main()
    {
        input_data();
        get_ans();
        return 0;
    }
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    //高精度求和 
    string num(string s1,string s2){
    	string r = ""; 
    	int i,len;//len存较长的长度 
    	int a[250] = {0},b[250] = {0},c[250] = {0};
    	//第一步:将2个字符数组逆序存入2个整数数组
    	//s1 = "12345";
    	for(i = 0;i < s1.size();i++){
    		//0->s1.size()-1  1->s1.size()-2
    		a[s1.size()-i-1] = s1[i] - '0';
    	} 
    	
    	for(i = 0;i < s2.size();i++){
    		b[s2.size()-i-1] = s2[i] - '0';
    	}
    	
        //第二步:逐位相加,逐位进位
        //int len = s1.size() > s2.size()?s1.size():s2.size();//三目运算符,替代if else 
    	len = s1.size();
    	if(s2.size() > s1.size()){
    		len = s2.size();
    	} 
    	
    	//加 
    	for(i = 0;i < len;i++){
    		c[i] = a[i] + b[i];
    	}
    	//进位
    	for(i = 0;i < len;i++){
    		if(c[i] >= 10){
    			c[i+1] = c[i+1] + c[i] / 10;
    			c[i] = c[i] % 10;
    		}
    	} 
    	
    	//第三步:逆序输出
    	if(c[len] != 0){
    		len++;
    	} 
    	
    	for(i = len - 1;i>=0;i--){
    		r = r + (char)(c[i] + 48);
    	}
    	return r;
    }
    
    int main(){
    	long long m,n,x,i;
    	cin>>m>>n;
    	x = n - m;
    	string t1,t2,t;
    	t1 = "1";
    	t2 = "2";
    	for(i = 3;i <= x;i++){
    		t = num(t1,t2);
    //		cout<<i<<" "<<t<<endl;
    		t1 = t2;
    		t2 = t;
    	}
    	
    	cout<<t2<<endl;
    //	cout<<LONG_LONG_MAX<<endl;
        return 0;
    }
    
    

    Python :

    sr=input().split()
    M=int(sr[0])
    N=int(sr[1])
    a=[]
    a.append(0)
    for i in range(1,N+1):
        if(i==1):
            a.append(1)
        elif(i==2):
            a.append(1)
        else:
            a.append(a[i-2]+a[i-1])
    print(a[N-M+1])
    
    • 1

    信息

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