绝对值排序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 96343 Accepted Submission(s): 45470
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <math.h>
void main()
{
int n;
int aa[1000];
int bb[1000];
int i,j;
int tempaa,tempbb;
while(~scanf("%d",&n))
{
if (n==0)
return;
memset(aa,0,n);
memset(bb,0,n);
for(i=0;i<n;i++)
{
scanf("%d",&aa[i]);
if(aa[i]<0)
{
aa[i] = 0-aa[i];
bb[i] = 1;
}
}
//冒泡排序
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(aa[j]<aa[j+1])
{
tempaa = aa[j];
aa[j] = aa[j+1];
aa[j+1] = tempaa;
tempbb = bb[j];
bb[j] = bb[j+1];
bb[j+1] = tempbb;
}
}
}
for(i=0;i<n;i++)
{
if(bb[i]==1)
printf("%d",-aa[i]);
else
printf("%d",aa[i]);
if(i!=n-1)
printf(" ");
}
printf("\n");
}
}
正确答案:
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <math.h>
void main()
{
int n;
int aa[1000];
int i,j;
int tempaa,tempbb;
while(~scanf("%d",&n))
{
if (n==0)
return;
memset(aa,0,n);
for(i=0;i<n;i++)
{
scanf("%d",&aa[i]);
}
//冒泡排序
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(abs(aa[j])<abs(aa[j+1]))
{
tempaa = aa[j];
aa[j] = aa[j+1];
aa[j+1] = tempaa;
}
}
}
for(i=0;i<n;i++)
{
printf("%d",aa[i]);
if(i!=n-1)
printf(" ");
}
printf("\n");
}
}