2014年3月4日 星期二

UVa OJ - 10062 Tell me the frequencies!

The following program is my ACcepted code for UVA-10062 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UVA 10062 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

這題被 "A blank line should separate each set of output. " 偷襲到了。
是每個test case 中間要格一行空白,最後一個case後面不能空白
所以我在除了第一個case後面的每個case都先輸出一行空白。

Because the problem said "A blank line should separate each set of output.",
between cases, there should be a blank line. (the last case is not needed)


//This program is for UVA 10062 Tell me the frequencies!
//題目來源 Problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=1003

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
    char s[1010];
    int ascii[500];
    int t = 0;
 
    //use gets(), not scanf() because we should get "one line." (there may be space included)
    while(gets(s) != NULL)
    {
        if(t != 0)//not the first test case
            printf("\n");
         
        int len = strlen(s);
     
        memset(ascii, 0, sizeof(ascii));
         
        for(int i = 0; i < len; i++)
            ascii[ s[i] ] ++;
     
        //search from the small frequency
        for(int frequency = 1; frequency <= 1000; frequency++)
        {
            //if there are same frequency ascii, it will printf the larger one first.
            for(int asc = 499; asc >= 0; asc --)
                if(ascii[asc] == frequency)
                    printf("%d %d\n", asc, frequency);
        }
        t++;
    }
    return 0;
}


Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D 

歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!