Input
4
2 3 4 9
Output
YES
4
2 3 4 9
Output
YES
不写这个可以吗?
#include<bits/stdc++.h> using namespace std; int n,a[10010]; bool flg=false; void dfs(int i,int t1,int t2){ if(i==n){ if(?????)flg=true; return; } dfs(?????); dfs(?????); } int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>a[?????]; } dfs(?????); if(?????)cout<<"YES"; else cout<<"NO"; return 0; }
//我不会算法只能用最笨的方法 //第二题 #include<iostream> using std::cout; using std::cin; using std::endl; bool arry(int a[],short x,short y,short z) { short n1=y-x+1; short n2=z-y; int l[n1+1];int r[n2+1]; int sum=0;int sum2=0; for(int i=0;i<n1;++i) { l[i]=a[i]; sum+=l[i];} // cout<<sum; for(int j=1;j<n2;++j) {r[j]=a[y+j]; sum2+=r[j];} // cout<<sum2; return sum==sum2;} int main() { int a[164356]={};int dexs=0 ; while((std::cin.peek()!=EOF)&&(std::cin.peek()!='\n')) { cin>>a[dexs]; ++dexs; } cout<<std::boolalpha; cout<< arry(a,0,dexs/2,dexs); }
第一题 #include<iostream> using std::cout; using std::cin; using std::endl; bool arry(int num[],int dest,int a) { int count=0; for(int i=0; i<dest&&num[i]; ++i) { for( int j=1; j<dest&&(j!=i); ++j) { int c=num[i]+num[j]; if(c==a) { ++count; } } } return count!=0; } int main() { int dest=0; int num[10010]= {}; int a={}; cout<<"输入目标t"<<endl; cin>>a; int count=0; cout<<"输入数组"<<endl; std::cin.ignore(32767,'\n'); while((cin.peek()!=EOF)&&(cin.peek()!='\n')) {cin>>num[dest]; ++dest;} cout<<std::boolalpha; cout<< arry(num,dest,a); }
[此贴子已经被作者于2020-3-3 15:08编辑过]
#include<bits/stdc++.h> using namespace std; int n,t,a[10010]; bool flg=false; void dfs(int i,int sum){ if(i==n){ if(sum==t)flg=true; return; } dfs(i+1,sum+a[i]); dfs(i+1,sum); } int main(){ cin>>n>>t; for(int i=0;i<n;i++){ cin>>a[i]; } dfs(0,0); if(flg)cout<<"YES"; else cout<<"NO"; return 0; }