Skip to content

Instantly share code, notes, and snippets.

@yxjxx
Created November 24, 2014 16:51
Show Gist options
  • Save yxjxx/74a258fbca8a71918cb7 to your computer and use it in GitHub Desktop.
Save yxjxx/74a258fbca8a71918cb7 to your computer and use it in GitHub Desktop.
int sum_to_digit(int a)
#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