• Vertical Histogram C语言 POJ2136


    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
    

    我觉得这题最坑的一点是:这题有多个测试样例,但题没说。我以为只有4行输入,结果一直wa,心态那个炸,最后是看别人的代码才发现有多个输入。

    我使用gets来读取输入,每输入一行,计算每个字母出现的频率并将频率存入数组,每读取4次就输出,输出后将输入次数置0、用来保存字母频率的数组置0。

    输出之前先找到频率的最大值,记为max,然后从最高的那行开始,从上往下,从左往右输出,行号:1<=j<=max。在每一行输出前,先要找到这行的右边界。对右边界之前每一列,如果a[i]>=j,则输出”* “,否则输出”  “,右边界这一列输出”* "。

    最后输出底行。

     1 //注意,测试数据有多组
     2 #include <stdio.h>
     3 
     4 int main(void){
     5     //num存储每个字母出现的频率,i用于遍历,j用于遍历和记录输入次数,max是最高的频率 
     6     int num[26],i,j=0,max=0;
     7     int r;//右边界
     8     char b;
     9     char a[73];//存储输入
    10     for(i=0;i<26;i++) num[i]=0;//num数组置0
    11     while(gets(a)!=NULL){
    12         j++;//成功输入
    13         for(i=0;a[i]!='';i++){//计算并记录此次输入的字符串中字母的频率
    14             if(a[i]>='A'&&a[i]<='Z') num[(int)a[i]-65]+=1;
    15         }
    16         if(j==4){//输入4次后开始输出
    17             for(i=0;i<26;i++) if(num[i]>max) max=num[i];//找到最大频率
    18             for(i=max;i>0;i--){//每换一行都要找一次右边界
    19                 //找到本行的右边界
    20                 for(j=25;j>=0;j--){
    21                     if(num[j]>=i){
    22                         r=j;
    23                         break;
    24                     }
    25                 }
    26                 //右边界以前的列
    27                 for(j=0;j<r;j++){
    28                     if(num[j]>=i) printf("* ");
    29                     else printf("  ");
    30                 }
    31                 //右边界这一列
    32                 printf("*
    ");
    33             }
    34             //输出底行
    35             for(i=0;i<25;i++){
    36                 printf("%c ",'A'+i);
    37             }
    38             printf("Z");
    39             //重置num,j,max
    40             for(i=0;i<26;i++) num[i]=0;
    41             j=0;max=0;
    42         }
    43     }
    44     return 0;
    45 }
    View Code
  • 相关阅读:
    【原创】go语言学习(十六)接口
    【原创】go语言学习(十五)IO操作2
    【原创】go语言学习(十四)IO操作1
    【原创】go语言学习(十三)struct介绍2
    【原创】go语言学习(十二)struct介绍1
    【原创】go语言学习(十一)package简介
    【原创】sed正则表达式替换
    【原创】go语言学习(十)Map类型
    【原创】go语言学习(九)指针类型
    【原创】go语言学习(八)切片
  • 原文地址:https://www.cnblogs.com/20174317zhuyuan/p/9389496.html
Copyright © 2020-2023  润新知