Hdu1248 cold ice throne [math problems] or [full backpack]

Source: Internet
Author: User

Cold ice throne time limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) Total submission (s): 10550 accepted submission (s): 5355

Problem description the Lich King of the undead family sends a salary pull, and the Death Knight receives a N yuan bill (Remember, there is only one bill). To prevent him from dying frequently during the battle, he decided to buy some items for himself, so he came to the top store.

Death Knight: "I want to buy items! "

Taobao Merchants: "We have three items here: one for 150 blood bottles, one for 200 magic medicines, and one for 350 for invincible potions ."

Death Knight: "OK, give me a blood bottle ."

Then he took out the n-dollar bill and handed it to the goblin merchant.

Taobao Merchants: "I forgot to remind you that we don't have the habit of looking for money here. We accept a tip for a lot of money ."

Death Knight :"......"

The Death Knight thought, instead of sending money as a tip to him, it would be better to buy more items on his own. He would like to buy them all in the future and put them at home early, but try to make him as little tip as possible.

Now, the Death Knight hopes that you can help him calculate the minimum tip he wants to tip the goblin merchant.
The first line of input data is an integer T (1 <= T <= 100), representing the number of test data. then there is T-row test data. Each test data contains only one positive integer N (1 <= n <= 10000), and N represents the nominal value of the money in the hands of the Death Knight.

Note: There are only three items described in the topic.
 
Output for each group of test data, please output the minimum amount of money the Death Knight will waste to the goblin merchant as a tip.
 
Sample Input
2900250
 
Sample output
050

Full backpack:

#include <stdio.h>#include <string.h>#define maxn 10002int dp[maxn];const int sample[] = {150, 200, 350};int main(){    int t, n, i, j;    scanf("%d", &t);    while(t--){        scanf("%d", &n);        memset(dp, 0, sizeof(dp));        for(i = 0; i < 3; ++i){            for(j = sample[i]; j <= n; ++j)                if(dp[j] < dp[j - sample[i]] + sample[i])                    dp[j] = dp[j - sample[i]] + sample[i];        }        printf("%d\n", n - dp[n]);    }    return 0;}

Another idea on the Internet: Because 150 + 200 = 350, 350 of items can be ignored. First, use 150 of items and consider the remainder. If the remainder is greater than 50, replace an item with 200. In this way, the remainder is reduced by 50. Code implementation:
#include <stdio.h>int main(){    int t, n, ans, m;    scanf("%d", &t);    while(t--){        scanf("%d", &n);        ans = n % 150;        m = n / 150;        while(ans >= 50 && m--)            ans -= 50;        printf("%d\n", ans);    }    return 0;}


Hdu1248 cold ice throne [math problems] or [full backpack]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.