Lottery
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1086 Accepted Submission(s): 537
拿到第一張卡片的概率是 n/n ,期望次數是 1,拿到第二張的概率是 (n-1)/n, 期望次數是 n/(n-1) ...
把這些次數加起來 n/n + n/(n-1) + ... + n/1
don 分母 doneminator
num 分子 numerator
ws 空格 white space
ln 分數線 line
http://acm.hdu.edu.cn/forum/read.php?tid=16311&keyword=1099
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <algorithm>
long long GCD (long long a, long long b) {
if (b == 0) {
return a;
}
return GCD (b, a % b);
}
int main () {
long long n;
while (scanf ("%lld", &n) == 1) {
long long don = 1, num = 0;
for (long long i=1; i<=n; ++i) {
num = num * i + n * don;
don = don * i;
long long gcd = GCD (don, num);
don = don / gcd;
num = num / gcd;
}
if (num % don == 0) {
printf ("%lld\n", num / don);
} else {
static char strInt[1024], strNum[1024], strDon[1024];
static char strWS[1024], strLn[1024];
static char ws[] = " ";
static char ln[] = "------------------------------";
sprintf (strInt, "%lld", num / don);
sprintf (strNum, "%lld", num % don);
sprintf (strDon, "%lld", don);
memset (strWS, 0, sizeof (strWS));
strncpy (strWS, ws, strlen (strInt));
memset (strLn, 0, sizeof (strLn));
strncpy (strLn, ln, std::max (strlen (strNum), strlen (strDon)));
printf ("%s %s\n%s %s\n%s %s\n", strWS, strNum, strInt, strLn, strWS, strDon);
}
}
return 0;
}