Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<algorithm>
- #include<time.h>
- using namespace std;
- int sumOfDigits( int a) {
- int b=0;
- while (a > 0) {
- b += a % 10;
- a /= 10;
- }
- return b;
- }
- int main()
- {
- int a, b, sumB, sumI;
- cin >> a;
- b = a;
- double start = clock();
- for (int i = 1; i <= a; i++) {
- if (a % i == 0) {
- //sumB = sumOfDigits(b);
- //sumI = sumOfDigits(i);
- if (sumOfDigits(b) < sumOfDigits(i)) {
- b = i;
- }
- else {
- if (sumOfDigits(b) == sumOfDigits(i)) {
- b = min(b, i);
- }
- }
- }
- }
- cout << b << ' ' << (double(clock()) - start) / 1000 << endl;
- start = clock();
- for (int i = 1; i <= a/2+1; i++) {
- if (a % i == 0) {
- sumB = sumOfDigits(b);
- sumI = sumOfDigits(i);
- if (sumB < sumI) {
- b = i;
- }
- else {
- if (sumB == sumI) {
- b = min(b, i);
- }
- }
- }
- }
- cout << b << ' ' << (double(clock()) - start) / 1000;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement