C语言,输入任意年份,判断生肖

1.

#include 
int main()
{
    char *p[12]={"鼠","牛","虎","兔","龙","蛇","马","羊","鸡","猴","狗","猪"};
    int year,a;
    printf("please input years:");
    scanf("%d",&year);
    if(year%12>=5)
    {
    	a=year%12-4;
	}
	if(year%12<5)
	{
		a=year%12+8;
	}
    printf("%d年是%s年",year,p[a]);
    
}

2.

#include
main()
{
	 int n,a;
	printf("please input your year:\n");
	scanf("%d",&n);
	if(n%12>=5)
	{
		a=n%12-4;
	}
	else if(n%12<5)
	{
		a=n%12+8;
	}
	
	switch(a){
		case 0:printf("今年是鼠年");break;
		case 1:printf("牛年");break; 
		case 2:printf("虎年");break;
		case 3:printf("兔年");break;
		case 4:printf("龙年");break;
		case 5:printf("蛇年");break;
		case 6:printf("马年");break;
		case 7:printf("羊年");break;
		case 8:printf("侯年");break;
		case 9:printf("鸡年");break;
		case 10:printf("狗年");break;
		case 11:printf("猪年");break;
	}
}

结果:C语言,输入任意年份,判断生肖_第1张图片

 

你可能感兴趣的:(c语言,算法)