题意:要买n平方的土地,可以买的土地必须为某数的平方数,求至少要几块土地
链接:http://acm.timus.ru/problem.aspx?space=1&num=1073
思路:dp完全背包问题,状态转移方程dp[j] = min ( dp[j], dp[j-sq[i]] + 1 )
注意点:无
以下为AC代码:
ID | Date | Author | Problem | Language | Judgement result | Test # | Execution time | Memory used |
---|---|---|---|---|---|---|---|---|
6252988 |
12:45:29
18 May 2015 | luminous11 | 1073. Square Country | G++ 4.9 C++11 | Accepted | 0.031 | 634 KB |
/*
***********************************************
*# @Author : Luminous11 (573728051@qq.com)
*# @Date : 2015-05-18 13:02:01
*# @Link : http://blog.csdn.net/luminous11
***********************************************
*/
#include <bits/stdc++.h>
#define clr(a, v) memset( a , v , sizeof(a) )
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1.0);
int dp[60005];
int main()
{
ios::sync_with_stdio ( false );
int sq[300];
for ( int i = 0; i < 300; i ++ ){
sq[i] = i * i;
}
int n;
while ( scanf ( "%d", &n ) != EOF ){
memset ( dp, 0x3f3f3f3f, sizeof ( dp ) );
dp[0] = 0;
for ( int i = 1; i < 300; i ++ ){
for ( int j = sq[i]; j <= n; j ++ ){
dp[j] = min ( dp[j], dp[j-sq[i]] + 1 );
}
}
printf ( "%d\n", dp[n] );
}
return 0;
}