您的位置:首页 > 其它

UVa 357 - Let Me Count The Ways

2013-04-09 10:55 471 查看
还是硬币的问题,开始毫不犹豫的按之前的思路。写完,提交,WA。很尴尬。也用了long long。不知道什么原因30000这个就是表示不出来。找到了WA的原因,真是一头雾水啊。后来看了discuss发现有用一维数组的。于是想起了看过滚动数组可以优化。于是抱着尝试的态度。用了滚动数组优化。结果TLE,TLE的原因就是没有加 != EOF 时间居然是是3.000 和 0.016 居然差这么多。真的很诧异。。。

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int Case;
long long dp[30010], ans;
int V[5] = { 1, 5, 10, 25, 50 };

void solve ( ) {
memset ( dp, 0, sizeof ( dp ) );
dp[0] = 1;
for ( int i = 0; i < 5; ++i )
for ( int j = 1; j < 30010; ++j )
if ( j >= V[i] ) dp[j] += dp[j - V[i]];

}

int main ( ) {
solve ( );
while ( scanf ( "%d", &Case ) != EOF ) {
if ( dp[Case] > 1 ) printf ( "There are %lld ways to produce %d cents change.\n", dp[Case], Case );
else printf ( "There is only 1 way to produce %d cents change.\n", Case );;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: