Guest User

Untitled

a guest
Apr 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5.  
  6. // Class to hold line data
  7. class line {
  8. public:
  9. int m, b;
  10.  
  11. // Returns true if this line intersects y at an x which is an integer
  12. bool intersects_y(int y)
  13. {
  14. // m * x + b = y
  15. // m * x = y - b
  16. // x = (y - b) / m
  17. double x = (y - b) / (double)m;
  18. return std::floor(x) == x;
  19. }
  20.  
  21. bool operator==(const line& l)
  22. {
  23. return m == l.m && b == l.b;
  24. }
  25. };
  26.  
  27. // Input a line (m and b)
  28. std::istream& operator>>(std::istream& in, line& l)
  29. {
  30. in >> l.m >> l.b;
  31. return in;
  32. }
  33. // Output a line
  34. std::ostream& operator<<(std::ostream& out, const line& l)
  35. {
  36. out << "y = " << l.m << "x + " << l.b;
  37. return out;
  38. }
  39.  
  40. int main() {
  41. // Use a vector to store the lines
  42. std::vector<line> lines;
  43.  
  44. int num_commands;
  45. std::cin >> num_commands;
  46.  
  47. line tmp;
  48. int to_check;
  49. int matches;
  50.  
  51. for (int i = 0; i < num_commands; i++) {
  52. char op;
  53. std::cin >> op;
  54. switch (op) {
  55. case '+':
  56. // Input a line and put into vector
  57. std::cin >> tmp;
  58. lines.push_back(tmp);
  59. break;
  60. case '?':
  61. std::cin >> to_check;
  62. matches = 0;
  63. // Check each line in the list
  64. for (auto l : lines) {
  65. if (l.intersects_y(to_check)) {
  66. matches += 1;
  67. }
  68. }
  69. std::cout << matches << 'n';
  70. break;
  71. case '-':
  72. std::cin >> tmp;
  73. lines.erase(std::find(lines.begin(), lines.end(), tmp));
  74. break;
  75. }
  76. }
  77. return 0;
  78. }
Add Comment
Please, Sign In to add comment