猜正數(shù)游戲镀虐,用戶給出要猜的數(shù)和至多可猜的次數(shù)
代碼
//猜正數(shù)游戲,用戶給出要猜的數(shù)和至多可猜的次數(shù)
#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;
}
輸出
求前n項(xiàng)的和
//求序列前n項(xiàng)和: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;
}
輸出
約分最簡(jiǎn)分式:2/4->1/2
//約分最簡(jiǎn)分式
#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);
//輾轉(zhuǎn)相除法求最大公約數(shù)
a = divident;
b = divisor;
while(b!=0)
{
t = a%b;
a = b;
b = t;
}
printf("最大公約數(shù):%d\n",a);
printf("最簡(jiǎn)分式:%d/%d",divident/a,divisor/a);
return 0;
}
輸出
輸出數(shù)字對(duì)應(yīng)的拼音:200->er ling ling, -4->fu si
//輸出數(shù)字對(duì)應(yīng)的拼音: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;
//求可用來(lái)除的最大倍數(shù)
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;
}
輸出
求 輸入a,n,計(jì)算a+aa+aaa+aaaa+...
//輸入a,n,計(jì)算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;
}
輸出