Онлайн компилятор C

#include <stdio.h> int main() { long long int m; int q=0; scanf("%lld", &m); if (m == 0 ) { printf("0"); return 0; } long long int save = m; while (m>0) { m /= 10; q++; } int A[q]; m =save; int i = 0; while (m>0) { A[i] = m%10; i++; m /= 10; } int count = 0; while (count = 0) { count =1; for(int j =0; j< q-1 ; j++) { int t = A[i+1]; if (A[i]>A[i+1]) { A[i+1] = A[i]; A[i] = t; count = 0; } } } for(int j = 0 ; j < q; j++) { printf("%d",A[j]); } return 0; }
这段代码的目的是读取一个整数,然后将其各位数字按降序排序后输出。但代码中存在几个问题,导致它无法正确运行。

首先,排序部分的循环条件 `while (count = 0)` 是一个赋值语句,而不是比较。这会导致循环永远不会执行,因为 `count = 0` 的结果是 0,条件为假。你应该使用 `==` 来比较。

其次,在排序的冒泡循环中,你使用了变量 `i`,但此时 `i` 的值已经是数组的长度(即数字的位数),这会导致数组访问越界。你应该使用循环变量 `j` 来访问相邻元素,而不是 `i`。

- 检查 `while (count = 0)` 这一行,思考赋值和比较的区别。
- 检查排序循环中使用的索引变量,确保它正确遍历数组的相邻元素。