猜正数游戏,用户给出要猜的数和至多可猜的次数
代码
//猜正数游戏,用户给出要猜的数和至多可猜的次数
#include<stdio.h>
int main()
{
int num,n;
int guessNum;
int cnt = 0;
int finished = 0;
printf("input num and n: ");
scanf("%d %d",&num,&n);
printf("start guess:\n");
do
{
if(cnt>=n)
{
finished = 1;
printf("%d times,game over!\n",n);
}
else
{
scanf("%d",&guessNum);
cnt++;
if(guessNum<0)
{
finished = 1;
}
else if(guessNum>num)
{
printf("too big!\n");
}
else if(guessNum<num)
{
printf("too small!\n");
}
else
{
if(cnt==1)
{
printf("bingo!\n");
}
else if(cnt<=3)
{
printf("lucky you!\n");
}
else
{
printf("good guess!\n");
}
finished = 1;
}
}
}while(!finished);
return 0;
}
输出
输出.PNG
求前n项的和
//求序列前n项和:2/1+3/2+5/3+8/5+...
#include<stdio.h>
int main()
{
int n,i;
double divident = 2, divisor = 1;
double t;
double sum = 0;
printf("input n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum +=divident/divisor;
t = divident;
divident = divident+divisor;
divisor = t;
}
printf("%.2f",sum);
return 0;
}
输出
输出.PNG
约分最简分式:2/4->1/2
//约分最简分式
#include<stdio.h>
int main()
{
int divident,divisor;
int t;
int a,b;
printf("input divident/divisor,like 4/8: ");
scanf("%d/%d",÷nt,&divisor);
//辗转相除法求最大公约数
a = divident;
b = divisor;
while(b!=0)
{
t = a%b;
a = b;
b = t;
}
printf("最大公约数:%d\n",a);
printf("最简分式:%d/%d",divident/a,divisor/a);
return 0;
}
输出
输出.PNG
输出数字对应的拼音:200->er ling ling, -4->fu si
//输出数字对应的拼音:200->er ling ling, -4->fu si
#include<stdio.h>
int main()
{
int x,t,d;
int mask = 1;
printf("input x: ");
scanf("%d",&x);
if(x<0)
{
x = -x;
printf("fu ");
}
t = x;
//求可用来除的最大倍数
while(t>9)
{
t /=10;
mask = mask*10;
}
do
{
d = x/mask;
switch(d)
{
case 0: printf("ling");break;
case 1: printf("yi");break;
case 2: printf("er");break;
case 3: printf("san");break;
case 4: printf("si");break;
case 5: printf("wu");break;
case 6: printf("liu");break;
case 7: printf("qi");break;
case 8: printf("ba");break;
case 9: printf("jiu");break;
}
if(mask>9)
{
printf(" ");
}
x = x%mask;
mask = mask/10;
}while(mask>0);
return 0;
}
输出
输出.PNG
求 输入a,n,计算a+aa+aaa+aaaa+...
//输入a,n,计算a+aa+aaa+aaaa+...
//如输入2,4 输出2+22+222+2222的和
#include<stdio.h>
int main()
{
int a,n;
int i;
int sum = 0;
int t = 0;
printf("input a and n:\n");
scanf("%d %d",&a,&n);
for(i = 1;i<=n;i++)
{
t = t*10+a;
sum +=t;
}
printf("\nsum=%d",sum);
return 0;
}
输出
输出.PNG
网友评论