2014年3月2日 星期日

UVa OJ - 10327 Flip Sort

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

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

//This program is for UVA 10327 Flip Sort
//題目來源 Problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show_problem&problem=1268

#include<stdio.h>
using namespace std;

//call by reference! So the x and y will actually swap.
void swap(int &x, int &y)
{
    int tmp;
    tmp = x;
    x = y;
    y = tmp;
}

int BubbleSort(int* number, int n)
{
    int ans = 0;
 
    for(int i = 1; i < n; i++)
        for(int j = n - 1; j >= i; j--)
            if(number[j] < number[j - 1])
            {
                swap(number[j], number[j - 1]);
                ans ++;
            }
    return ans;
}

int num[1000001];

int main()
{
    int n, swapCount;
 
    while(scanf("%d", &n) != EOF)
    {
        swapCount = 0;
     
        for(int i = 0; i < n; i++)
            scanf("%d", &num[i]);
     
        swapCount = BubbleSort(num, n);
     
        printf("Minimum exchange operations : %d\n", swapCount);
    }
    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!