C语言,简单的一小题

Filo369 2016-03-05 10:02:56
在某个Code平台上面碰到的一题。不知道哪里错了,各位爷帮看下。

1、题目描述 Description

给出n和n个整数,希望你从小到大给他们排序

2、输入描述 Input Description

第一行一个正整数n

第二行n个用空格隔开的整数

3、输出描述 Output Description

输出仅一行,从小到大输出n个用空格隔开的整数

4、样例输入 Sample Input

3

3 1 2

5、样例输出 Sample Output

1 2 3

6、数据范围及提示 Data Size & Hint

1<=n<=100000

-----------------------------------------------------------------
我的代码

/*
,%%%%%%%%,
,%%/\%%%%/\%%
,%%%\c "" J/%%%
%. %%%%/ o o \%%%
`%%. %%%% _ |%%%
`%% `%%%%(__Y__)%%'
// ;%%%%`\-/%%%'
(( / `%%%%%%%'
\\ .' |
\\ / \ | |
\\/ ) | |
\ /_ | |__
(___________)))))))

*/

#include<stdio.h>
#include<stdlib.h>

int SortStr(unsigned long int* str,unsigned long int n)
{
int i,j,temp = 0;
for(i =0;i<n;i++)
for(j = i+1;j<n;j++)
if(str[j] <str[i] )
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
}
for(i = 0;i<n;i++)
{
printf("%d ",str[i]);
}

return 0;
}

int main()
{
unsigned long int i,n,*str;

scanf("%d",&n);
str = (unsigned long int*)malloc(sizeof(unsigned long int)*n);
for(i = 0;i<n;i++)
scanf("%d",&str[i]);
SortStr(str,n);
return 0;
}



时间限制: 1 s
空间限制: 128000 KB
题目等级 : 白银 Silver

在code平台运行后



不知道是哪里错,个人怀疑是因为排序算法,太耗时间。希望前辈们指点一下。

...全文
137 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Filo369 2016-03-08
  • 打赏
  • 举报
回复
非常感谢楼上个各位,一楼的正解 这是我后面用,快速排序写出来的,通过了

/*
            ,%%%%%%%%,
           ,%%/\%%%%/\%%
          ,%%%\c "" J/%%%
 %.       %%%%/ o  o \%%%
 `%%.     %%%%    _  |%%%
  `%%     `%%%%(__Y__)%%'
  //       ;%%%%`\-/%%%'
 ((       /  `%%%%%%%'
  \\    .'          |
   \\  /       \  | |
    \\/         ) | |
     \         /_ | |__
     (___________)))))))
 
*/
#include<stdio.h>
#include<stdlib.h>

void quiksort(unsigned long int a[],int low,int high)
{

    int i = low;
    int j = high;  
    unsigned long int temp = a[i]; 
  
    if( low < high)
    {          
        while(i < j) 
        {
            while((a[j] >= temp) && (i < j))
            { 
                j--; 
            }
            a[i] = a[j];
            while((a[i] <= temp) && (i < j))
            {
                i++; 
            }  
            a[j]= a[i];
        }
        a[i] = temp;
        quiksort(a,low,i-1);
        quiksort(a,j+1,high);
    }
    else
    {
        return;
    }
}

int main()
{
	unsigned long int i,n,*str;
	scanf("%d",&n);
	str = (unsigned long int*)malloc(sizeof(unsigned long int)*n);
	for(i = 0;i<n;i++)
		scanf("%d",&str[i]);
	quiksort(str,0,n-1);
	for( i = 0;i<n;i++)
		printf("%d ",str[i]);
	return 0;
}
darknoll 2016-03-05
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int num;
	cin >> num;
	vector<int> ivec;
	ivec.reserve(num);

	int temp;
	for (int i = 0; i != num; ++i)
	{
		cin >> temp;
		ivec.push_back(temp);
	}

	sort(ivec.begin(), ivec.end());

	for (auto &i : ivec)
	{
		cout << i << " ";
	}
	cout << endl;
}
lm_whales 2016-03-05
  • 打赏
  • 举报
回复
明显希望你做个快速排序

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧