標題: C語言的算法:三色球組合程序 [打印本頁]

作者: XYDDD    時間: 2022-9-9 09:33
標題: C語言的算法:三色球組合程序
學技術,重在日拱一卒、一點一滴的積累。

今天,我們一起來分析C語言的算法:三色球組合。
算法:在一個盒子里裝有三色球,其中紅色的有3個,白色的也有3個,黑色的有6個,問當從盒子中取出8個球時共有多少種可能的方案?

編程思路
編程思路:用兩個for循環(huán)嵌套+if條件語句實現(xiàn)。

程序范例

#include <stdio.h>


unsigned char i,j;

int main()

{

  for(i=0; i<=3; i++)

  {

    for(j=0;j<=3;j++)

    {

       if((8-i-j)<= 6)

      {

          printf("黑球:%d,白球:%d,紅球:%d\r\n",i,j,8-i-j);

       }

    }

  }

}

程序運行結果:







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1