C语言习题 输出月份

C语言习题 输出月份

时间: 1ms        内存:128M

描述:

 编写一程序,输入月份号,输出该月的英文月名。例如,输入3,则输出March,要求用指针数组处理。

输入:

月份号

输出:

英文月名

示例输入:

3
12
13

示例输出:

March
December
Wrong!

提示:

参考答案(内存最优[748]):

/*C语言习题 输出月份*/
#include<stdio.h>
int main()
{
	int n;
	char *month[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
	while(scanf("%d",&n)!=EOF)
	{
		if(n<=12&&n>=1)
			printf("%s\n",month[n-1]);
		else
			printf("Wrong!\n");
	}
	return 0;
}

参考答案(时间最优[0]):

#include <iostream>
using namespace std;
int main()
{
    char *month_name[13]= {"illegal month","January","February","March","April",
                           "May","June","July","August","September","October", "November","December"
                          };
    int n;
    while(cin>>n)
        if ((n<=12) && (n>=1))
            cout<<*(month_name+n)<<endl;
        else
            cout<<"Wrong!"<<endl;
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注