fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int count = 0;
  6.  
  7. void RE(char a[]) /* 反轉字串 */
  8. {
  9. int i, j;
  10. int len = strlen(a);
  11.  
  12. for (i = 0; i < len / 2; i++)
  13. {
  14. char temp = a[i];
  15. a[i] = a[len - i - 1];
  16. a[len - i - 1] = temp;
  17. }
  18.  
  19. for (j = 0; j < len; j++) /* 如果開頭是0的話每一位往前 */
  20. if (a[0] == '0')
  21. {
  22. for (i = 0; i < len - 1; i++)
  23. {
  24. a[i] = a[i + 1];
  25. }
  26. a[i] = '\0';
  27. }
  28. else
  29. break;
  30. return;
  31. }
  32.  
  33. int main()
  34. {
  35. int n, i, j;
  36.  
  37. scanf("%d", &n);
  38.  
  39. for (i = 0; i < n; i++) /* N筆測資 */
  40. {
  41. count = 0;
  42. char a[50];
  43. char b[50];
  44.  
  45. scanf("%s", a);
  46.  
  47. long long int num;
  48.  
  49. while (1)
  50. {
  51.  
  52. strcpy(b, a);
  53.  
  54. RE(b);
  55.  
  56.  
  57.  
  58. if (!strcmp(a, b) && count != 0)
  59. {
  60. printf("%d %s\n", count, a);
  61. break;
  62. }
  63.  
  64. else
  65. {
  66. num = atoll(a) + atoll(b); /*轉成long long int 輸出 */
  67.  
  68. count++;
  69.  
  70. sprintf(a, "%lld", num);
  71. }
  72. }
  73. }
  74. return 0;
  75. }
  76.  
Time limit exceeded #stdin #stdout 5s 9424KB
stdin
Standard input is empty
stdout
Standard output is empty