1.猜价格游戏
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int oldprice,price = 0 ,i= 0;
printf("请输入真实商品的价格:");
scanf("%d",&oldprice);
system("cls");
printf("请输入试猜的价格:\n");
while(oldprice != price)
{
i++;
printf("参与者:");
scanf("%d",&price);
printf("主持人:");
if(price > oldprice)
printf("高了\n");
else if(price < oldprice)
printf("低了\n");
else
printf("恭喜你,答对了,一共答了%d次\n",i);
}
return 0;
}
2.数制转换
#include<stdio.h>
#include<string.h>
void convto(char *s,int n,int b)
{
char bit[] = {"0123456789ABCDEF"};
int len;
if(n == 0)
{
strcpy(s,"");
return;
}
convto(s,n/b,b);
len = strlen(s);
s[len] = bit[n%b];
s[len+1] = '\0';
}
int main()
{
char s[80];
int i,base,old;
printf("请输入十进制数:");
scanf("%d",&old);
printf("请输入转换的进制:");
scanf("%d",&base);
convto(s,old,base);
printf("%s\n",s);
return 0;
}
3.100以内的素数之和
#include<stdio.h>
int isPrime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i == 0) return 0;
}
return 1;
}
int main()
{
int sum = 0;
int i;
for(i=1;i<=100;i++)
{
if(isPrime(i))
{
sum = sum + i;
}
}
printf("1至100所有素数之和为: %d\n",sum);
return 0;
}
4.任意次方后的最后三位数
#include<stdio.h>
int main(void)
{
int i,x,y,z=1;
printf("输入底数和幂数:\n");
scanf("%d%d",&x,&y);
for(i=0;i<y;i++)
{
z = z*x%1000;
}
if(z>=100)
printf("%d^%d的最后三位数是%d\n",x,y,z);
else
printf("%d^%d的最后三位数是0%d\n",x,y,z);
return 0;
}
5.计算某日是该年的第几天
#include<stdio.h>
/*判断是否为闰年*/
int leapYear(int a)
{
if(a%4==0 && a%100!=0 || a%400==0)
return 1;
else
return 0;
}
/*计算输入日期是一年中的第几天*/
int calc(int year,int month,int day)
{
int sum=0,i;
int a[12] ={31,28,31,30,31,30,31,31,30,31,30,31};
int b[12] ={31,29,31,30,31,30,31,31,30,31,30,31};//闰年每月的天数
if(leapYear(year) == 1)
for(i=0;i<month-1;i++)
sum += b[i];
else
for(i=0;i<month-1;i++)
sum += a[i];
sum += day;
return sum;
}
int main(void)
{
int year,month,day,total;
printf("请输入年月日:");
scanf("%d%d%d",&year,&month,&day);
total = calc(year,month,day);
printf("第%d天\n",total);
return 0;
}