写了一段代码,但我没法验证它是否正确
程序代码:
#include <stdio.h> typedef struct { unsigned a; unsigned b; } pair; static pair foo_( pair p[], unsigned n, unsigned m, pair cur ) { if( m == 0 ) return cur; double cpr = 0; unsigned index = 0; for( unsigned i=0; i!=n; ++i ) { if( p[i].a != 0 ) { if( cpr < (p[i].a + cur.a)*1.0/(p[i].b + cur.b) ) { cpr = (p[i].a + cur.a)*1.0/(p[i].b + cur.b); index = i; } } } pair t = { p[index].a+cur.a, p[index].b+cur.b }; p[index].a = 0; return foo_(p,n,m-1,t); } double foo( pair p[], unsigned n, unsigned m ) { pair r = foo_( p, n, m, (pair){0,0} ); return r.a*1.0/r.b; } int main( void ) { unsigned t; scanf( "%u", &t ); for( unsigned i=0; i!=t; ++i ) { pair p[10000]; unsigned n, m; scanf( "%u%u", &n, &m ); for( unsigned j=0; j!=n; ++j ) scanf( "%u%u", &p[j].a, &p[j].b ); printf( "Case #%u: %.2f\n", i+1, foo(p,n,m) ); } }