C语言紧急求救!!! 这个程序运行时间太长了,怎么改,是他优化一下?非常感谢!!!!!!

C语言紧急求救!!! 这个程序运行时间太长了,怎么改,是他优化一下?非常感谢!!!!!!,第1张

#include<stdio.h>

int main(){

int i,n,m,a,b,count=0

int facsum(int n)

scanf("%d%d",&m,&n)

for(m<=nm++) {

a=facsum(m)

if(m<a)

b=facsum(a)

if(m==b&&m<a)

printf("%5d%5d\n",m,a)

if(m==a)

count++

}

printf("%5d\n",count)

return 0

}

int facsum(int n){

int i,s=0,m=n/2

for(i=1i<=mi++){

if(n%i==0)

s=s+i

}

return s

}

将双重循环改为单次循环,可大大减少运行时间!

C语言写的21点程序!

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define NULL 0

int compare(char sum1,char sum2)

{

if(sum1>=sum2)

return 1

else

return 0

}

int breakal(int sum)

{

if(sum>21)

return 0

else

return 1

}

char change(int card)

{

char ch,n,m,x

switch(card)

{

case 1: ch='A'

break

case 10:

{

n=1+int(rand()/(RAND_MAX+1.0))

if(n==1)

ch='K'

else

{

m=1+int(rand()/(RAND_MAX+1.0))

if(m==1)

ch='Q'

else

{

x=1+int(rand()/(RAND_MAX+1.0))

if(x==1)

ch='J'

}

}

break

}

} //以上switch语句用于将数字转换成字符

return ch

}

void game()

{

int i,k,n

char host_ch,player_ch,host_card[15],player_card[15]

int host_sum=0,player_sum=0

srand(time(NULL))

for(i=0i<2i++)

{

int (10.0*rand()/(RAND_MAX+1.0))

player_card[i]=1+int (10.0*rand()/(RAND_MAX+1.0))

player_ch=change(player_card[i])

if(player_card[i]>1 &&player_card[i]<10) //输出玩家抽到的牌的点数

printf("您抽到的第%d张牌是%d\n",i+1,player_card[i])

else if(player_card[i]==10)

printf("您要到的第%d张牌是%c\n",i+1,player_ch)

else

printf("您要到的第%d张牌是A\n",i+1)

if(player_ch=='A') //询问判断玩家的A的点值为1或11

{

printf(" 您有一张A,您想要A作1还是11?\n")

printf(" 1. 1\n" )

printf(" 2. 11\n")

if(scanf("%d",&n) &&n==1)

player_card[i]=1

else

player_card[i]=11

}

host_card[i]=1+int (10.0*rand()/(RAND_MAX+1.0))

host_ch=change(host_card[i])

if(host_ch=='A') //电脑自动判断庄家的A的点值为1或11

{

k=1+int(rand()/(RAND_MAX+1.0))

if(k==1)

host_card[i]=11

else

host_card[i]=1

}

player_sum+=player_card[i] //分别对庄家和玩家的点数求和

host_sum+=host_card[i]

}

printf("您现在的总面值为%d\n",player_sum)//输出玩家的总面值

for(i=2i<5i++)

{

if(host_sum<18 &&host_sum>0)//电脑自动判断庄家是否要牌

{

host_card[i]=1+int (10.0*rand()/(RAND_MAX+1.0))

host_ch=change(host_card[i])

if(host_ch=='A') //电脑自动判断庄家的A的点值为1或11

{

k=1+int(rand()/(RAND_MAX+1.0))

if(k==1)

host_card[i]=11

else

host_card[i]=1

}

host_sum+=host_card[i]

}

printf("您还要继续要牌吗?\n")

printf(" 1.要\n")

printf(" 2.不要\n")

scanf("%d",&k)

if(k==1)

{

{

player_card[i]=1+int (10.0*rand()/(RAND_MAX+1.0))

player_ch=change(player_card[i])

if(player_ch=='A') //询问判断玩家的A的点值为1或11

{

printf("您有一张A,您想要A作1还是11?\n")

printf(" 1. 1\n" )

printf(" 2. 11\n")

if(scanf("%d",&n) &&n==1)

player_card[i]=1

else

player_card[i]=11

printf("您要到的第%d张牌是A\n",i+1)

}

else

printf("您要到的第%d张牌是%d\n",i+1,player_card[i])

player_sum+=player_card[i]

if(i==4)

printf("您手中已有5张牌,一决胜负的时刻到啦!\n")

if(breakal(host_sum)==0)

{

if(breakal(player_sum)==0)

{

printf("庄家爆牌了,您也爆牌了!庄家的牌面值是%d,您的牌面值是%d\n",host_sum,player_sum)

printf("\nYou lose..\n")

break

}

}

else

if(breakal(player_sum)==0)

{

printf("您爆牌了!您的牌面值是%d\n \nYou lose..\n",player_sum)

break

}

}

if(breakal(host_sum)==0)

{

printf("庄家爆牌了!庄家的牌面值是%d\n",host_sum)

printf("\nCongratulations! YOu win!\n")

break

}

}

else

{

printf("庄家的牌面值是%d,您的牌面值是%d\n",host_sum,player_sum)

if(compare(host_sum,player_sum)==1)

printf("\nYou lose..\n")

else

printf("\nCongratulations! You win!\n")

break

}

}

}

int main()

{

kaishi:

int n

printf(" 欢迎进入21点游戏!\n")

printf("************************************\n")

printf(" 1.开始游戏\n")

printf(" 2.退出游戏\n")

printf("************************************\n")

if(scanf("%d",&n)&&n==1)

game()

else if(n==2)

{

printf(" ******************************\n")

printf("游戏结束!祝您愉快!\n")

printf("\n\n")

printf(" 再见!\n")

printf(" ******************************\n")

return 0

}

goto kaishi

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12121493.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存