【Algorithm】基础算法学习

1.猜价格游戏

#include
#include
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
#include

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

int isPrime(int n)
{
    int i;
    for(i=2;i

4.任意次方后的最后三位数

#include

int main(void)
{
	int i,x,y,z=1;
	printf("输入底数和幂数:\n");
	scanf("%d%d",&x,&y);
	for(i=0;i=100)
		printf("%d^%d的最后三位数是%d\n",x,y,z);
	else
		printf("%d^%d的最后三位数是0%d\n",x,y,z);
	return 0;
}

5.计算某日是该年的第几天

#include

/*判断是否为闰年*/
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







你可能感兴趣的:(Algorithm)