6-14 查找星期 (15 point(s))

By | 最新修改:2024-08-17

声明

这是 拼题A(PTA)《中M2019秋C入门和进阶练习集》的习题。原题在 https://pintia.cn/problem-sets/1163286449659043840/problems/1174288506294865933 (侵删)

本人的答案仅供交流学习,请勿用于当作答案来提交!

题目描述

6-14 查找星期 (15 point(s))

本题要求实现函数,可以根据下表查找到星期,返回对应的序号。

序号 星期

0   Sunday
1   Monday
2   Tuesday
3   Wednesday
4   Thursday
5   Friday
6   Saturday

函数接口定义:

int getindex( char *s );

函数getindex应返回字符串s序号。如果传入的参数s不是一个代表星期的字符串,则返回-1。

裁判测试程序样例:

#include 

#define MAXS 80

int getindex( char *s );

int main()
{
    int n;
    char s[MAXS];

    scanf("%s", s);
    n = getindex(s);
    if ( n==-1 ) printf("wrong input!\n");
    else printf("%d\n", n);

    return 0;
}

// 你的代码将被嵌在这里

输入样例1:
Tuesday
输出样例1:
2
输入样例2:
today
输出样例2:
wrong input!

我的答案

/*================================================================
*   Copyright (C) 2019 程序知路. All rights reserved.
*   
*   Filename    :6-14-查找星期.c
*   Author      :程序知路
*   E-Mail      :admin@icxzl.com
*   Create Date :2019年10月08日
*   Description :
================================================================*/
#include 
#include 

#define MAXS 80

int getindex( char *s );

int main()
{
    int n;
    char s[MAXS];

    scanf("%s", s);
    n = getindex(s);
    if ( n==-1 ) printf("wrong input!\n");
    else printf("%d\n", n);

    return 0;
}

// 以下是有效代码
#include 
int getindex( char *s ) {
    char week[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    for (int i = 0; i 


程序知路

鉴于本人的相关知识储备以及能力有限,本博客的观点和描述如有错漏或是有考虑不周到的地方还请多多包涵,欢迎互相探讨,一起学习,共同进步。

本文章可以转载,但是需要说明来源出处!

本文使用的部分图片来源于网上,若是侵权,请与本文作者联系删除: admin@icxzl.com