C++ 没见过的编译失败方式

P1464 Function

HC_Lin @ 2022-08-26 21:20:44

  • 由此入
#include<iostream>
using namespace std;
int ia[99999999999];
int ib[99999999999];
int ic[99999999999];
int ans[99999999999];
int i=1;
int w(int a,int b,int c){
    if(a<=0||b<=0||c<=0){
        ia[i]=a;
        ib[i]=b;
        ic[i]=c;
        ans[i]=1;
        i++;
        return 0;
    }
    else if(a>20||b>20||c>20){
        w(20,20,20);
        i++;
        return 0;
    }
    else if(a<b&&b<c){
        int d=w(a,b,c-1);
        int e=w(a,b-1,c-1);
        int f=w(a,b-1,c);
        int g=d+e-f;
        ia[i]=a;
        ib[i]=b;
        ic[i]=c;
        ans[i]=g;
        i++;
        return 0;
    }
    int d=w(a-1,b,c);
    int e=w(a-1,b-1,c);
    int f=w(a-1,b,c-1);
    int g=w(a-1,b-1,c-1);
    int h=d+e+f-g;
    ia[i]=a;
    ib[i]=b;
    ic[i]=c;
    ans[i]=h;
    i++;
    return 0;
}
int main(){
    int a,b,c,i=1;
    for(;;){
        cin>>a>>b>>c;
        if(a==-1&&b==-1&&c==-1) break;
        else w(a,b,c);
    }
    for(int d=1;d<=i;d++) cout<<"w("<<ia[d]<<", "<<ib[d]<<", "<<ic[d]<<") = "<<ans[d]<<endl;
}

by HC_Lin @ 2022-08-27 17:00:40

@CoCo_Candy

我太弱了[哭]


by gmy_coding @ 2022-10-12 21:02:45

@HC_Lin 你忘了return 0了吧


by int_stl @ 2022-11-16 14:38:19

你空间复杂度爆了!

#include <bits/stdc++.h>
using namespace std;
long long f[25][25][25];
long long w(long long a,long long b,long long c)
{
  if(a<=0||b<=0||c<=0) return 1;
  else if(a>20||b>20||c>20) return w(20,20,20);
  else if(f[a][b][c]!=0) return f[a][b][c];
  else if(a<b&&b<c) f[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
  else f[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
  return f[a][b][c];
}
int main()
{
  long long a,b,c;
  while(cin>>a>>b>>c)
  {
    if(a==-1&&b==-1&&c==-1) break;
    cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(a,b,c)<<endl;
  }
  return 0;
}

上一页 |