1、題目:
Problem Description
Input
Output
第一行Nana的排名。
第二行輸出所有女生收到的花的數量的排序結果。每兩個數之間一個空格。
Sample Input
5 3 1 4 5 3 3 6 4 1 1 4 7 0 9
Sample Output
2 1 3 3 4 5 4 0 1 1 4 7 9
2、參考代碼:
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
class LinkList{
private:
Node* head;
public:
LinkList(int* a,int n);
~LinkList();
int getLen();
void Sort();
void find(int x);
void show();
};
LinkList::LinkList(int* a,int n){
Node* r,* s;
head=new Node;
r=head;
for(int i=0;i<n;i++){
s=new Node;
s->data=a[i];
s->next=r->next;
r->next=s;
r=s;
}
r->next=NULL;
}
LinkList::~LinkList(){
Node* p,* q;
p=head;
while(p){
q=p;
p=p->next;
delete q;
}
}
int LinkList::getLen(){
int sum=0;
Node* p;
p=head->next;
while(p){
sum++;
p=p->next;
}
return sum;
}
void LinkList::Sort(){
int len=getLen();
Node* p,* q;
p=head->next;
for(int i=0;i<len;i++){
for(int j=i;j<len-1;j++){ ///注意這裏一定要是從i到len-1
if(j==i)
q=p;
q=q->next;
if(p->data>q->data){
int tmp=p->data;
p->data=q->data;
q->data=tmp;
}
}
p=p->next;
}
}
void LinkList::find(int x){
int cnt=0;
Node* p,* q;
q=head;
p=q->next;
while(p){
cnt++;
if(p->data>=x){
cout<<cnt<<endl;
break;
}
q=q->next;
p=p->next;
}
}
void LinkList::show(){
Node* p;
p=head->next;
if(p){
cout<<p->data;
p=p->next;
while(p){
cout<<" "<<p->data;
p=p->next;
}
cout<<endl;
}
}
int main()
{
int i,n,m,a[111];
while(cin>>n>>m){
for(i=0;i<n;i++)
cin>>a[i];
LinkList w(a,n);
w.Sort();
w.find(m);
w.show();
}
return 0;
}