阶乘和数(正序).c 788 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <stdio.h>
  2. int factorial(int n) {
  3. int fact = 1;
  4. int i;
  5. for (i = 1; i <= n; i++) {
  6. fact *= i;
  7. }
  8. return fact;
  9. }
  10. int main() {
  11. int num, originalNum, sum = 0;
  12. int temp, power;
  13. scanf("%d", &num);
  14. originalNum = num;
  15. printf("%d,", originalNum);
  16. temp = originalNum;
  17. power = 1;
  18. while (temp > 0) {
  19. temp /= 10;
  20. power *= 10;
  21. }
  22. power /= 10;
  23. while (power > 0) {
  24. int d = originalNum / power;
  25. printf("%d!", d);
  26. sum += factorial(d);
  27. originalNum %= power;
  28. power /= 10;
  29. if (power > 0) {
  30. printf("+");
  31. }
  32. }
  33. if (sum == num) {
  34. printf("=%d\nYes\n", num);
  35. } else {
  36. printf("=%d\nNo\n", sum);
  37. }
  38. return 0;
  39. }