HDU 1073- Online Judge

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('\t'), or enters('\n'), 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. 
InputThe 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. 
OutputFor 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
 
思路:
1. 将正确答案数据与用户输入数据读入存于char数组,然后对这两个数组进行比对,如果一样则Accept。
2. 若不一样则将数组中的制表符,空格与换行符去掉,在此比较,若一样则为Presentation Error,否则WA。
代码如下:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdbool.h>
#define START 1
#define END 2
#define DATA 3
using namespace std;
int readline(char *line) {
	char ch;
	int i = 0;
	while ((ch = getchar()) != '\n')
		line[i++] = ch;
	line[i++] = '\n';
	line[i] = '\0';
	if (!strcmp("START\n",line))
		return START;
	if (!strcmp("END\n",line))
		return END;
	else
		return DATA;
}
char* toactual(const char  *data) {
	char *ac=new char[5005];
	int j = 0;
	for (int i = 0; data[i] != '\0'; i++)
		if (data[i] != ' '&&data[i] != '\n'&&data[i] != '\t')
			ac[j++] = data[i];
	ac[j] = '\0';
	return ac;
}
bool cmp(char *data, char *input) {
	for (int i = 0; input[i] != '\0'; i++)
		if (data[i] == input[i])
			continue;
		else 
			return false;
	return true;
}
int main(void) {
	int t;
	scanf("%d", &t);
	while (t--) {
		getchar();
		char data[5005] = {'\0'};
		char input[5005] = { '\0' };
		char line[1000];
		readline(line);
		while (readline(line)!=END)
			strcat(data, line);
		readline(line);
		while (readline(line) != END)
			strcat(input, line);
		if (cmp(data, input))
		{
			printf("Accepted\n");
			continue;
		}
		char *a = toactual(data);
		char *b = toactual(input);
		if (cmp(a, b))
		{
			printf("Presentation Error\n");
		}
		else {
			printf("Wrong Answer\n");
		}
	}
	return 0;
}
 有一个问题尚未解决:
代码中加粗的地方,若是换成
  char ac[5005];
主函数调用时toactual改变input时,会改变前面的a,也就是说函数内若是直接定义数组,两次调用时返回的地址是同一个地址
而用new开辟的数组两次调用返回的地址不一样。具体原因还需要查找些资料。若有知道为什么的,欢迎讨论 :)

猜你喜欢

转载自blog.csdn.net/Titanium_S/article/details/79151963
今日推荐