練習題目1(沒事多練習)

一、雞兔同籠

題目:某個籠子裡有一群雞和一群兔子,已知總共有 m 隻腳及 n 個頭,求雞和兔子的個數。

輸入:每行有兩個整數 m、n , m 為籠子裡的腳數, n 為籠子裡頭的個數。

輸出:每行輸出兩個數字,分別是雞的個數及兔子的個數。

測試輸入資料:
36 10
44 20
142 40
268 100
666 200

輸出結果:
2 8
18 2
9 31
66 34
67 133

#include<stdio.h>
int main(){
int foot,head,ck,rb;
do{
printf("輸入腳數、頭數:");
scanf("%d %d",&foot,&head);
if(foot%2!=0||head>foot/2){
printf("無解(無效)的輸入\n請重新");
}
}while(foot%2!=0||head>foot/2);

ck=(4*head-foot)/2;
rb=head-ck;
printf("*ck*%d\n*rb*%d\n",ck,rb);

return 0;
}

留言