关于ACM的一道题,无法通过编译,求大神!
题目在这:http://acmoj.shu.edu.cn/openjudge/viewproblem.php?coll_id=1&prob_id=10
- C/C++ code
#include<iostream>#include<string>using namespace std;struct link{ string a; link *next; };void sort(int *temp,int a){ int i,j,pos,tem; for (int i = 0;i < a-1;i++) { pos = i; for (int j = i+1;j < a;j++) { if (*(temp+pos) > *(temp+j)) { pos = j; } } tem = *(temp+pos); *(temp+pos) = *(temp+i); *(temp+i) = tem; }} int main(){ int a,i=0; string p; char q[10]; link *head,*l,*temp; head=l=new link; while(cin>>a) { p=""; int *h=new int[a]; for(i=0;i<a;i++) { cin>>*(h+i); } sort(h,a); for(i=0;i<a;i++) { if(i!=(a-1)) { itoa(*(h+i),q,10); p=p+q+" "; } else { itoa(*(h+i),q,10); p=p+q; } } l->a=p; temp=new link; l->next=temp; temp->next=NULL; l=temp; } while(head->next!=NULL) { cout<<head->a<<endl; head=head->next; } return 0;} 这段代码无法通过编译,但是在dev-c++可以运行,求大神帮忙指点下
[解决办法]
题目人家不都说了么?1≤n≤1000)看好题目啊。
楼主的一个排序想的太复杂了,居然整出list。我觉得如果你想省事,就直接像上面那位仁兄一样,用一个sort就解决问题了。如果想自己来实现呢,可以使用的排序方法多了去了。堆排序,选择排序,插入排序,快速排序等等,都可以自己实现下,作为练习。
[解决办法]
一样的,只要输出格式对了就行,你也可能把数据一下全部读进去,再全部一次输出,但这样要太大的内存,没必要,只要读入一组输出一组就好。