Advertisement
NickAndNick

В одном шаге от счастья

Mar 13th, 2013
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <string>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class ticket {
  7. public:
  8.     ticket(int);
  9.     bool test() { return happy; }
  10. private:
  11.     void search();
  12.     int sum(int);
  13.     int prev, current, next;
  14.     bool happy;
  15. };
  16.  
  17. int main() {
  18.     ifstream ifs;
  19.     ifs.open("INPUT.TXT", ifstream::in);
  20.     if (ifs.is_open()) {
  21.         int size;
  22.         ifs >> size;
  23.         if (size) {
  24.             ofstream ofs;
  25.             ofs.open("OUTPUT.TXT", ifstream::out);
  26.             if (ofs.is_open()) {
  27.                 string yes("Yes\n"), no("No\n"), sum("");
  28.                 int next;
  29.                 for (int n = 0; n < size; n++) {
  30.                     ifs >> next;
  31.                     ticket tick(next);
  32.                     sum += tick.test() ? yes : no;
  33.                 }
  34.                 ofs << sum;
  35.                 ofs.close();
  36.             }
  37.         }
  38.         ifs.close();
  39.     }
  40.     return 0;
  41. }
  42.  
  43. ticket::ticket(int _ticket) : prev(_ticket - 1), current(_ticket), next(_ticket + 1), happy(false) {
  44.     if (current > 1 && current < 999999) search();
  45. }
  46.  
  47. void ticket::search() {
  48.     int head = current / 1000, temp = head * 1000, tail = current - temp;
  49.     if (abs(sum(head) - sum(tail)) == 1) {
  50.         head = prev / 1000;
  51.         tail = prev - temp;
  52.         if (sum(head) == sum(tail)) happy = true;
  53.         else {
  54.             head = next / 1000;
  55.             tail = next - temp;
  56.             if (sum(head) == sum(tail)) happy = true;
  57.         }
  58.     }
  59. }
  60.  
  61. int ticket::sum(int _num) {
  62.     return _num / 100 + _num % 10 + (_num / 10) % 10;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement