Advertisement
DaniDori

Наилучший делитель

Feb 3rd, 2023
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include<algorithm>
  3. #include<time.h>
  4. using namespace std;
  5. int sumOfDigits( int a) {
  6. int b=0;
  7. while (a > 0) {
  8. b += a % 10;
  9. a /= 10;
  10. }
  11. return b;
  12. }
  13.  
  14. int main()
  15. {
  16. int a, b, sumB, sumI;
  17. cin >> a;
  18. b = a;
  19. double start = clock();
  20. for (int i = 1; i <= a; i++) {
  21. if (a % i == 0) {
  22. //sumB = sumOfDigits(b);
  23. //sumI = sumOfDigits(i);
  24. if (sumOfDigits(b) < sumOfDigits(i)) {
  25. b = i;
  26. }
  27. else {
  28. if (sumOfDigits(b) == sumOfDigits(i)) {
  29. b = min(b, i);
  30. }
  31. }
  32. }
  33. }
  34. cout << b << ' ' << (double(clock()) - start) / 1000 << endl;
  35. start = clock();
  36. for (int i = 1; i <= a/2+1; i++) {
  37. if (a % i == 0) {
  38. sumB = sumOfDigits(b);
  39. sumI = sumOfDigits(i);
  40. if (sumB < sumI) {
  41. b = i;
  42. }
  43. else {
  44. if (sumB == sumI) {
  45. b = min(b, i);
  46. }
  47. }
  48. }
  49. }
  50. cout << b << ' ' << (double(clock()) - start) / 1000;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement