#include <stdio.h>
#pragma warning(disable:4996)
void one_digit_arrangement(int*, int); //正常(按個位數字排列)排列
void same_one_digit_big_to_small_arrangement(int*, int); //個位數相同時,由大到小排列
int main(void)
{
int n = 0, numbers[1000] = { 0 },counter=1;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &numbers[i]);
}
/*if (counter == 19)
{
printf("%d\n", n);
}*/
one_digit_arrangement(&numbers[0], n);
same_one_digit_big_to_small_arrangement(&numbers[0], n);
for (int i = 0; i < n; i++)
{
printf("%d ", numbers[i]);
}
printf("\n");
//counter++;
}
return 0;
}
void one_digit_arrangement(int* numbers, int n)
{
int temp = 0;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1-i; j++)
{
if (numbers[j] % 10 > numbers[j + 1] % 10)
{
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
}
void same_one_digit_big_to_small_arrangement(int* numbers, int n)
{
int temp = 0, counter = 1;
for (int i = 0; i < n; i++)
{
if (numbers[i] % 10 == numbers[i+1] % 10)
{
counter = 1;
counter++;
//檢查到底有多少個相同
for (int j = i+1; j < n; j++)
{
if (numbers[j]%10 == numbers[j + 1]%10)
{
counter++;
}
else
{
break;
}
}
//由大到小排列
for (int r = 0; r < counter-1; r++)
{
for (int q = i; q < counter-1+i-r; q++)
{
if (numbers[q] < numbers[q + 1])
{
temp = numbers[q];
numbers[q] = numbers[q + 1];
numbers[q + 1] = temp;
}
}
}
i += counter - 1;
}
}
}
#include
#pragma warning(disable:4996)
void one_digit_arrangement(int*, int); //正常(按個位數字排列)排列
void same_one_digit_big_to_small_arrangement(int*, int); //個位數相同時,由大到小排列
int main(void)
{
int n = 0, numbers[1000] = { 0 },counter=1;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &numbers[i]);
}
/*if (counter == 19)
{
printf("%d\n", n);
}*/
one_digit_arrangement(&numbers[0], n);
same_one_digit_big_to_small_arrangement(&numbers[0], n);
for (int i = 0; i < n; i++)
{
printf("%d ", numbers[i]);
}
printf("\n");
//counter++;
}
return 0;
}
void one_digit_arrangement(int* numbers, int n)
{
int temp = 0;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1-i; j++)
{
if (numbers[j] % 10 > numbers[j + 1] % 10)
{
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
}
void same_one_digit_big_to_small_arrangement(int* numbers, int n)
{
int temp = 0, counter = 1;
for (int i = 0; i < n; i++)
{
if (numbers[i] % 10 == numbers[i+1] % 10)
{
counter = 1;
counter++;
//檢查到底有多少個相同
for (int j = i+1; j < n; j++)
{
if (numbers[j]%10 == numbers[j + 1]%10)
{
counter++;
}
else
{
break;
}
}
//由大到小排列
for (int r = 0; r < counter-1; r++)
{
for (int q = i; q < counter-1+i-r; q++)
{
if (numbers[q] < numbers[q + 1])
{
temp = numbers[q];
numbers[q] = numbers[q + 1];
numbers[q + 1] = temp;
}
}
}
i += counter - 1;
}
}
}
已自行解決,問題在for的迴圈判斷少-1