3 条题解

  • 0
    @ 2026-1-10 19:41:21
    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAX_N = 10000;
    const int bi = 3e5 + 10;
    
    string fib[MAX_N + 5];
    
    string add(string a, string b)
    {
        int ah[bi] = {0}, bh[bi] = {0}, ch[bi] = {0}, x = 0;
        int la = a.size();
        int lb = b.size();
        int lc = max(la, lb);
    
        for (int i = 0; i < la; i++)
        {
            ah[i] = a[la - 1 - i] - '0';
        }
        for (int i = 0; i < lb; i++)
        {
            bh[i] = b[lb - 1 - i] - '0';
        }
    
        for (int i = 0; i < lc; i++)
        {
            ch[i] = ah[i] + bh[i] + x;
            x = ch[i] / 10;
            ch[i] %= 10;
        }
    
        if (x)
        {
            ch[lc] = x;
            lc++;
        }
    
        while (lc > 1 && ch[lc - 1] == 0)
        {
            lc--;
        }
    
        string cj = "";
        for (int i = lc - 1; i >= 0; i--)
        {
            cj += (ch[i] + '0');
        }
        return cj;
    }
    
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0);
    
        fib[1] = "1";
        fib[2] = "1";
        for (int i = 3; i <= MAX_N; i++)
        {
            fib[i] = add(fib[i - 1], fib[i - 2]);
        }
    
        int n;
        while (cin >> n)
        {
            cout << fib[n] << endl;
        }
    
        return 0;
    }
    
    
    • 0
      @ 2023-9-26 22:47:23
      #include <iostream>
      #include <algorithm>
      #include <vector>
      #include <stack>
      #include <string>
      #include <math.h>
      using namespace std;
      //vector:存放结果
      //stack:  计算过程
      int main(){
          int n;
          vector <string> haha;
          char ai[1050],bi[1050];
          stack <char> a,b,fin;
          ai[0]='1';ai[1]='\0';bi[0]='1';bi[1]='\0';
          haha.push_back(ai);
          haha.push_back(bi);//存入一二两位
          int ci, len=0;//ci为进位标志,len为实时记录每轮运算结果的长度,超过1000跳出循环
          int i;
          char ch,aa,bb;//ch记录每一位运算结果,aa,bb记录参与每位运算的数
          while(len<=1000){
              i=0;
              len=0;
              ci=0;
              while(ai[i]!='\0'){
                  a.push(ai[i]);
                  i++;
              }//入栈
       
              i=0;
              while(bi[i]!='\0'){
                  b.push(bi[i]);
                  i++;
              }//入栈
              while(!a.empty()&&!b.empty()){
                  aa=a.top();a.pop();
                  bb=b.top();b.pop();
                  ch=(char)('0'+(ci+(int)((aa-'0')+(bb-'0')))%10);
                  ci=(ci+(int)((aa-'0')+(bb-'0')))/10;
                  fin.push(ch);
                  len++;
              }//逐位加法运算
              if(a.empty()&&!b.empty()){
                      while(!b.empty()){
                          bb=b.top();
                          b.pop();
                          fin.push((char)('0'+(ci+(int)(bb-'0'))%10));
                          len++;
                          ci=(ci+(bb-'0'))/10;
                      }
              }//由于两个数可能不等长,长的那一部分高位必须继续进行加法运算
              if(ci>0){
                  fin.push((char)('0'+ci));
                  len++;
              }//算到最后一位还有可能有进位
       
              if(len>1000){
                  break;
              }//超过一千位,结果超过限制,跳出循环
       
              i=0;
              while(bi[i]!='\0'){
                  ai[i]=bi[i];
                  i++;
              }
              ai[i]='\0';
              
              i=0;
              while(!fin.empty()){
                  bi[i]=fin.top();
                  fin.pop();
                  i++;
              }
              bi[i]='\0';
              
              haha.push_back(bi);//计算结果压入数组
         }
         
          while(cin>>n){
              if(n<=0){
                  break;
              }
              cout<<haha.at(n-1)<<endl;
          }
          haha.clear();
       
          return 0;
      }
      
    • 0
      @ 2022-10-2 11:54:07
      #include<bits/stdc++.h>
      //抄的是sz
      using namespace std;
      string a,b,s[100005];
      int t,n,x[10005],y[10005];
      string gjd(string a,string b)
      {
      	string ans="";
      	t=0;
      	memset(x,0,sizeof(x));	
      	memset(y,0,sizeof(y));
      	if(a.size()<b.size()) swap(a,b);
      	for(int i=a.size()-1,j=0;i>=0;i--,j++)
      	{
      		x[j]=a[i]-'0';
      	}
      	for(int i=b.size()-1,j=0;i>=0;i--,j++)
      	{
      		y[j]=b[i]-'0';
      	}
      	for(int i=0;i<a.size();i++)
      	{
      		x[i]=x[i]+y[i];
      	}
      	for(int i=0;i<a.size();i++)
      	{
      		if(x[i]>=10)
      		{
      			x[i+1]++;
      			x[i]-=10;
      			if(i==a.size()-1) t++; 
      		}
      	}
      	if(t==1) ans+='1';
      	for(int i=a.size()-1;i>=0;i--)
      	{
      		ans+=char(x[i]+'0');
      	}
      	return ans;
      }
      int main()
      {
      	while(cin>>n)
      	{
      		s[1]='1';
      		s[2]='1';
      		if(s[n]!="") cout<<s[n]<<endl;
      		else
      		{
      			for(int i=3;i<=n;i++)
      			{
      				s[i]=gjd(s[i-1],s[i-2]);
      			}
      			cout<<s[n]<<endl;
      		}
      	}
      	return 0;
      }
      
      • 1

      信息

      ID
      1185
      时间
      1000ms
      内存
      128MiB
      难度
      6
      标签
      递交数
      213
      已通过
      65
      上传者