阶乘和数.c 787 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. }