• HDU-1703 Online Judge (练习处理输入)


    一、题目概述

     
    Ignatius is building an Online Judge, now he has worked out all the problems except the Judge System. The system has to read data from correct output file and user's result file, then the system compare the two files. If the two files are absolutly same, then the Judge System return "Accepted", else if the only differences between the two files are spaces(' '), tabs(' '), or enters(' '), the Judge System should return "Presentation Error", else the system will return "Wrong Answer".

    Given the data of correct output file and the data of user's result file, your task is to determine which result the Judge System will return.
     
    Input
    The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
    Each test case has two parts, the data of correct output file and the data of the user's result file. Both of them are starts with a single line contains a string "START" and end with a single line contains a string "END", these two strings are not the data. In other words, the data is between the two strings. The data will at most 5000 characters.
     
    Output
    For each test cases, you should output the the result Judge System should return.
     
    Sample Input
    4
    START
    1 + 2 = 3
    END
    START
    1+2=3
    END
     
     
    START
    1 + 2 = 3
    END
    START
    1 + 2 = 3
    END
     
     
    START
    1 + 2 = 3
    END
    START
    1 + 2 = 4
    END
     
     
    START
    1 + 2 = 3
    END
    START
    1 + 2 = 3
    END
     
    Sample Output
    Presentation Error
    Presentation Error
    Wrong Answer
    Presentation Error

     

    二、题目释义

    获取输入,比对两个字符串,根据结果输出不同的答案

    三、思路分析

    这道题关键在于怎么处理输入,这里就需要许多对输入、字符、字符串的处理,一个一个读字符可以读取所有(回车符什么的都可以),但这样做这道题效率会偏低,这里我们用cin.getline()来读取整行,用strcmp()来做读取时转存的比较,用strcat()保存完成的字符串拼接,以正确获取两个answer。之后再做判断处理,判断相对读取是比较简单的。

    四、AC代码

    代码借鉴自其他博主

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <cstring>
    using namespace std;
    
    const int N = 5050;
    void input(char a[]);
    void simplify(char a[]);
    int cmp(char a[],char b[]);
    char s1[N]={0},s2[N]={0};
    
    int main(){
        int n;
        scanf("%d",&n);
        while(n--)
        {
            memset(s1,0,sizeof(s1));
            memset(s2,0,sizeof(s2));
            input(s1),input(s2);
            int m = cmp(s1,s2);
            switch(m)
            {
            case 1:
                printf("Accepted
    ");break;
            case 2:
                printf("Presentation Error
    ");break;
            case 3:
                printf("Wrong Answer
    ");break;
            }
        }
        return 0;
    }
    void input(char a[])
    {
        char s[N];
        while(scanf("%s",s),strcmp(s,"START"));
        while(cin.getline(s,N),strcmp(s,"END"))
        {
            if(s[0] != '')
                strcat(a,s);
            else strcat(a,"
    ");
        }
    }
    
    void simplify(char a[])
    {
        int i,k=0;
        char s[N];
        for(i=0; a[i]!=''; i++)
        {
            if(a[i]==' '||a[i]=='
    '||a[i]=='	')
                continue;
            s[k++]=a[i];
        }
        s[k++]='';
        strcpy(a,s);
    }
    
    int cmp(char a[],char b[])
    {
        if(!strcmp(a,b))
            return 1;
        simplify(a),simplify(b);
        if(!strcmp(a,b))
            return 2;
        return 3;
    }
  • 相关阅读:
    C#中测量消耗的内存和时间的方法
    log4net和log2console的配置
    C#多语言编程
    C#和Qt实现的对于异常处理的一个使用策略
    分享一个小工具,提供源码,以便根据自己需要进行修改
    适用于关系型数据库的抽象工厂的数据库访问类
    一万小时定律(真正科学的学习方法)
    动态的SQL分页
    【转】数据库查询优化原则
    ssh 登录出现的几种错误以及解决办法
  • 原文地址:https://www.cnblogs.com/EcliWalker/p/8329159.html
Copyright © 2020-2023  润新知