有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
Some parts use both Chinese and English, but some parts use only one language. Feel free to share, but please contact me first and list this blog as your reference.

2014年3月2日 星期日

UVa OJ - 11321 Sort! Sort!! and Sort!!!

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

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

點這裡看題目 Click here to see this Problem!

這題跟我之前所解的 UVA 612 DNA Sorting 寫法蠻相似的
請參考這理的解說:

This problem is similar with one of my solved problem:  UVA 612 DNA Sorting
Please see this for reference.


//This program is for UVA 11321 Sort! Sort!! and Sort!!!
//題目來源 Problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2296

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

typedef struct
{
    int num;
    int reminder;
}strSet;

/*
注意: 當你在判斷奇數或偶數時,不要使用 (a.num % 2)==1 來判斷奇數
因為當數字為負數時, (a.num % 2)是 -1
我使用 (a.num % 2)==1 拿了很多次 runtime error (RE)
所以我推薦直接使用 (a.num % 2) 來代表為奇數 (代表1 or -1)
(因為在C++判斷式中,非零就是代表 true!)

Note: when you judge the odd or even, don't use (a.num % 2)==1 to mean the odd!!!
Because when the number could be negative, (a.num % 2) would be -1 !
I used (a.num % 2)==1 to get runtime error (RE) several times.
So I recommend only use (a.num % 2) to mean the odd. (That means 1 or -1)
(In the condition, non-zero represent true!)
*/

bool cmp(strSet a, strSet b)
{
    if(a.reminder != b.reminder)
        return a.reminder < b.reminder;
    if( (a.num % 2) && (b.num % 2) )//both of them are odd
        return a.num > b.num;//larger precede the smaller one
    else if( (a.num % 2 == 0) && (b.num % 2 == 0) )//both of them are even
        return a.num < b.num;//smaller precede the larger one
    else if( (a.num % 2) && (b.num % 2 == 0) )//a is odd, b is even
        return true;//odd number precede the even
    else  if( (a.num % 2 == 0) && (b.num % 2) )//b is odd, a is even
        return false;//odd number precede the even
    return true;
}

int main()
{
    int n, m;
    vector<strSet> set;
   
    while(scanf("%d %d", &n, &m) != EOF)
    {  
        printf("%d %d\n", n, m);
       
        if(n == 0 && m == 0)
            break;
       
        set.clear();
       
        for(int i = 0; i < n; i++)
        {
            strSet tmp;
            scanf("%d", &tmp.num);
            tmp.reminder = tmp.num % m;
            set.push_back(tmp);
        }
       
        //sort set[0] to set[n-1]
        sort(set.begin(), set.end(), cmp);
       
        for(int i = 0; i < n; i++)
            printf("%d\n", set[i].num);
    }
    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!