1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <algorithm> #include <iostream> using namespace std; int n; int d[50][2]; int ans; void dfs(int n, int sum){ if(n==1){ ans=max(sum,ans); return ; } for (int i = 1; i < n; i++){ int a=d[i-1][0],b=d[i-1][1]; int x=d[i][0],y=d[i][1]; d[i-1][0]=a+x; d[i-1][1]=b+y; for (int j = i; j < n-1; j++) d[j][0]=d[j+1][0],d[j][1]=d[j+1][1]; int s=a+x+abs(b-y); cout<<s<<' '; dfs(n-1,sum+s); for (int j = n-1; j >i; --j){ d[j][0]=d[j-1][0]; d[j][1]=d[j-1][1]; } d[i-1][0]=a; d[i-1][1]=b; d[i][0]=x; d[i][1]=y; } } int main(){ cin>>n; for (int i = 0; i < n; i++){ cin>>d[i][0]; } for (int i = 0; i < n; i++){ cin>>d[i][1]; } ans=0; dfs(n,0); cout<<ans<<endl; return 0; } |
假设输入的 n是不超过 50 的正整数,d[i][0]
、d[i][1]
都是不超过 10000 的正整数。
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1.若输入的n为0,此程序可能会死循环或发生运行错误。
2.若输入的n为20,接下来的输入全为0,则输出为0。
3.输出的数一定不小于输入的d[i][0]和d[i][1]的任意一个。
4.若输入的n为20,接下来的输入是20个9和20个0,则输出为( )
5.若输入的n为30,接下来的输入是30个0和30个5,则输出为( )。
6.若输入的n为15,接下来输入是15到1,以及15到1,则输出为( )。