fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int OneDigit(int x) //Kiem tra xem co phai 1 chu so hay khong?
  5. {
  6. if (x>=0 && x<=9)
  7. return 1;
  8. return 0;
  9. }
  10.  
  11. int SumOfDigits(int x) //Tinh tong cac chu so cua mot so
  12. {
  13. int S = 0;
  14. while (x!=0)
  15. {
  16. int mod = x%10;
  17. S+=mod;
  18. x=x/10;
  19. }
  20. return S;
  21. }
  22.  
  23. int FinalDigit (int x)
  24. {
  25. while (1)
  26. {
  27. if (OneDigit(x)==1)
  28. break;
  29.  
  30. x = SumOfDigits(x);
  31. }
  32. return x;
  33. }
  34.  
  35. int main ()
  36. {
  37. int T;
  38. cin>>T;
  39. while (T--)
  40. {
  41. int x;
  42. cin>>x;
  43. cout<<FinalDigit(x)<<endl;
  44. }
  45. return 0;
  46. }
Success #stdin #stdout 0s 4328KB
stdin
3
1009
167
102
stdout
1
5
3