Backward Digit Sums
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5001 Accepted: 2876
Description
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this:
3 1 2 4
4 3 6
7 9
16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.
Write a program to help FJ play the game and keep up with the cows.
Input
Line 1: Two space-separated integers: N and the final sum.
Output
Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.
Sample Input
4 16
Sample Output
3 1 2 4
Hint
Explanation of the sample:
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5001 Accepted: 2876
Description
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this:
3 1 2 4
4 3 6
7 9
16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.
Write a program to help FJ play the game and keep up with the cows.
Input
Line 1: Two space-separated integers: N and the final sum.
Output
Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.
Sample Input
4 16
Sample Output
3 1 2 4
Hint
Explanation of the sample:
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
写出全排列,再判断每一个全排列是否符合条件,符合条件就输出,停止继续生成全排列,因为生成的排列是从小到大生成的,所以第一个符合条件的一定就是答案。
为什么手写的方式就错了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <stdlib.h>
using namespace std;
int flag;
int sum;
int b[20][20];
int is_yes(int n,int *a)
{
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
b[0][i]=a[i];
for(int i=1;i<n;i++)
{
for(int j=0;j<n-i+1;j++)
{
b[i][j]=b[i-1][j]+b[i-1][j+1];
}
}
// printf("%d\n",b[n-1][0]);
if(b[n-1][0]==sum)
return 1;
return 0;
}/*
void print_permution(int n,int *a,int cur)
{
if(cur==n&&flag==0&&is_yes(n,a))
{
flag=1;
for(int i=0;i<n-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[n-1]);
}
else if(flag==0)
{
for(int i=1;i<=n;i++) //如果是想自己定义数组,就把自己定义的数组存在b数组里,//i改为b[i]
{
int ok=1;
for(int j=0;j<=cur;j++)
{
if(a[j]==i)//一般在这里添加剪枝条件//那么这里也要改一下为b[i]
{
ok=0;
break;
}
}
if(ok)
{
a[cur]=i;
print_permution(n,a,cur+1);
}
}
}
}*/
int main(void)
{
int a[20];
int n;
while( scanf("%d%d",&n,&sum)!=EOF)
{
flag=0;
memset(a,0,sizeof(a));
//print_permution(n,a,0);
for(int i=0;i<n;i++)
a[i]=i+1;
while(!is_yes(n,a))
{
next_permutation(a,a+n);
}
for(int i=0;i<n-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[n-1]);
}
return 0;
}