标签:cal cin star help first tip 杨辉三角 style more
3 1 2 4Behind 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.
4 3 6
7 9
16
4 16Sample Output
3 1 2 4Hint
#include <iostream> #include <string> using namespace std; int n,d,vis[11],num[11],flag = 0; int csum(); void print(); void dfs(int k) { if(flag)return; if(k > n) { // cout<<csum()<<endl; if(csum() == d) { print(); flag = 1; return; } } for(int i = 1;i <= n;i ++) { if(vis[i] == 0) { vis[i] = 1; num[k] = i; dfs(k + 1); vis[i] = 0; } } } void print() { cout<<num[1]; for(int i = 2;i <= n;i ++) cout<<‘ ‘<<num[i]; } int counti(int k) { int c = 1; for(int i = 0;i < k;i ++) c *= (n - 1 - i); for(int i = 0;i < k;i ++) c /= (i + 1); return c; } int csum() { int sum = 0; for(int i = 1;i <= n;i ++) { sum += num[i] * counti(i - 1); } return sum; } int main() { cin>>n>>d; dfs(1); }
标签:cal cin star help first tip 杨辉三角 style more
原文地址:http://www.cnblogs.com/8023spz/p/7786961.html