#21986: WA (line:19) 求解!


ktpss97094@gmail.com (彭星樺)

學校 : 不指定學校
編號 : 86608
來源 : [49.216.40.170]
最後登入時間 :
2022-07-12 22:31:17
a225. 明明愛排列 | From: [123.195.194.142] | 發表日期 : 2020-08-08 11:58

#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;

 

}

}

 

}

 
#22012: Re:WA (line:19) 求解!


ktpss97094@gmail.com (彭星樺)

學校 : 不指定學校
編號 : 86608
來源 : [49.216.40.170]
最後登入時間 :
2022-07-12 22:31:17
a225. 明明愛排列 | From: [101.10.7.12] | 發表日期 : 2020-08-10 10:00

#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

 
ZeroJudge Forum