码迷,mamicode.com
首页 > 其他好文 > 详细

Project Euler:Problem 76 Counting summations

时间:2017-04-17 11:34:33      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:大于   main   possible   least   for   ssi   integer   ==   mil   

It is possible to write five as a sum in exactly six different ways:

4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1

How many different ways can one hundred be written as a sum of at least two positive integers?


#include <iostream>
using namespace std;

int c = 0;//累划分数
void p(int n, int a[], int m)//m表示每一种划分的加数的个数
{
	int i;
	if (n == 0)
	{
		c++;
		//int i;
		//for (i = 0; i < m - 1; i++)
		//	cout << a[i] << "+";
		//cout << a[m - 1] << endl;
	}
	else
	for (i = n; i >= 1; i--)
	{
		if (m == 0 || i <= a[m - 1])//要保证下一个划分因子不大于上一个划分因子
		{
			a[m] = i;
			p(n - i, a, m + 1);
		}
	}
}

void main(void)
{
	int n;
	int a[200] = { 0 };//存储整数n的划分
	printf("输入要被划分的整数: ");
	cin >> n;
	p(n, a, 0);
	cout << "整数" << n << "的划分数是:" << c-1 << "种。" << endl;
	system("pause");
}


Project Euler:Problem 76 Counting summations

标签:大于   main   possible   least   for   ssi   integer   ==   mil   

原文地址:http://www.cnblogs.com/brucemengbm/p/6721973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!