求助lambda递归写法

P1001 A+B Problem

peterwuyihong @ 2022-01-26 19:51:25

把这个改对即可,这个函数套函数不准拆开

#include<bits/stdc++.h>
using namespace std;
int G(int a,int b){
  auto GG=[&](int a){
    if(a==0)return 1;
    return 1+GG(a-1);
  };
  return GG(a+b);
}
signed main(){
  int a,b;
  cin>>a>>b;
  cout<<G(a,b);
}

by peterwuyihong @ 2022-01-26 19:52:29

哎呦,中间那个应该是return 0;


by peterwuyihong @ 2022-01-26 19:58:35

会了,这样写

#include<bits/stdc++.h>
using namespace std;
int G(int a,int b){
  function<int(int a)>GG;
  GG=[&](int a){
    if(a==0)return 0;
    return 1+GG(a-1);
  };
  return GG(a+b);
}
signed main(){
  int a,b;
  cin>>a>>b;
  cout<<G(a,b);
}

by 灰鹤在此 @ 2022-01-26 20:01:52

#include<bits/stdc++.h>
using namespace std;
int G(int a,int b){
  function<int(int a)>GG;
  GG=[&](int a){
        return a;
  };
  return GG(a+b);
}
signed main(){
  int a,b;
  cin>>a>>b;
  cout<<G(a,b);
}

by MarvinWang @ 2022-01-29 22:06:35

a+b用函数人才啊


|