Created
November 24, 2014 16:51
-
-
Save yxjxx/74a258fbca8a71918cb7 to your computer and use it in GitHub Desktop.
int sum_to_digit(int a)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <complex> | |
using namespace std; | |
int HowMuchBit(int elem){ | |
int bit = 1; | |
int p = 10; | |
while(elem >= p){ | |
p *= 10; | |
bit++; | |
} | |
return bit; | |
} | |
int sum_to_digit(int a){ | |
int bit = HowMuchBit(a); | |
int sum = 0; | |
for (int i = 1; i <= bit; ++i) { | |
//double pow(double x, double y); | |
sum += (a % (int)pow(10,i)) / (int)pow(10,i-1); | |
} | |
if(sum >= 10){ | |
return sum_to_digit(sum); | |
} | |
else{ | |
return sum; | |
} | |
} | |
int main() { | |
cout << HowMuchBit(12333) << endl; | |
cout << sum_to_digit(123456); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment