/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <list> #include <algorithm> long n, m, q; long i, j; std::list<std::list<long>> lista; void wyswietl_liste() { long i=1; for(std::list<std::list<long>>::iterator it1=lista.begin(); it1 != lista.end(); ++it1) { std::cout << i++ << ":"; for (std::list<long>::iterator it2=it1->begin(); it2 != it1->end(); ++it2) std::cout << ' ' << *it2; std::cout <<"\n"; } } std::list<long> n_ty_element( long n) { for(std::list<std::list<long>>::iterator it1=lista.begin(); it1 != lista.end(); ++it1) { n--; if(n==0) return *it1; } return *lista.end(); } void sumuj(long x, long y) { std::list<long> lista1 = n_ty_element(x); std::list<long> lista2 = n_ty_element(y); std::list<long> wynik; std::list<long>::iterator it1=lista1.begin(); std::list<long>::iterator it2=lista2.begin(); while (it1 != lista1.end() && it2 != lista2.end() ) { if(*it1<*it2) { wynik.push_back(*it1); it1++; } else if(*it1>*it2) { wynik.push_back(*it2); it2++; } else { wynik.push_back(*it1); it1++; it2++; } } while (it1 != lista1.end()) { wynik.push_back(*it1); it1++; } while (it2 != lista2.end()) { wynik.push_back(*it2); it2++; } lista.push_back(wynik); } void czesc_wspolna(long x, long y) { std::list<long> lista1 = n_ty_element(x); std::list<long> lista2 = n_ty_element(y); std::list<long> wynik; std::list<long>::iterator it1=lista1.begin(); std::list<long>::iterator it2=lista2.begin(); while (it1 != lista1.end() && it2 != lista2.end() ) { if(*it1<*it2) { it1++; } else if(*it1>*it2) { it2++; } else { wynik.push_back(*it1); it1++; it2++; } } lista.push_back(wynik); } void negacja(long x) { std::list<long> lista1 = n_ty_element(x); std::list<long> wynik; long poprzedni=1; for (std::list<long>::iterator it=lista1.begin(); it != lista1.end(); ++it) { for(long i=poprzedni; i<*it; i++) wynik.push_back(i); poprzedni = (*it) + 1; } for(long i=poprzedni; i<=n; i++) { wynik.push_back(i); } lista.push_back(wynik); } int main() { std::cin >> n >> m >> q; for(i=1; i<=n; i++) { std::list<long> l; for(j=i; j<=n; j+=i) { l.push_back(j); } lista.push_back(l); } for(i=0; i<m; i++) { char operacja; std::cin>> operacja; long x, y; if(operacja=='1') // suma { std::cin >> x >> y; // std:: cout << "suma: " << x << " "<<y << "\n"; sumuj(x, y); } if(operacja=='2') //czesc wspolna { std::cin >> x >> y; // std:: cout << "czesc wspolna: " << x << " " <<y << "\n"; czesc_wspolna(x, y); } if(operacja=='3') //negacja { std::cin >> x; // std:: cout << "negacja: " << x << "\n"; negacja(x); } } // std:: cout <<"LISTA:\n"; // wyswietl_liste(); for(i=0; i<q; i++) { long x, v; std:: cin >> x >> v; std::list<long> l = n_ty_element(x); auto it = std::find(l.begin(), l.end(), v); if (it != l.end()) std:: cout << "TAK\n"; else std:: cout << "NIE\n"; } //wyswietl_liste(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | /****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <list> #include <algorithm> long n, m, q; long i, j; std::list<std::list<long>> lista; void wyswietl_liste() { long i=1; for(std::list<std::list<long>>::iterator it1=lista.begin(); it1 != lista.end(); ++it1) { std::cout << i++ << ":"; for (std::list<long>::iterator it2=it1->begin(); it2 != it1->end(); ++it2) std::cout << ' ' << *it2; std::cout <<"\n"; } } std::list<long> n_ty_element( long n) { for(std::list<std::list<long>>::iterator it1=lista.begin(); it1 != lista.end(); ++it1) { n--; if(n==0) return *it1; } return *lista.end(); } void sumuj(long x, long y) { std::list<long> lista1 = n_ty_element(x); std::list<long> lista2 = n_ty_element(y); std::list<long> wynik; std::list<long>::iterator it1=lista1.begin(); std::list<long>::iterator it2=lista2.begin(); while (it1 != lista1.end() && it2 != lista2.end() ) { if(*it1<*it2) { wynik.push_back(*it1); it1++; } else if(*it1>*it2) { wynik.push_back(*it2); it2++; } else { wynik.push_back(*it1); it1++; it2++; } } while (it1 != lista1.end()) { wynik.push_back(*it1); it1++; } while (it2 != lista2.end()) { wynik.push_back(*it2); it2++; } lista.push_back(wynik); } void czesc_wspolna(long x, long y) { std::list<long> lista1 = n_ty_element(x); std::list<long> lista2 = n_ty_element(y); std::list<long> wynik; std::list<long>::iterator it1=lista1.begin(); std::list<long>::iterator it2=lista2.begin(); while (it1 != lista1.end() && it2 != lista2.end() ) { if(*it1<*it2) { it1++; } else if(*it1>*it2) { it2++; } else { wynik.push_back(*it1); it1++; it2++; } } lista.push_back(wynik); } void negacja(long x) { std::list<long> lista1 = n_ty_element(x); std::list<long> wynik; long poprzedni=1; for (std::list<long>::iterator it=lista1.begin(); it != lista1.end(); ++it) { for(long i=poprzedni; i<*it; i++) wynik.push_back(i); poprzedni = (*it) + 1; } for(long i=poprzedni; i<=n; i++) { wynik.push_back(i); } lista.push_back(wynik); } int main() { std::cin >> n >> m >> q; for(i=1; i<=n; i++) { std::list<long> l; for(j=i; j<=n; j+=i) { l.push_back(j); } lista.push_back(l); } for(i=0; i<m; i++) { char operacja; std::cin>> operacja; long x, y; if(operacja=='1') // suma { std::cin >> x >> y; // std:: cout << "suma: " << x << " "<<y << "\n"; sumuj(x, y); } if(operacja=='2') //czesc wspolna { std::cin >> x >> y; // std:: cout << "czesc wspolna: " << x << " " <<y << "\n"; czesc_wspolna(x, y); } if(operacja=='3') //negacja { std::cin >> x; // std:: cout << "negacja: " << x << "\n"; negacja(x); } } // std:: cout <<"LISTA:\n"; // wyswietl_liste(); for(i=0; i<q; i++) { long x, v; std:: cin >> x >> v; std::list<long> l = n_ty_element(x); auto it = std::find(l.begin(), l.end(), v); if (it != l.end()) std:: cout << "TAK\n"; else std:: cout << "NIE\n"; } //wyswietl_liste(); return 0; } |