首页 > ACM题库 > HDU-杭电 > hdu 2708 Vertical Histogram-模拟-[解题报告]C++
2014
02-14

hdu 2708 Vertical Histogram-模拟-[解题报告]C++

Vertical Histogram

问题描述 :

Write a program to read four lines of upper case (i.e., all CAPITAL LETTERS) text input (no more than 72 characters per line) from the input file and print a vertical histogram that shows how many times each letter (but not blanks, digits, or punctuation) appears in the all-upper-case input. Format your output exactly as shown.

输入:

* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.

输出:

* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.

样例输入:

THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
THIS IS AN EXAMPLE TO TEST FOR YOUR
HISTOGRAM PROGRAM.
HELLO!

样例输出:

                            *
                            *
        *                   *
        *                   *     *   *
        *                   *     *   *
*       *     *             *     *   *
*       *     * *     * *   *     * * *
*       *   * * *     * *   * *   * * * *
*     * * * * * *     * * * * *   * * * *     * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

#include <iostream>
#include <string>
using namespace std;

int letter[300];
char str[300],map[300][300];

int main()
{

#ifndef ONLINE_JUDGE
	freopen("2.txt","r",stdin);
#endif

	int i,j;
	while (gets(str))		//read four line at one time
	{
		memset(letter,0,sizeof letter);
		for (j=0; str[j]; ++j)
		{
			if (isupper(str[j]))
				++letter[str[j]-'A'];
		}
		for (i=0; i < 3; ++i)
		{
			gets(str);
			for (j=0; str[j]; ++j)
			{
				if (isupper(str[j]))
					++letter[str[j]-'A'];
			}
		}					//end read

		memset(map,' ',sizeof map);
	
		for (j=0; j < 26; ++j)	//calculate how many time letter appears
		{
			for (; letter[j] > 0; --letter[j])
			{
				map[letter[j]][j+j] = '*';
			}
		}
		
		for (i=1; ; ++i)		//ignore the ending spaces
		{
			for (j=52; j >= 0; --j)
			{
				if (map[i][j] == '*')
				{
					map[i][j+1] = '\0';
					break;
				}
			}
			if (j < 0)
				break;
		}
		while (--i)
			puts(map[i]);
		puts("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
	}
	return 0;
}

解题转自:http://blog.csdn.net/linraise/article/details/19163961


  1. I go through some of your put up and I uncovered a good deal of expertise from it. Many thanks for posting this sort of exciting posts

  2. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c

  3. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?