2017-01-23 98 views
0

我寫了這個代碼打印所有從1到n的強數字,但根本沒有得到輸出。運行該程序後終端卡住了,我不知道錯在哪裏。請糾正我。C程序打印所有從1到n的強數字

145是強一些,因爲1! + 4! + 5! == 145

#include <stdio.h> 

void main() { 
    int i = 1, fact, sum, n, a; 
    long int number; 

    printf("\n Find Strong numbers between 1 to \n"); 
    scanf("\n%ld", &number); 

    printf("\n All Strong numbers between 1 to %ld are:\n", number); 

    for (int j = 1; j <= number; j++) { 
     sum = 0; 

     while (j != 0) { 
      a = j % 10; 
      j = j/10; 
      fact = 1; 

      while (i <= a) { 
       fact = fact * a; 
       a--; 
      } 
      sum = sum + fact; 
     } 
     if (j == sum) 
      printf("\n%d\n", j); 
    } 
} 
+0

如果您正在從根本上走錯了就是'for'循環通過增加'j'進行迭代,並且第一個內部'while'循環將'j'減少到零。結合起來,這會造成無限循環。這也意味着'j == sum'永遠不會是真的,所以不會產生輸出。結合起來,這會使代碼看起來「卡住」 - 程序不斷循環,不產生任何輸出。最後,'main()'返回'int',而不是'void'。 – Peter

+0

我不相信在'\ n'中使用scanf。 請看這裏,如何使用長整數的scanf。 [看這裏](http://stackoverflow.com/questions/2852390/using-scanf-in-c-c) – allevo

回答

0

你的代碼是停留在一個無限循環比較總和此修正J因您的另一循環內的循環變量的使用。 for循環的變量j在每次迭代中增加1。但是在同樣的迭代中,你將j除以10直到它爲0.這就是爲什麼在for循環的每次迭代開始時j總是1。

解決方案是簡單地爲while循環使用一個額外的變量,並將其初始化爲該迭代的j值。請參閱下面的代碼來解決這個問題。

void main() 
{ 
    int i=1,fact,sum, n, a, tmp; 
    long int number; 

    printf("\n Find Strong numbers between 1 to \n"); 
    scanf("\n%ld",&number); 

    printf("\n All Strong numbers between 1 to %ld are:\n",number); 

    for(int j=1;j<=number;j++) 
    { 
     tmp = j; 
     sum=0; 

     while(tmp!=0) 
     { 
      a=j%10; 
      tmp=tmp/10; 
      fact=1; 

      while(i<=a) 
      { 
       fact=fact*a; 
       a--; 
      } 
      sum=sum+fact; 
     } 
     if(j==sum) 
      printf("\n%d\n",j); 

    } 
} 
+0

非常感謝。 –

0
j=j/10; 

是影響學家然後,您在

if(j==sum) 
    printf("\n%d\n",j); 
+0

非常感謝,我現在明白了 –

1
#include <stdio.h> 
#include <conio.h> 

void main() { 
    char next_time; 
    int num, fact, n, sum = 0, i, value; 
    next_time = 'y'; 
    while (next_time == 'y' || next_time == 'Y') { 
     printf("Enter a number to check whether a number is strong or not:\t"); 
     scanf("%d", &num); 
     value = num; 

     while (num != 0) { 
      fact = 1; 
      n = num % 10; 
      num = num/10; 
      for (i = 1; i <= n; i++) { 
       fact *= i; 
      } 
      sum += fact; 
     } 
     if (sum == value) 
      printf("%d is strong number", value); 
     else 
      printf("%d is not strong number", value); 

     printf("\n\n******************************"); 
     printf("\n\nDo you want to start again?"); 
     printf("\n\nEnter Y or y to to continue and any other key to exit:\t"); 
     scanf(" %c", &next_time); 
     printf("\n*********************************************\n\n"); 
    } 
    getch(); 
} 

使用增量運算即i++和比較原始號碼的總和。

+0

你的程序不回答這個問題:* C程序打印1到n的所有強數字* – chqrlie

0

您的程序失敗,因爲您修改循環內的循環計數器j以枚舉其數字。您也忘記在循環內重新初始化i

下面是一個簡單和快速的版本:

#include <stdio.h> 

int main(void) { 
    unsigned long int factorials[10]; 
    unsigned long int number; 

    factorials[0] = 1; 
    for (int i = 1; i < 10; i++) { 
     factorials[i] = factorials[i - 1] * i; 
    } 

    printf("Find Strong numbers from 1 to "); 
    scanf("%lu", &number); 

    printf("\nAll Strong numbers between 1 to %ld are:\n", number); 

    for (unsigned long int j = 1; j <= number; j++) { 
     long int n, sum = 0; 
     for (n = j; n > 9; n /= 10) { 
      sum += factorials[n % 10]; 
     } 
     sum += factorials[n]; 
     if (j == sum) { 
      printf("%ld\n", j); 
     } 
    } 
    return 0; 
} 
0

代碼在C找到強大的數字高達10萬 OUTPUT

#include<stdio.h> 
int main() { 
    int ino = 0; 
    int newno = 0; 
    int digit = 1; 
    int fact = 1; 
    int i; 
    for(i = 0; i <= 100000; i++) 
    { 
     ino = i; 
     newno = 0; 
     while(ino != 0) 
     { 
      fact = 1; 
      digit = ino % 10; 
      while(digit > 1) 
      { 
       fact *= digit--;    
      } 
      newno += fact; 
      ino /= 10; 
     } 

     if(i == newno) 
     printf("%d, ",i); 
    } 
    printf("\b\b "); 
return 0; 
} 
相關問題