Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. //Przemysław Jakub Kozłowski
  2. #include <cstdio>
  3. #include <bits/stdc++.h>
  4. // *** POCZATEK check.h ***
  5. //Przemysław Jakub Kozłowski
  6. //17.10.2013 23:25
  7. #include <cstdio>
  8. #include <string>
  9. #include <vector>
  10.  
  11. namespace Check
  12. {
  13. class WrongAnswer
  14. {
  15. std::string message;
  16. public:
  17. WrongAnswer() {}
  18. WrongAnswer(const char *msg) : message(msg) {}
  19. const char* what() const
  20. {
  21. return message.c_str();
  22. }
  23. };
  24. WrongAnswer WA;
  25.  
  26. inline unsigned char getChar(FILE *stream, int znak = -2)
  27. {
  28. int c = fgetc(stream);
  29. if(c == EOF) throw WA;
  30. if(znak != -2 && c != znak) throw WA;
  31. return c;
  32. }
  33. inline void getEOF(FILE *stream) // Wczytaj EOF
  34. {
  35. int c = fgetc(stream);
  36. if(c != EOF) throw WA;
  37. }
  38. inline void getSpace(FILE *stream) // Wczytaj spację
  39. {
  40. int c = fgetc(stream);
  41. if(c == EOF || c != ' ') throw WA;
  42. }
  43. inline void getNewLine(FILE *stream) // Wczytaj nową linię
  44. {
  45. int c = fgetc(stream);
  46. if(c == EOF || c != '\n') throw WA;
  47. }
  48. inline void getNewLineEOF(FILE *stream) // Wczytaj eof lub nową linię i eof
  49. {
  50. int c = fgetc(stream);
  51. if(c == '\n')
  52. {
  53. int c = fgetc(stream);
  54. if(c != EOF) throw WA;
  55. }
  56. else if(c != EOF) throw WA;
  57. }
  58. inline int ignoreSpaces(FILE *stream) // Zignoruj spacje i zwróć ich liczbę
  59. {
  60. int c, ret = 0;
  61. while((c=fgetc(stream)) == ' ') ret++;
  62. ungetc(c, stream);
  63. return ret;
  64. }
  65. inline int ignoreWhitespaces(FILE *stream) // Zignoruj spacje i zwróć ich liczbę
  66. {
  67. int c, ret = 0;
  68. do
  69. {
  70. c = fgetc(stream);
  71. if (c == ' ' || c == '\n')
  72. {
  73. ret++;
  74. }
  75. }
  76. while(c == ' ' || c == '\n');
  77.  
  78. ungetc(c, stream);
  79. return ret;
  80. }
  81. inline std::string getLine(FILE *stream)
  82. {
  83. std::string ret = "";
  84.  
  85. while(true)
  86. {
  87. char c = fgetc(stream);
  88. if (c == EOF)
  89. {
  90. ungetc(c, stream);
  91. break;
  92. }
  93. else if(c == '\n')
  94. {
  95. break;
  96. }
  97. else
  98. {
  99. ret += c;
  100. }
  101. }
  102.  
  103. return ret;
  104.  
  105. }
  106.  
  107. inline long long getLL(FILE *stream, const long long &minw, const long long &maxw) // Wczytuje long longa z przedziału [minw, maxw].
  108. {
  109. unsigned char buf[25];
  110. int bufi = 0, mnoz = 1;
  111.  
  112. for(int i = 1;;i++)
  113. {
  114. int c = fgetc(stream);
  115. if(c == EOF) break;
  116. else if(c == '-')
  117. {
  118. if(i != 1) throw WA;
  119. mnoz = -1;
  120. }
  121. else if('0' <= c && c <= '9')
  122. {
  123. if(bufi == 19) throw WA;
  124. buf[ ++bufi ] = c - '0';
  125. }
  126. else
  127. {
  128. ungetc(c,stream);
  129. break;
  130. }
  131. }
  132. if(bufi == 0) throw WA;
  133. if(buf[1] == '0' && (bufi > 1 || mnoz == -1)) throw WA;
  134.  
  135. long long ret = 0;
  136. for(int i = 1;i <= bufi;i++) ret = ret*10+mnoz*buf[i];
  137.  
  138. long long tmp = ret;
  139. for(int i = bufi;i >= 1;i--)
  140. {
  141. if(tmp%10*mnoz != buf[i]) throw WA;
  142. tmp /= 10;
  143. }
  144.  
  145. if(!(minw <= ret && ret <= maxw)) throw WrongAnswer("Wczytania liczba nie mieści się w zakresie!");
  146.  
  147. return ret;
  148. }
  149.  
  150. }
  151. // *** KONIEC check.h ***
  152. // *** POCZATEK judge.h ***
  153. //Przemysław Jakub Kozłowski
  154. // 18.10.2013 00:12
  155. #include <cstdio>
  156.  
  157. struct Judge
  158. {
  159. /* Ułatwia pisanie programów sprawdzających outy
  160. * Na począktu funkcji main należy wstawić:
  161. * Judge judge = Judge(argc, argv);
  162. * Po zainicjalizowaniu dostępne będą: judge.TESTIN, judge.APPOUT, judge.TESTOUT.
  163. * Gdy znany będzie już wynik należy wywołać: judge.accepted() lub judge.wrongAnswer().
  164. */
  165. bool diffout;
  166. FILE *TESTIN, *APPOUT, *TESTOUT, *RESULT, *DIFFOUT;
  167. Judge(int argc, char *argv[])
  168. {
  169. if(argc < 5)
  170. {
  171. printf("Sposób użycia: judge plik_wejsciowy.in sprawdzany.out poprawny.out wynik.xml [diff.out]\n");
  172. throw;
  173. }
  174. if(argc > 5) diffout = 1;
  175. else diffout = 0;
  176.  
  177. TESTIN = fopen(argv[1], "r");
  178. APPOUT = fopen(argv[2], "r");
  179. TESTOUT = fopen(argv[3], "r");
  180. RESULT = fopen(argv[4], "w");
  181. if(diffout) DIFFOUT = fopen(argv[5], "w");
  182.  
  183. if(!TESTIN || !TESTOUT || !APPOUT || !RESULT || (diffout && !DIFFOUT))
  184. {
  185. printf("Błąd przy otwieraniu plików.\n");
  186. throw;
  187. }
  188. }
  189.  
  190. ~Judge()
  191. {
  192. fclose(TESTIN);
  193. fclose(TESTOUT);
  194. fclose(APPOUT);
  195. fclose(RESULT);
  196. if(diffout) fclose(DIFFOUT);
  197. }
  198.  
  199. void writeResult(const char *msg)
  200. {
  201. fprintf(RESULT,"<?xml version=\"1.0\"?>\n");
  202. fprintf(RESULT,"<!DOCTYPE result [\n");
  203. fprintf(RESULT," <!ELEMENT result (#PCDATA)>\n");
  204. fprintf(RESULT," <!ATTLIST result outcome CDATA #REQUIRED>\n");
  205. fprintf(RESULT,"]>\n");
  206. fprintf(RESULT,"<result outcome=\"%s\">%s</result>\n",msg,msg);
  207. }
  208.  
  209. void writeDiff(const char *msg)
  210. {
  211. if(diffout) fprintf(DIFFOUT, "%s", msg);
  212. }
  213.  
  214. void accepted() {writeResult("Accepted");}
  215. void wrongAnswer() {writeResult("Wrong answer");}
  216. };
  217. // *** KONIEC judge.h ***
  218. using namespace Check;
  219. using namespace std;
  220. typedef long long LL;
  221.  
  222. // ** Właściwa część programu weryfikującego ***
  223.  
  224. void spr(FILE *testin, FILE *appout, FILE *testout)
  225. {
  226. int n = getLL(testin);
  227. getSpace(testin);
  228. int K = getLL(testin);
  229. getSpace(testin)
  230. int L = getLL(testin);
  231. getNewLine(testin);
  232.  
  233. vector<int> v(n, 0);
  234. for (int i = 0; i < n; i++) {
  235. int x = getLL(testin);
  236. v[i] = x;
  237. if (i == n - 1)
  238. getNewLineEof(testin);
  239. else
  240. getSpace(testin);
  241. }
  242.  
  243. string s = getLine(appout)
  244. if (s == "NIE") {
  245. string s2 = getLine(testout);
  246. if (s != s2)
  247. throw WrongAnswer("Bledna odpowiedz.");
  248. }
  249. else {
  250. string s2 = getLine(testout);
  251. if (s != s2)
  252. throw WrongAnswer("Bledna odpowiedz.");
  253. else {
  254. vector<int> v2(n, 0);
  255. for (int i = 0; i < n; i++) {
  256. int x = getLL(testin);
  257. v2[i] = x;
  258. if (i == n - 1)
  259. getNewLineEof(testin);
  260. else
  261. getSpace(testin);
  262. }
  263. set<int> s1, s2;
  264. for (int i = 0 ; i < n; i++) {
  265. if (v[i] == 0)
  266. s1.push(v[i]);
  267. else
  268. s2.push(v[i]);
  269. }
  270. for (auto i : s1)
  271. if (!s1.count(K-i))
  272. throw WrongAnswer("Bledna odpowiedz.");
  273. for (auto i : s2)
  274. if (!s2.count(L-i))
  275. throw WrongAnswer("Bledna odpowiedz.");
  276. }
  277. }
  278. }
  279.  
  280. // ** Koniec właściwej części programu weryfikującego ***
  281.  
  282. int main(int argc, char *argv[])
  283. {
  284. Judge judge = Judge(argc, argv);
  285.  
  286. bool result = true;
  287. string message;
  288. try
  289. {
  290. spr(judge.TESTIN, judge.APPOUT, judge.TESTOUT);
  291. }
  292. catch(WrongAnswer &wa)
  293. {
  294. result = false;
  295. message = wa.what();
  296. }
  297.  
  298. if(result)
  299. {
  300. printf("Accepted!\n");
  301. judge.accepted();
  302. }
  303. else
  304. {
  305. printf("WrongAnswer!\n");
  306. judge.wrongAnswer();
  307. if(!message.empty())
  308. {
  309. message += '\n';
  310. printf("%s", message.c_str());
  311. judge.writeDiff(message.c_str());
  312. }
  313. }
  314.  
  315. return 0;
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement