Backward Digit Sums
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 2
Problem 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:
Write a program to help FJ play the game and keep up with the cows.
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
N <= 10 全排列 10!,继续暴力(逃
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <queue>
using namespace std;
const int INF = 0x3fffffff;
string str;
int num[15];
int a[15];
int main() {
//freopen("in.txt", "r", stdin);
int n, sum;
while (cin >> n >> sum) {
for (int i = 0; i < n; ++i)
num[i] = i + 1;
do {
for (int i = 0; i < n; ++i)
a[i] = num[i];
int foo = n;
while (foo != 1) {
for (int i = 0; i < foo - 1; ++i) {
a[i] = a[i] + a[i + 1];
}
--foo;
}
if (a[0] == sum) {
for (int i = 0; i < n; ++i) {
cout << num[i] << (i == n ? "" : " ");
}
cout << endl;
break;
}
}while (next_permutation(num, num + n));
}
}
本文介绍了一款名为BackwardDigitSums的数字游戏,玩家需从1到N的数字序列出发,通过不断合并相邻数字直至只剩一个数。文章提供了一个C++实现方案,用于寻找能够得到指定总和的起始数字序列。
&spm=1001.2101.3001.5002&articleId=75256849&d=1&t=3&u=1a927bf5bd834ab69ac15e76dea32223)
477

被折叠的 条评论
为什么被折叠?



