当前位置: 代码迷 >> 综合 >> Call me father
  详细解决方案

Call me father

热度:99   发布时间:2023-12-04 12:10:45.0

Call me father

  • 1.题目
  • 2.分析
  • 3.代码
    • 第一次尝试:(?)[未考虑读取到文件末尾]
      • 优化:(√)
    • 第二次尝试:(?)[RANDOM 写成了 Random]
    • 第三次(√):考虑全乎
  • 3.总结
  • 4.更新日志

1.题目

2.分析

第一想法,用结构体分别写入数据
后来发现直接字符数组即可

3.代码

第一次尝试:(?)[未考虑读取到文件末尾]

#include <stdio.h>
#include <string.h>
struct Team
{
    char Name[4][100];  //名字 char Words[4][100];
};
int main()
{
    struct Team team;char Mode[10];int i;int signal = 0, flag = 0, temp = 0;scanf("%s", Mode);    //输入模式if (strcmp(Mode, "RANDOM") == 0)  //匹配signal = 1;for (i = 0; i < 4; i++){
    scanf("%s", team.Name[i]);    //名字scanf("%s", team.Words[i]);    //话语if (strcmp(team.Name[i], "IMfather") == 0 && strcmp(team.Words[i], "Callmefather") == 0)  //标记fatherflag = 1;if (strcmp(team.Name[i], "girl") == 0 && strcmp(team.Words[i], "father") == 0)temp = 1;                                     //记录}if (signal)      //匹配的时候和女孩保持队形{
    if (flag && temp)    //有父亲printf("father");elseprintf("hello");}else   //排位时。看队友需求{
    if (flag)printf("father");elseprintf("hello");}return 0;
}

优化:(√)

#include <stdio.h>
#include <string.h>
struct Team
{
    char Name[4][100];  //名字 char Words[4][100];
};
int main()
{
    struct Team team;char Mode[50];while (scanf("%s", Mode)!=EOF){
    int i;int signal = 0, flag = 0, temp = 0;if (strcmp(Mode, "RANDOM") == 0)  //匹配signal = 1;for (i = 0; i < 4; i++){
    scanf("%s", team.Name[i]);    //名字scanf("%s", team.Words[i]);    //话语if (strcmp(team.Name[i], "IMfather") == 0 && strcmp(team.Words[i], "Callmefather") == 0)  //标记fatherflag = 1;if (strcmp(team.Name[i], "girl") == 0 && strcmp(team.Words[i], "father") == 0)temp = 1;                                     //记录}if (signal)      //匹配的时候和女孩保持队形{
    if (flag && temp)    //有父亲printf("father\n");elseprintf("hello\n");}else   //排位时。看队友需求{
    if (flag)printf("father\n");elseprintf("hello\n");}}return 0;}

在这里插入图片描述

第二次尝试:(?)[RANDOM 写成了 Random]

#include <stdio.h>
#include <string.h>
int main()
{
    char Mode[50], Person[50], Words[50];while (scanf("%s", Mode) != EOF){
    int i, flag = 0, signal = 0, temp = 0;      //每一次重新置为0if (strcmp(Mode, "Random") == 0)signal = 1;for (i = 0; i < 4; i++){
    scanf("%s %s", Person, Words);if (strcmp(Person, "IMfather") == 0 && strcmp(Words, "Callmefather") == 0)flag = 1;if (strcmp(Person, "girl") == 0 && strcmp(Words, "father") == 0)temp = 1;}if (signal)  //匹配{
    if (flag && temp)printf("father\n");elseprintf("hello\n");}else{
    if (flag)printf("father\n");elseprintf("hello\n");}}return 0;
}

第三次(√):考虑全乎

#include <stdio.h>
#include <string.h>
int main()
{
    char Mode[50], Person[50], Words[50];while (scanf("%s", Mode) != EOF){
    int i, flag = 0, signal = 0, temp = 0;   //每一次重新置为0if (strcmp(Mode, "RANDOM") == 0)signal = 1;for (i = 0; i < 4; i++){
    scanf("%s %s", Person, Words);if (strcmp(Person, "IMfather") == 0 && strcmp(Words, "Callmefather") == 0)flag = 1;if (strcmp(Person, "girl") == 0 && strcmp(Words, "father") == 0)temp = 1;}if (signal)  //匹配{
    if (flag && temp)printf("father\n");elseprintf("hello\n");}else{
    if (flag)printf("father\n");elseprintf("hello\n");}}return 0;
}

在这里插入图片描述

3.总结

这道题,从拿到手到AC用了三个小时
反思:
题目本身的难度并不算高,但是主要是算法未考虑清楚,太着急做题,思路不清晰
齐次,注意题目给出的符号,不要自己臆想(复制粘贴)

4.更新日志

2022.3.20 整理

  相关解决方案