homework_1

发布于 2019-06-05  1.54k 次阅读


P1177

#include<iostream>
using namespace std;
int a[100005];
void quicksort(int left,int right)
{
    int i,j,base;
    i=left; j=right;
    base=a[(i+j)/2];
    while (i<=j){
        while (a[i]<base) i++;
        while (a[j]>base) j--;
        if (i<=j){
            int t=a[i]; a[i]=a[j];a[j]=t;
            i++;
            j--;
        }
    }
    if (left<j) quicksort(left,j);
    if (i<right) quicksort(i,right);
}
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n;i++) cin>>a[i];
    quicksort(0,n-1);
    for (int i=0;i<n;i++)
     cout<<a[i]<<" ";
     return 0;
}

P1059

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 1000010
using namespace std;
int a[N];
int main()
{
    int cnt=0;
    memset(a,0,sizeof(a));
    int n;
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        int c;
        cin>>c;
        if (a[c]==0) cnt++;
        a[c]++;
    }
    cout<<cnt<<endl;
    for (int i=1;i<=N;i++)
        if (a[i]>0) cout<<i<<' ';
    cout<<endl;
    return 0;
}

P1068

#include<cstdio>
using namespace std;
int n,m,sum,k[5009],s[5009],fx,p;
int main()
{
    scanf("%d%d",&n,&m);m=m*1.5,sum=m;
    for(int i=1;i<=n;i++){scanf("%d%d",&k[i],&s[i]);}
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(s[i]<s[j])
            {
                p=s[i],s[i]=s[j],s[j]=p,
                p=k[i],k[i]=k[j],k[j]=p;
            }
            if(s[i]==s[j]&&k[i]>k[j])
            {
                p=s[i],s[i]=s[j],s[j]=p,
                p=k[i],k[i]=k[j],k[j]=p;
            }
        }
    }
    fx=s[m];
    for(int i=m;;i++)
    {
        if(s[i]==s[i+1]){sum++;}
        else{break;}
    }
    printf("%d %d",fx,sum);
    for(int i=1;i<=sum;i++){printf("\n%d %d",k[i],s[i]);}
    return 0;
}

P1781

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n,si,i;
    string a,ss;
    cin>>n;
    ss="0";
    si=0;
    for (i=1;i<=n;i++)
    {
        cin>>a;
        if (a.length()>ss.length()){ss=a;si=i;}
        else if (a>ss&&a.length()==ss.length()) {ss=a;si=i;}
    }
    cout<<si<<endl<<ss<<endl;
    return 0;
}

成绩排序

#include<iostream>
#include<algorithm>
using namespace std;
 struct ex
{
    string name;
    int val;
};
bool cmp(ex x,ex y)
{
    if (x.val!=y.val) return x.val>y.val;
    return x.name<y.name;
}
int main()
{
    int n;
    cin>>n;
    ex node[110];
    for (int i=1;i<=n;i++)
        cin>>node[i].name>>node[i].val;
    sort(node+1,node+n+1,cmp);
    for (int i=1;i<=n;i++)
        cout<<node[i].name<<' '<<node[i].val<<endl;
    return 0;
}

谁考了第k名

#include<iostream>
#include<algorithm>
using namespace std;
 struct ex
{
    int name;
    double val;
};
bool cmp(ex x,ex y)
{
     return x.val>y.val;
}
int main()
{
    int n,k;
    cin>>n>>k;
    ex node[110];
    for (int i=1;i<=n;i++)
        cin>>node[i].name>>node[i].val;
    sort(node+1,node+n+1,cmp);
    cout<<node[k].name<<' '<<node[k].val<<endl;
    return 0;
}

I laid my burdens down, now I'm traveling light.
I found my freedom now, and I'm traveling light.