POJ 1276 Cash Machine 解题报告
编号:1276
考查点:枚举、搜索
思路:这个是zz让我看得代码,看了别人的自然知道如何抄了..自己肯定想不到怎么能够不拉下任何一种cash情况.。
提交情况:看了别人的代码,今天还在这道题上TLE了N次,后来才发现是循环变量写错了.汗.。
Source Code:
// POJ Grids 1276
#include < stdio.h >
#include < string .h >
#include < stdlib.h >
struct Bill{
int n;
int d;
};
Bill b[ 11 ];
bool flag[ 100001 ];
int compare( const void * e1, const void * e2)
{
Bill * a = (Bill * )e1;
Bill * b = (Bill * )e2;
return b -> d - a -> d;
}
int main()
{
int cash,n;
while (scanf( " %d %d " , & cash, & n) != EOF)
{
for ( int i = 0 ;i < n;i ++ )
{
scanf( " %d %d " , & b[i].n, & b[i].d);
}
if ( ! cash ||! n)
{
printf( " 0\n " );
continue ;
}
qsort(b,n, sizeof (Bill),compare);
memset(flag, false , sizeof flag);
flag[ 0 ] = true ;
int max = 0 ;
for ( int i = 0 ;i < n;i ++ )
for ( int j = max;j >= 0 ;j -- )
if (flag[j])
for ( int k = 1 ;k <= b[i].n;k ++ )
{
int temp = j + k * b[i].d;
if (temp > cash)
break ;
max = temp > max ? temp : max;
if ( ! flag[temp])
flag[temp] = true ;
}
printf( " %d\n " ,max);
}
return 0 ;
}
总结:这道英文题不是书上的,自己目前肯定搞不定这么麻烦的问题,努力.。
By Ns517
Time 09.02.05