首页 > ACM题库 > HDU-杭电 > HDU 1004 Let the Balloon Rise-字符串-[解题报告] C++
2013
11-26

HDU 1004 Let the Balloon Rise-字符串-[解题报告] C++

Let the Balloon Rise

问题描述 :

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges’ favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.This year, they decide to leave this lovely job to you.

输入:

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) — the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.A test case with N = 0 terminates the input and this test case is not to be processed.

输出:

For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.

样例输入:

5
green
red
blue
red
red
3
pink
orange
pink
0

样例输出:

red
pink

本题练习穷举。

题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=1004

本题以前做过,之所以记录本题,是因为看到一种生成回文数的有效方法,套用此模板可以快速生成1 – 10^9范围内的回文数表。虽然看似繁杂,但是却非常有效。

本题是求一定范围内的所有回文素数。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int a,b;
    scanf("%d %d",&a,&b);

    int Palind[110000],pnum=0;
    int n,m,k,p,q;

    //求所有的回文数

    for(n=1; n<=9; n++)
        Palind[pnum++]=n;
    for(n=1; n<=9; n++)
        Palind[pnum++]=11*n;
    for(n=1; n<=9; n++)
        for(m=0; m<=9; m++)
            Palind[pnum++]=101*n+10*m;
    for(n=1; n<=9; n++)
        for(m=0; m<=9; m++)
            Palind[pnum++]=1001*n+110*m;
    for(n=1; n<=9; n++)
        for(m=0; m<=9; m++)
            for(k=0; k<=9; k++)
                Palind[pnum++]=10001*n+1010*m+100*k;
    for(n=1; n<=9; n++)
        for(m=0; m<=9; m++)
            for(k=0; k<=9; k++)
                Palind[pnum++]=100001*n+10010*m+1100*k;
    for(n=1; n<=9; n++)
        for(m=0; m<=9; m++)
            for(k=0; k<=9; k++)
                for(p=0; p<=9; p++)
                    Palind[pnum++]=1000001*n+100010*m+10100*k+1000*p;
    for(n=1; n<=9; n++)
        for(m=0; m<=9; m++)
            for(k=0; k<=9; k++)
                for(p=0; p<=9; p++)
                    Palind[pnum++]=10000001*n+1000010*m+100100*k+11000*p;
    for(n=1; n<=9; n++)
        for(m=0; m<=9; m++)
            for(k=0; k<=9; k++)
                for(p=0; p<=9; p++)
                    for(q=0; q<=9; q++)
                        Palind[pnum++]=100000001*n+10000010*m+1000100*k+101000*p+10000*q;
    //判断素数
    for(int i=0;i<pnum;i++)
    {
        if(Palind[i]>b)
        {
            break;
        }
        int q = sqrt(Palind[i]) + 1;
        int flag = 0;
        for(int j = 2;j<q;j++)
        {
            if(Palind[i]%j == 0)
            {
                flag = 1;
                break;
            }
        }
        if(flag == 0 && Palind[i]>=a)
        {
            printf("%d\n",Palind[i]);
        }
    }

    return 0;
}

  1. 第二块代码if(it != mp.end())应改为if(it != mp.end() && (i+1)!=(it->second +1));因为第二种解法如果数组有重复元素 就不正确

  2. 代码是给出了,但是解析的也太不清晰了吧!如 13 abejkcfghid jkebfghicda
    第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3),为什么要这样拆分,原则是什么?

  3. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }