1 //求出用1,2,5这三个数不同个数组合的和为1000的组合个数。
2 //如:100个1是一个组合,5个1加19个5是一个组合。。。。
3 public class getNumbers{
4 public static void sums1(int temp)
5 {
6 int num1 = temp/1;
7 int num2 = temp/2;
8 int num3 = temp/5;
9 int numbers = 0;
10 String values;
11 for(int i=0;i<num1;i++)
12 {
13 for(int j=0;j<num2;j++)
14 {
15 for(int k=0;k<num3;k++)
16 {
17 if((1*i+2*j+5*k)==temp)
18 {
19 numbers++;
20 values = i+“个1,“+j+“个2,“+k+“个5“;
21 System.out.println(“the “+numbers+“ groups numbers is:“+values);
22 }
23 }
24 }
25 }
26 System.out.println(“the numbers of group is ===>“+numbers);
27 }
28 public static void main(String[] args) {
29 int s = 1000;
30 sums1(s);
31 }
32 }
33
1 运行结果:
2 E:\java\ProgramJava\csdn>javac getNumbers.java
3 E:\java\ProgramJava\csdn>java getNumbers >>groups.txt
4 groups.txt中的输出结果如下:
5 the 1 groups numbers is:0个1,5个2,198个5
6 the 2 groups numbers is:0个1,10个2,196个5
7 the 3 groups numbers is:0个1,15个2,194个5
8 the 4 groups numbers is:0个1,20个2,192个5
9 the 5 groups numbers is:0个1,25个2,190个5
10 the 6 groups numbers is:0个1,30个2,188个5
11 the 7 groups numbers is:0个1,35个2,186个5
12 the 8 groups numbers is:0个1,40个2,184个5
13 the 9 groups numbers is:0个1,45个2,182个5
14 the 10 groups numbers is:0个1,50个2,180个5
15 the 11 groups numbers is:0个1,55个2,178个5
16 the 12 groups numbers is:0个1,60个2,176个5
17 the 13 groups numbers is:0个1,65个2,174个5
18 the 14 groups numbers is:0个1,70个2,172个5
19 the 15 groups numbers is:0个1,75个2,170个5
20 the 16 groups numbers is:0个1,80个2,168个5
21 …………
22 the 50379 groups numbers is:983个1,6个2,1个5
23 the 50380 groups numbers is:984个1,3个2,2个5
24 the 50381 groups numbers is:984个1,8个2,0个5
25 the 50382 groups numbers is:985个1,0个2,3个5
26 the 50383 groups numbers is:985个1,5个2,1个5
27 the 50384 groups numbers is:986个1,2个2,2个5
28 the 50385 groups numbers is:986个1,7个2,0个5
29 the 50386 groups numbers is:987个1,4个2,1个5
30 the 50387 groups numbers is:988个1,1个2,2个5
31 the 50388 groups numbers is:988个1,6个2,0个5
32 the 50389 groups numbers is:989个1,3个2,1个5
33 the 50390 groups numbers is:990个1,0个2,2个5
34 the 50391 groups numbers is:990个1,5个2,0个5
35 the 50392 groups numbers is:991个1,2个2,1个5
36 the 50393 groups numbers is:992个1,4个2,0个5
37 the 50394 groups numbers is:993个1,1个2,1个5
38 the 50395 groups numbers is:994个1,3个2,0个5
39 the 50396 groups numbers is:995个1,0个2,1个5
40 the 50397 groups numbers is:996个1,2个2,0个5
41 the 50398 groups numbers is:998个1,1个2,0个5
42 the numbers of group is ===>50398
43
1 #include<iostream>
2 using namespace std;
3 void main()
4 {
5 int i,j,n=0;
6 for(i=0;i<=20;i++)
7 {
8 for(j=0;j<=(100-i*5)/2;j++)
9 {
10 n++;
11 }
12 }
13 cout<<n<<endl;
14 }
1 #include <stdio.h>
2 int main(void)
3 {
4 int j,k,n;
5
6 for(j=0;j<=50;j++)
7 {
8 for(k=0;k<=20;k++)
9 {
10 if(2*j+5*k<=100)
11 n++;
12 }
13 }
14
15 printf(“count=%d\n“,n);
16 }
posted on 2006-01-19 14:51
★yesjoy★ 阅读(2020)
评论(2) 编辑 收藏 所属分类:
算法总结