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

作者: XYDDD    時(shí)間: 2022-9-9 09:33
標(biāo)題: C語言的算法:三色球組合程序
學(xué)技術(shù),重在日拱一卒、一點(diǎn)一滴的積累。

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

編程思路
編程思路:用兩個(gè)for循環(huán)嵌套+if條件語句實(shí)現(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);

       }

    }

  }

}

程序運(yùn)行結(jié)果:







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