Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 以用户给定的一位数字打印所有麻醉数字_C_While Loop_Nested Loops - Fatal编程技术网

C 以用户给定的一位数字打印所有麻醉数字

C 以用户给定的一位数字打印所有麻醉数字,c,while-loop,nested-loops,C,While Loop,Nested Loops,我试着打印用户输入的数字的所有自恋数字 例如,对于输入3,程序应打印:153370371407。现在由于某种原因,它没有打印数字,而是什么也不打印,程序被卡住了 #include <stdio.h> #include <math.h> int main() { int digit, a, c = 0; unsigned long long int count, b, sum; printf("Enter digits to check narci

我试着打印用户输入的数字的所有自恋数字

例如,对于输入
3
,程序应打印:
153370371407
。现在由于某种原因,它没有打印数字,而是什么也不打印,程序被卡住了

#include <stdio.h>
#include <math.h>

int main() {
    int digit, a, c = 0;
    unsigned long long int count, b, sum;
    printf("Enter digits to check narcisistic: ");
    scanf("%d", &digit);
    count = pow(10, digit - 1);
    if (digit > 2) {
        while (count < pow(10, digit)) {
            b = count;
            sum = 0;
            while (count >= 1) {
                a = b % 10;
                b /= 10;
                sum += pow(a, digit);
            }
            if (sum == count) {
                printf("\n Narcissistic found:\t%llu", count);
                c++;
            }
            count++;
        }
        if (c == 0)
            printf("No Narcissistic number for this digit.");
    }
    return 0;
}
#包括
#包括
int main(){
整数位数,a,c=0;
无符号长整型计数,b,和;
printf(“输入数字以检查麻醉:”);
扫描频率(“%d”和数字);
计数=功率(10,数字-1);
如果(数字>2){
while(计数<功率(10位)){
b=计数;
总和=0;
而(计数>=1){
a=b%10;
b/=10;
总和+=功率(a,数字);
}
如果(总和==计数){
printf(“\n发现自恋:\t%llu”,计数);
C++;
}
计数++;
}
如果(c==0)
printf(“此数字无自恋数字”);
}
返回0;
}
这个代码有什么问题

  while(count>=1){
      a=b%10;
      b/=10;
      sum+=pow(a,digit);
  }
count
在这个循环中从不改变,所以它将永远循环

count
在这个循环中永远不会改变,所以它将永远循环。

根据@dcp的回答。 内部while循环将永远不会退出。你应该循环的是数字的数量。例如(在前面将digNum声明为int之后):

根据@dcp的回答。 内部while循环将永远不会退出。你应该循环的是数字的数量。例如(在前面将digNum声明为int之后):


是的,但是使用for循环和具有有意义名称的变量会使代码的意图更加明显。干净的可维护代码不会再次困扰您。是的,但是使用for循环和具有有意义名称的变量会使代码的意图更加明显。干净的可维护代码是不会再困扰你的代码。
    for(digNum = digit; digNum > 0; digNum--)
    {
        a = b%10;
        b /= 10;
        sum+=pow(a,digit);
    }