您好我想按照外观的降序打印给定的数字。 例子{1 2 5 4 3 1 6 3 1 3 5 1} 产量 1给4次 3给3次 5给2次 2给1次 ............... ............... 数组已排序。 无论如何,这是我的代码。如果有人有任何想法请添加。
int n,i,j,a,count,prev;
int pin1[SIZE];
for(i=0;i<=SIZE;i++)
{ do{
printf("Give a positive number (1..9): ");
scanf("%d",&n);
pin1[i]=n;
if(n<=0 || n>9)
{
printf("Give number within range (1..9) please!\n");
}
}while(n<=0 || n>9);
}
printf("the numbers you typed are:[");
for(i=0;i<=SIZE;i++)
{
printf("%d ",pin1[i]);
}
printf("]");
for(i=0;i<SIZE;++i)
{
for(j=i+1;j<=SIZE;++j)
{
if(pin1[i]<pin1[j])
{
a=pin1[i];
pin1[i]=pin1[j];
pin1[j]=a;
}
}
}
printf("\nthe numbers you typed sorted descending are:[");
for(i=0;i<=SIZE;i++)
{
printf("%d ",pin1[i]);
}
printf("]");
prev=pin1[0];
count=1;
for (i=1;i<=SIZE;i++)
{
if (pin1[i]==prev)
count++;
else
{
printf("\nthe number %d is given %d times",prev,count);
prev=pin1[i];
count=1;
}
}//for last number of array
printf("\nthe number %d is given %d times",prev,count);
}
答案 0 :(得分:0)
你有大量的&lt; = SIZE调节你的for
循环。应该&lt; SIZE ...
int n, i, j, a, count, prev;
int pin1[SIZE];
for (i = 0; i < SIZE; i++)
{
do{
printf("Give a positive number (1..9): ");
scanf("%d", &n);
pin1[i] = n;
if (n <= 0 || n>9)
{
printf("Give number within range (1..9) please!\n");
}
} while (n <= 0 || n>9);
}
printf("the numbers you typed are:[");
for (i = 0; i < SIZE; i++)
{
printf("%d ", pin1[i]);
}
printf("]");
for (i = 0; i<SIZE-1; ++i)
{
for (j = i + 1; j < SIZE; ++j)
{
if (pin1[i]<pin1[j])
{
a = pin1[i];
pin1[i] = pin1[j];
pin1[j] = a;
}
}
}
printf("\nthe numbers you typed sorted descending are:[");
for (i = 0; i < SIZE; i++)
{
printf("%d ", pin1[i]);
}
printf("]");
prev = pin1[0];
count = 1;
for (i = 1; i < SIZE; i++)
{
if (pin1[i] == prev)
count++;
else
{
printf("\nthe number %d is given %d times", prev, count);
prev = pin1[i];
count = 1;
}
}//for last number of array
printf("\nthe number %d is given %d times", prev, count);
你应该真的使用qsort
它会为你做的工作。还有一个计数器错误会导致嵌套循环中出现不可预测的内存错误。这也已经改变了。但逻辑似乎很合理。
更新============================================== ===========================
鉴于你的澄清,我认为这应该做你想要的。令我感到惊讶的是,这里的人们还没有成功。我有&#34;重构&#34;代码稍微。今天才接受这个。
#define SIZE 6
typedef struct
{
int num;
int counter;
} Inputs;
void sort_(const void* data1, const void* data2)
{
Inputs* input1 = (Inputs*)data1;
Inputs* input2 = (Inputs*)data2;
if (input1->counter > input2->counter) return -1;
if (input1->counter < input2->counter) return 1;
if (input1->num > input2->num) return -1;
if (input1->num < input2->num) return 1;
return 0;
}
int main(int argc, char** argv)
{
int n, i, j, a, count, prev;
Inputs pin1[SIZE];
/* initialise array elements */
for (i = 0; i < SIZE; i++)
{
pin1[i].counter = 0;
pin1[i].num = -1;
}
count = 0;
j = 0;
while (1)
{
if (j == SIZE) break;
printf("Give a positive number (1..9): ");
scanf("%d", &n);
if (n <= 0 || n > 9)
{
printf("Give number within range (1..9) please!\n");
continue;
}
/* check if it already exists and increment the counter */
for (i = 0; i < count; i++)
{
if (n == pin1[i].num)
{
pin1[i].counter++;
pin1[i].num = n;
break;
}
}
if (i == count)
{
pin1[count].counter++;
pin1[count].num = n;
count++;
}
j++;
}
qsort(pin1, count, sizeof(Inputs), sort_);
printf("the numbers you typed are:[");
for (i = 0; i < count; i++)
{
printf("(%d of %d) ", pin1[i].counter, pin1[i].num);
}
printf("]\n");
while (1){};
return 0;
}