---
title: "关于不定方程整数解的个数的求法"
author: "cjc"
date: "2005-12-12T21:07:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2005-12-12T21:07:00Z"
id: 299146
url: "https://cjc.iteye.com/blog/299146"
---

题目：

整数数列 {xn}  ∈\[0,max] , 且 ∑xi=sum, 则这样的序列共有多少个？

方法：递归

Function resultcount(ByVal n As Integer, ByVal max As Integer, ByVal sum As Integer) As Integer  
If n * max &lt; sum Then resultcount = 0: Exit Function  
If n = 1 Then resultcount = 1  
If sum = 1 Then resultcount = n  
If n &gt; 1 Then  
Dim i As Integer, temp As Integer  
temp = 0  
For i = 0 To max  
temp = temp + resultcount(n - 1, max, sum - i)  
Next  
resultcount = temp  
End If  
End Function