Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. void NWW(unsigned long);
  6. unsigned long NWD(int, int);
  7. unsigned long NWWmax(const std::vector<unsigned long>, int, int);
  8. int main()
  9. {
  10. unsigned short z = 0;
  11. std::cin >> z;
  12. unsigned long liczba;
  13. for(int i = 0; i < z; i++){
  14. std::cin >> liczba;
  15. NWW(liczba);
  16. }
  17. return 0;
  18. }
  19.  
  20. void NWW(unsigned long liczbaa){
  21. unsigned long number = liczbaa;
  22. std::vector < unsigned long> dzielniki;
  23. dzielniki.push_back(number);
  24. if(number%2 == 0){
  25. number = number/2;
  26. dzielniki.push_back(number);
  27. } else{
  28. number = number / 2;
  29. }
  30. while(number != 1){
  31. number--;
  32. if(liczbaa % number == 0){
  33. dzielniki.push_back(number);
  34. }
  35. }
  36. int counter = 0;
  37. int seqStart = 0;
  38. int seqEnd = 0;
  39. std::reverse(dzielniki.begin(), dzielniki.end());
  40. for(int i = 0; i < (dzielniki.size()-1); i++){
  41. if(dzielniki[i] == (dzielniki[i+1]-1)){
  42. counter++;
  43. } else if(counter > (seqEnd-seqStart)){
  44. seqStart = i-counter;
  45. seqEnd = i;
  46. counter = 0;
  47. } else if(counter == (seqEnd-seqStart)){
  48. if(dzielniki[i - counter] > dzielniki[seqStart]){
  49. continue;
  50. }
  51. }
  52. }
  53. if(seqStart == seqEnd){
  54. std::cout << "NIE" << std::endl;
  55. } else{
  56. std::cout << dzielniki[seqStart] << " " << dzielniki[seqEnd] << std::endl;
  57. }
  58. }
  59.  
  60. unsigned long NWD(int liczba1, int liczba2){
  61. if(liczba1%liczba2 == 0){
  62. return liczba2;
  63. } else {
  64. int b = liczba1%liczba2;
  65. return NWD(liczba2,b);
  66. }
  67. }
  68.  
  69. unsigned long NWWmax(const std::vector<unsigned long> &dzielniki, int start, int end){
  70. unsigned long liczba = NWW(dzielniki[start],dzielniki[start+1]);
  71. for(int i = start+2; i < end; i++){
  72. unsigned long ed = dzielniki[i];
  73. liczba = NWW(ed, liczba);
  74. }
  75. return liczba;
  76.  
  77. }
  78. unsigned long NWW(unsigned long liczba1, unsigned long liczba2){
  79. return (liczba1 * liczba2) / NWD(liczba1, liczba2);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement