百钱买百鸡(vbs写的算法)(原创)

在我国的数学经典著作"九章算术"中有这样一道题:百钱买百鸡, 公鸡5钱一只, 母鸡3钱一只, 小鸡1钱3只)求得是能有多少种办法买这些鸡.
如果看不懂的话我用大白话说说:有人要去买鸡, 用100块钱正好买了100只鸡, 价格如下:公:5$, 母:3$, 小:1$ for 3,
让你求一共多少种卖法(公母小怎么搭配). 请用循环解决这个问题.

vbs代码如下:

 1 dim gjgs,mjgs,xjgs

 2 const zqs = 100

 3 const zjs = 100

 4 const gjjg = 5 

 5 const mjjg = 3 

 6 const xjjg = 1'xjge必须是三的倍数

 7 

 8 dim maxgj,maxmj

 9 

10 maxgj = int(zqs/gjjg)

11 maxmj = int(zqs/mjjg)

12 

13 for gjgs = 0 to maxgj

14 

15     if (zjs-gjgs) < maxmj then

16 

17         for mjgs =0 to zjs -gjgs

18             

19             for xjgs = 0*3 to zjs - gjgs-mjgs

20                 if ((gjgs + mjgs + xjgs) =zjs)and((gjgs * gjjg +mjgs*mjjg +xjgs/3)=zqs) then

21                     zfc = "公鸡个数:" & gjgs & VbCrLf & "母鸡个数:" & mjgs &VbCrLf & "小鸡个数:" & xjgs

22                     msgbox zfc

23 

24                 end if

25             next

26         next

27     else

28 

29         for mjgs = 0 to maxmj

30             for xjgs = 0*3 to zjs - gjgs-mjgs

31                 if ((gjgs + mjgs + xjgs) =zjs)and((gjgs * gjjg +mjgs*mjjg +xjgs/3)=zqs) then

32                     zfc = "公鸡个数:" & gjgs & VbCrLf & "母鸡个数:" & mjgs &VbCrLf & "小鸡个数:" & xjgs

33                     msgbox zfc

34                 end if

35             next

36         next

37     end if

38 next

 

你可能感兴趣的:(vbs)