我有一段非常简单的代码,可以检查给定的数字是否是排列
(位数 < 8;每个数字只显示一次)
从未知原因到我,带有标记注释 ( HERE
) 的循环只通过一次。有人可以帮我吗?
#include <stdio.h>
#include <stdbool.h>
int num_digits(int number)
{
int digits = 0;
while (number) {
number /= 10;
digits++;
}
return digits;
}
int get_n_digit(int number, int which)
{
if(which > num_digits(number))
{
printf("This number %d does not have that many digits: %d\n",number, which);
return -1;
}
int to_return = number;
int i = 0;
for(i = 0; i < num_digits(number) - which;i++)
{
int digit = number % 10;
to_return /= 10;
}
printf("get_n_digit(%d,%d)=%d\n",number,which, to_return %10);
return to_return % 10;
}
bool permutation_check(int number)
{
bool to_return = true;
if(num_digits(number) > 8)
{
printf("Your number has more than 8 digits\n");
return false;
}
int temp[10];
int i;
for( i = 0; i < 10; i++)
{
temp[i]=0;
}
printf(" for( i = 0; i < num_digits(%d) = %d; i++)\n",number, num_digits(number));
for( i = 0; i < num_digits(number); i++);
{
//HERE
printf("temp[get_n_digit(%d,%d)]++;\n",number,i);
temp[get_n_digit(number,i)]++;
if( temp[get_n_digit(number,i)] > 1)
{
to_return = false;
}
}
for( i = 0; i < 10; i++)
{
printf("%d\n",temp[i]);
}
return to_return;
}
int main(void)
{
char line[256];
int i, a;
printf("Put a number to be checked for permutation condition.\n");
if (fgets(line, sizeof(line), stdin)) {
if (1 == sscanf(line, "%d", &i)) {
printf("This many digits in this number: %d\n", num_digits(i));
if(num_digits(i)<8)
{
if(permutation_check(i))
printf("This number is a permutation.\n");
else
printf("This number is NOT a permutation.\n");
}
}
}
return 0;
}