zoj 1337 - 行者无疆 始于足下 - 行走,思考,在路上
zoj 1337
xiaohanyu
posted @ Tue, 25 May 2010 01:47:42 +0800
in Algorithm
with tags
ZOJ Algorithm
, 3469 readers
简单题,不知道为什么开始WA了一下。
题目大意是给定一组n个数,那么共有n(n-1)/2个数对,求出所有的互质数对,就是这样。gcd的写法还是值得背下来的。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <stdio.h> #include <math.h> int gcd( int a, int b) { return b ? gcd(b, a%b) : a; } int main( int argc, char *argv[]) { int num; int inta[60]; int i, j; int result; while ( scanf ( "%d" , &num)) { if (num == 0) { break ; } for (i = 0; i < num; ++i) { scanf ( "%d" , &inta[i]); } result = 0; for (i = 0; i < num; ++i) { for (j = i + 1; j < num; ++j) { if (gcd(inta[i], inta[j]) == 1) { result++; } } } if (result == 0) { printf ( "No estimate for this data set.\n" ); } else { printf ( "%.6lf\n" , sqrt (3.0*num*(num-1)/result)); } } return 0; } |
时间紧迫,继续水题……太不道德了……