POJ 3181 Dollar Dayz && Uva 147 Dollars (full backpack)

Source: Internet
Author: User
Tags integer division dayz

  The first is Uva 147:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=83

After the careful reading of the problem found or complete backpack, just need to deal with floating point. That is, the face value of all coins are multiplied by 100, into an integer, the input data also do the same processing, and then is a set of complete backpack template, in the output of the format and precision to card a card you ... At first I didn't think with printf can, so Baidu cout output format control, indeed cout format and precision control compared to printf is very inconvenient ah ...

1#include <cstdio>2#include <iomanip>3#include <iostream>4 using namespacestd;5typedefLong LongLL;6 7 ConstLL coin[ the]= {0,5,Ten, -, -, -, $, -, +, -, the,10000};8LL f[30010]= {1};9 TenInlinevoidinit () { One      for(LL i=1; i<= One; ++i) A          for(LL j=coin[i]; j<=30000; ++j) -f[j]+= f[j-Coin[i]]; - } the  - intMain () { - init (); -     DoubleMoney ; +COUT.SETF (iOS::fixed); -Cout.precision (2); +      while(Cin>>money && money!=0.00){ ACOUT&LT;&LT;SETW (6) &LT;&LT;MONEY&LT;&LT;SETW ( -) <<f[int(money* -+0.5)]<<Endl; Remember when money is converted to integers here +0.5! Prevent accuracy Errors!  at     } -     return 0; -}

And with scanf and printf, you just need to be so concise:

1  while (scanf ("%lf", &money), money!=0.00) {2     printf ("  %6.2f%17lld\n", money,f[int(money*+0.5)]); 3 }

And then the POJ 3181:http://poj.org/problem?id=3181.

Although still use coins as background, but in fact, is an integer division, the beginning of my array opened small, the result of Re, and later opened a large array after forgetting to test those strong data, to read someone else's blog before I know it will super long long, I would like to use high precision? Can others say that the direct high-precision words will tle, because the test of the largest data is only 33 bits, so with two long long can, the same type using high-precision principle to do the addition simulation, pay attention to some of the output details can:

1#include <cstdio>2#include <cstring>3typedefLong LongLL;4 ConstLL largest=1e17;5LL f1[1008], f2[1008];6 7 intMain () {8     intn,k;9      while(~SCANF ("%d%d",&n,&k)) {Tenmemset (F2,0,sizeof(F2)); Onememset (F1,0,sizeof(F1)); Af2[0]=1; -          for(intI=1; i<=k; ++i) -              for(intJ=i; j<=n; ++j) { thef1[j]+= f1[j-i]; -f2[j]+= f2[j-i]; -                 if(f2[j]>=largest) { -f1[j]+= f2[j]/largest; +f2[j]%=largest; -                 } +             } A         if(!f1[n]) printf ("%i64d\n", F2[n]); at         Elseprintf"%i64d%017i64d\n", F1[n],f2[n]); -     } -     return 0; -}

Attach the kuangbin of the Great God: http://www.cnblogs.com/kuangbin/archive/2012/09/20/2695165.html

POJ 3181 Dollar Dayz && Uva 147 Dollars (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.