STL例题

丑数 UVA-136

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500’th ugly number.

Input

There is no input to this program.

Output

Output should consist of a single line as shown below, with ‘< number >’ replaced by the number
computed.

Sample Output

The 1500’th ugly number is < number>.

用三个队列分别存放2,3,5,的倍数,每次取出三个队列队首元素中最小的数(取出来pop),然后分别乘以2,3,5并放入对应的队列

***防止重复,则只往大的数的队列里push,即从2 取出后放入2,3,5,从3取出的只放3,5,从5取出的只放回5。 ***

#include
#include
#include
#include
#include
using namespace std;
int main()
{
    queue<int>e, s, w;
    e.push(2);//er
    s.push(3);//san
    w.push(5);//wu
    int x;
    for (int i = 2; i <=1500; i++)
    {
        x = min(e.front(), min(s.front(), w.front()));
        if (x == e.front())
        {
            e.push(x*2);
            e.pop();
            s.push(x*3);
            w.push(x*5);
        }
        else if (x == s.front())
        {
            s.push(s.front()*3);
            s.pop();
            w.push(x*5);
        }
        else if (x == w.front())
        {
            w.push(w.front()*5);
            w.pop();
        }
    }
    printf("The 1500'th ugly number is %d.\n", x);
    return 0;
}

单词数 HDU-2027

Problem Description

lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。

Input

有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。

Output

每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。

Sample Input

you are my friend
#

Sample Output

4

使用字符串流,轻松处理在一行字符串中的逐个单词

#include
#include
#include
#include
using namespace std;
int main()
{
    string s;
    stringstream sin;//创建一个字符串流
    while (getline(cin, s))
    {
        sin.clear();//需要先将流清空
        map<string, int>mp;
        if (s[0] == '#')
            break;
        sin << s;
        while (sin >> s)
        {
            mp[s]++;//将这个词放入map
        }
        cout << mp.size() << endl;//有多少个不同的词,map就有多大
    }
}



The Average POJ2833

Time Limit: 6000MS Memory Limit: 10000K
Case Time Limit: 4000MS

Description

In a speech contest, when a contestant finishes his speech, the judges will then grade his performance. The staff remove the highest grade and the lowest grade and compute the average of the rest as the contestant’s final grade. This is an easy problem because usually there are only several judges.
Let’s consider a generalized form of the problem above. Given n positive integers, remove the greatest n1 ones and the least n2 ones, and compute the average of the rest.

Input

The input consists of several test cases. Each test case consists two lines. The first line contains three integers n1, n2 and n (1 ≤ n1, n2 ≤ 10, n1 + n2 < n ≤ 5,000,000) separate by a single space. The second line contains n positive integers ai (1 ≤ ai ≤ 108 for all i s.t. 1 ≤ i ≤ n) separated by a single space. The last test case is followed by three zeroes.

Output

For each test case, output the average rounded to six digits after decimal point in a separate line.

Sample Input

1 2 5
1 2 3 4 5
4 2 10
2121187 902 485 531 843 582 652 926 220 155
0 0 0

Sample Output

3.500000
562.500000

#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
    int x, y, n;
    while (cin >> x >> y >> n,x||y||n)
    {
        //两个优先队列分别放最大和最小
        priority_queue<int>s;
        priority_queue<int, vector<int>, greater<int> >t;
        double sum = 0;
        int a;
        for (int i = 0; i < y; i++)
        {
            scanf("%d", &a);
            s.push(a);
        }
        for (int i = 0; i < x; i++)
        {
            scanf("%d", &a);
            if (a < s.top())
            {
                t.push(s.top());
                s.pop();
                s.push(a);
            }
            else
            {
                t.push(a);
            }
        }
        for (int i = 0; i < n-x-y; i++)
        {
            scanf("%d", &a);
            if (a < s.top())
            {
                sum += s.top();
                s.pop();
                s.push(a);
            }
            else if(a>t.top())
            {
                sum += t.top();
                t.pop();
                t.push(a);
            }
            else
            {
                sum += a;
            }
        }
        printf("%.6f\n", sum / (n - x - y));
    }
}

你可能感兴趣的:(解题报告,STL)