HDU-2020
絕對值排序
?
Problem Description 輸入n(n<=100)個整數(shù),按照絕對值從大到小排序后輸出。題目保證對于每一個測試實例,所有的數(shù)的絕對值都不相等。??
Input 輸入數(shù)據(jù)有多組,每組占一行,每行的第一個數(shù)字為n,接著是n個整數(shù),n=0表示輸入數(shù)據(jù)的結(jié)束,不做處理。? Output 對于每個測試實例,輸出排序后的結(jié)果,兩個數(shù)之間用一個空格隔開。每個測試實例占一行。 Sample Input 3 3 -4 2 4 0 1 2 -3 0 Sample Output -4 3 2 -3 2 1 0 用一個數(shù)組存原數(shù)據(jù),一個存絕對值,通過對絕對值數(shù)組的排序同時對原數(shù)組進行排序1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 int main() 6 { 7 int n; 8 int a[1000]; 9 int b[1000]; 10 int i; 11 int t,j; 12 while (1) 13 { 14 scanf("%d",&n); 15 if (!n) break; 16 for (i=1;i<=n;i++) 17 { 18 scanf("%d",&a[i]); 19 b[i]=abs(a[i]); 20 } 21 for (i=1;i<n;i++) 22 for (j=i;j<=n;j++) 23 { 24 if (b[i]<b[j]) 25 { 26 t=a[i]; 27 a[i]=a[j]; 28 a[j]=t; 29 t=b[i]; 30 b[i]=b[j]; 31 b[j]=t; 32 } 33 } 34 for (i=1;i<n;i++) printf("%d ",a[i]); 35 printf("%d\n",a[i]); 36 } 37 return 0; 38 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/leiyuxiang/p/3493298.html
總結(jié)
- 上一篇: C#中调用Windows API时的数据
- 下一篇: 编码能力的提升?_20131228