C趣味程序百例(29)搶30

C趣味程序百例(29)搶30,第1張

C趣味程序百例(29)搶30,第2張

89.搶30
這是中國民間遊戯。兩個人輪流從1開始數。每個人每次可以數一到兩個連號。誰先到30名,誰就贏了。
*問題分析和算法設計
這個問題和上一個問題差不多,算法也差不多。不同的是,誰應該邁出第一步是可選的。如果計算機邁出了第一步,那麽計算機一定是贏家。如果人先走,那麽計算機就得等著出錯。人先走,不犯錯,那麽人就贏了。否則,計算機會抓住人爲錯誤,讓自己成爲贏家。
*程序和程序注釋
# include
# include
# include
int input(int t);
int copu(int s);
void main()
{
int tol = 0;
printf(" \ n * * * * * * * * *趕三十* * * * * * \ n");
printf("遊戯開始\ n");
randomize();
if(random(2)==1)
tol = input(tol);
while(tol!=30)
if((tol = copu(tol))= = 30)
printf("我輸了!\ n");
else
if((tol = input(tol))= = 30)
printf("我輸了!\ n");
printf(" * * * * * * * *遊戯結束* * * * * * * * \ n");
}
int input(int t)
{
int a;
do{
printf("請計數:");
scanf("%d",& a);
if(a>2||a30)
printf("錯誤輸入,再次!");
else
printf("你數:%d\n",t a);
} while(a > 2 | | a30);
return t a;
}
int copu(int s)
{
int c;
printf("計算機計數:");
if((s 1)%3==0)
printf(" %d\n", s);
else if((s 2)% 3 = = 0)
{
s = 2;
printf(" %d\n",s);
}
else
{
c = random(2) 1;
s = c;
printf(" %d\n",s);
}
return s;
}

位律師廻複

生活常識_百科知識_各類知識大全»C趣味程序百例(29)搶30

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情