初学者提问请求支援
c语言吧
全部回复
仅看楼主
level 1
i5feifei 楼主
Write a C program, twenty_one.c, that given a list of integers calculates whether this list contains one or more integers that together sum to 21. The numbers may occur anywhere in the list. They do not have to be consecutive. The integers are supplied on the command line. You program should then print a message either indicating no combination of any of the numbers sums to 21 or giving a combination of the numbers that does sum to 21.Your may assume there will be between one and twenty integers supplied on the command line. Follow the output format indicated below:% ./twenty_one 20No combination of the numbers given sums to 21.% ./twenty_one 2121 = 21% ./twenty_one 10 3 10 110 + 10 + 1 = 21% ./twenty_one 12 1 25 7No combination of the numbers given sums to 21.% ./twenty_one 3 3 3 3 3 3 3 3 3 3 3 + 3 + 3 + 3 + 3 + 3 + 3 = 21% ./twenty_one 9 13 -1 -109 + 13 + -1 = 21*******************************************************i know how to enter numbers and store them in an arraybut how to add them randomly and get the sum ?you may not write a program for me, i just want some ideas
2008年05月31日 01点05分 1
level 1
澳大利亚。。。LAB11?this question is suitably challenging。呵呵,你只需要思想是吧;我给你说说思想;怎么来解决这个问题;首先来分析问题;输入N个数后;要求要其中任意几个的和等于21的式子给输出来;首先是任意。就是说有可能是其中一个;二个;三个;。。。。那么要怎么把这些情况都考虑了?要解决这个问题;我首先来说说二进制;呵呵,你是不是觉得我扯得太远了?慢慢来,耐心听下去。二进制你应该懂了吧?呵呵,我不想说太多;简单点;人们说起二进制说得更多是满二进一。但我现在要说说的是权。假如有个二进制数0110;它的十进制是多少呢?0110==0*2^3+1*2^2+1*2^1+0*2^0===6从这个求上我们得到什么启发?如果把0110看作是 权 8  4  2  1 这个序列真假表达;那么0110中间的二个1表示需要取8421这个序列中的中间二个数;因此0110=4+2==6那么如果给我们 8 4 2 1 这个序列,我们能通过加法能多少个不同值呢?我们只需要遍历一下它序列二进度真假表达 XXXX:0000----1111比如0000--》一个也不取0001--》取最后一个  0001===10010--》倒数第二个  0010===20011--》最后二个    0011===2+1====30100--》取第二个    0100===40101--》第二加第四个0101===4+1===5  。。。。。。  。。。。。。1111---》取全部     1111===8+4+2+1=15现在如果给定一个序列8 4 2 1 ;那么它最多就只能组成上面的值。(当然条件是序列中每个数只能用一次)现在我们要求的是只要把8421这个序列换成我们需要取遍历的数组中的值就OK了;/////// 理论分析到此结束 /////////////////////根据上面的理论足够解决你的问题,其他细节如输入数组你自己考虑好了;我们来举个例子;假如我们有一个序列 10 、1、 9、 2、 3 ;现在我们想知道从这个序列中取出任意几个;看看它能组成什么值。这序列有5个值。因此我们只需要关心它们的真假表达00000---11111就可以当该位为1的时候表示需要该值去计算;比如00101==9
+3
==12好了我们可以实现了;我们使用一个变量bit来表示00000--11111的变化;bit每次只需要加1就可以;由于5位2进制最大值11111是31;因此我们只需要k《32就可以我们还需要一个变量tmp等于bit,然后通过移位来决定序列中的数是否需要参与计算;因为循环中需要bit++;因此bit的原值不能被破环。so,需要tmp下面是我对序列10、1、8、2、3中能加出几种值的算法(不考虑重复比如不会考虑10+10+1,因为序列没有二个10);#include 
void main(){  int a[20];  int i,sum,tag,bit,tmp;  int M,N;  a[0]=10;  a[1]=1;  a[2]=9;  a[3]=2;  a[4]=3;  M=32;   N=5;//序列中数有5个   for(bit=1  bit<32 bit++)   {  tmp=bit;  tag=sum=0;  for( i=0;i
>1;  }  if(tag==1)  printf("=%d\n",sum);   }}
2008年05月31日 04点05分 2
level 1
晕;程序重发一次;百度空格把我的“ ;”号给吃掉了
2008年05月31日 04点05分 3
level 1
#include
void main(){ int a[20]; int i,sum,tag,bit,tmp; int M,N; a[0]=10; a[1]=1; a[2]=9; a[3]=2; a[4]=3; M=32; N=5;//序列中数有5个 for(bit=1 ; bit<32 ;bit++) { tmp=bit; tag=sum=0; for( i=0;i
>1; } if(tag==1) printf("=%d\n",sum); }}
2008年05月31日 04点05分 4
level 1
i5feifei 楼主
晕,只要你不是我们TUTOR就行十分感谢
2008年06月01日 10点06分 5
level 1
i5feifei 楼主
这方法真的满好的哈哈感觉我灵光一闪啊!
2008年06月01日 10点06分 6
1