【题目自解】北京大学2015计算机学科夏令营上机考试

A:整数的个数

AC代码

#include<iostream>
using namespace std;

int a[100];
int n, c1 = 0, c5 = 0, c10 = 0;

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        if (a[i] == 1)c1++;
        if (a[i] == 5)c5++;
        if (a[i] == 10)c10++;
    }
    cout << c1 << endl;
    cout << c5 << endl;
    cout << c10 << endl;
    return 0;
}

B:过滤多余的空格

AC代码

#include<iostream>
using namespace std;

int main()
{
    char s[205];
    gets(s);
    int b = 0;
    for(int i=0;s[i]!=0;i++)
    {
        if(s[i]==' ')
        {
            if(b==0)cout<<' ';
            b=1;
        }
        else{
            cout<<s[i];
            b = 0;
        }
    }
    cout<<endl;
    return 0;
}

D:合影效果

AC代码

#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;

priority_queue<double, vector<double>, greater<double> >man;
priority_queue<double, vector<double>, less<double> >woman;

int main()
{
    int n;
    scanf("%d", &n);
    char sex[10];
    double x;
    for (int i = 0; i < n; i++)
    {
        scanf("%s%lf", sex, &x);
        if (sex[0] == 'm')man.push(x);
        if (sex[0] == 'f')woman.push(x);
    }
    while (!man.empty())
    {
        x = man.top();
        man.pop();
        printf("%.2lf ", x);
    }
    while (!woman.empty())
    {
        x = woman.top();
        woman.pop();
        if(woman.empty())printf("%.2lf", x);
        else printf("%.2lf ", x);
    }
    printf("
");
    return 0;
}
原文地址:https://www.cnblogs.com/yun-an/p/11139349.html