UVA12004 Bubble Sort【数学】

Check the following code which counts the number of swaps of bubble sort.

int findSwaps( int n, int a[] )

{

    int count = 0, i, j, temp, b[100000];

    for( i = 0; i < n; i++ ) {

        b[i] = a[i];

    }

   for( i = 0; i < n; i++ ) {

        for( j = 0; j < n - 1; j++ ) {

            if( b[j] > b[j+1] ) {

               temp = b[j];

               b[j] = b[j+1];

               b[j+1] = temp;

               count++;

            }

        }

    }

    return count;

}

  You have to find the average value of ’count’ in the given code if we run findSwaps() infinitely many times using constant ’n’ and each time some random integers (from 1 to n) are given in array a[]. You can assume that the input integers in array a[] are distinct.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases. Each test case contains an integer n (1 ≤ n ≤ 105) in a single line.

Output

For each case, print the case number and the desired result. If the result is an integer, print it. Otherwise print it in ‘p/q’ form, where p and q are relative prime.

Sample Input

2

1

2

Sample Output

Case 1: 0

Case 2: 1/2


问题链接UVA12004 Bubble Sort

问题简述:(略)

问题分析

  对于任意一对数,它们之间交换概率和不交换概率是相等的,这对数的期望值就为1/2。总共有C(n,2)对数,故期望值为n*(n-1)/4。

程序说明:(略)


题记:(略)


参考链接:(略)


AC的C++语言程序如下:

/* UVA12004 Bubble Sort */

#include <iostream>

using namespace std;

int main()
{
    int t;
    long long n, up;

    cin >> t;
    for(int i=1; i<=t; i++) {
        cin >> n;

        up = n * (n - 1);
        if(up % 4 == 0)
            cout << "Case " << i << ": " << up / 4 << endl;
        else
            cout << "Case " << i << ": " << up / 2 << "/2" << endl;
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值