POJ 2136 Vertical Histogram
2015-06-17 19:12
393 查看
分析:很久以前,在《K&R》上面碰到过这个题,只不过比这个复杂一点。。。。
也是水题,没什么说的,注意一下细节,比如输出的格式等,还有就是,一开始用的for循环,每输入一行结束后就直接打印,好郁闷^~_~^
Description
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.
Input
* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.
Output
* Lines 1..??: Several lines with asterisks and spaces followed by one line with the upper-case alphabet separated by spaces. Do not print unneeded blanks at the end of any line. Do not print any leading blank lines.
Sample Input
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int len,i,j,maxhigh=0;
string s;
int c[26];
memset(c,0,sizeof(c));
while (cin>>s)
{
len=s.length();
for(i=0;i<len;i++)
if(s[i]>=65 && s[i]<=90)
++c[s[i]-'A'];
}
for(i=0;i<26;i++)
if(maxhigh<c[i])
maxhigh=c[i];
for(i=maxhigh;i>0;i--)
{
for(j=0;j<26;j++)
if(c[j]>=i)
printf("* ");
else
printf(" ");
printf("\n");
}
for(i=0;i<26;i++)
{
if(i>0)
printf(" ");
printf("%c",i+'A');
}
printf("\n");
return 0;
}
Description
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.
Input
* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.
Output
* Lines 1..??: Several lines with asterisks and spaces followed by one line with the upper-case alphabet separated by spaces. Do not print unneeded blanks at the end of any line. Do not print any leading blank lines.
Sample Input
Sample Output
也是水题,没什么说的,注意一下细节,比如输出的格式等,还有就是,一开始用的for循环,每输入一行结束后就直接打印,好郁闷^~_~^
Description
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.
Input
* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.
Output
* Lines 1..??: Several lines with asterisks and spaces followed by one line with the upper-case alphabet separated by spaces. Do not print unneeded blanks at the end of any line. Do not print any leading blank lines.
Sample Input
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. THIS IS AN EXAMPLE TO TEST FOR YOUR HISTOGRAM PROGRAM. HELLO!
Sample Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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 <cstdio>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int len,i,j,maxhigh=0;
string s;
int c[26];
memset(c,0,sizeof(c));
while (cin>>s)
{
len=s.length();
for(i=0;i<len;i++)
if(s[i]>=65 && s[i]<=90)
++c[s[i]-'A'];
}
for(i=0;i<26;i++)
if(maxhigh<c[i])
maxhigh=c[i];
for(i=maxhigh;i>0;i--)
{
for(j=0;j<26;j++)
if(c[j]>=i)
printf("* ");
else
printf(" ");
printf("\n");
}
for(i=0;i<26;i++)
{
if(i>0)
printf(" ");
printf("%c",i+'A');
}
printf("\n");
return 0;
}
Description
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.
Input
* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.
Output
* Lines 1..??: Several lines with asterisks and spaces followed by one line with the upper-case alphabet separated by spaces. Do not print unneeded blanks at the end of any line. Do not print any leading blank lines.
Sample Input
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. THIS IS AN EXAMPLE TO TEST FOR YOUR HISTOGRAM PROGRAM. HELLO!
Sample Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++中拷贝构造函数的应用详解
- C++中引用(&)的用法与应用实例分析
- C++使用CriticalSection实现线程同步实例
- C++智能指针实例详解
- 解析C++ 浮点数的格式化输出