Ultraviolet A Problem Solution: 10137-the trip

Source: Internet
Author: User

I convert all the calculations from floating-point to integral to avoid all the messes in floating-point calculation. the method is straight forward: I simulate the exchanging SS and use a 'budget 'to record the extra cents that shocould be exchanged to keep the costs in balance.

Code:

  1. /*************************************** **********************************
  2. * Copyright (c) 2008 by liukaipeng *
  3. * Liukaipeng at gmail dot com *
  4. **************************************** *********************************/
  5. /* @ Judge_id 00000 10137 C "the trip "*/
  6. # Include <stdio. h>
  7. # Include <stdlib. h>
  8. # Include <string. h>
  9. # Include <strings. h>
  10. # Deprecision MAX 1000
  11. Int calc_exchange (INT costs [], int persons)
  12. {
  13. Int Total = 0, aver, exchange, budget;
  14. Int I;
  15. For (I = 0; I <persons; ++ I)
  16. Total + = costs [I];
  17. Aver = total/persons;
  18. Exchange = 0;
  19. Budget = 0;
  20. If (Total % persons! = 0)
  21. Aver + = 1;
  22. For (I = 0; I <persons; ++ I ){
  23. If (costs [I]> = aver ){
  24. Exchange ++ = costs [I]-aver;
  25. Budget + = costs [I]-aver;
  26. } Else if (costs [I] <aver-1 ){
  27. Budget-= aver-1-costs [I];
  28. }
  29. }
  30. If (Budget <0)
  31. Exchange-= budget;
  32. Return exchange;
  33. }
  34. Int main (INT argc, char * argv [])
  35. {
  36. # Ifndef online_judge
  37. Char in [256];
  38. Char out [256];
  39. Strcpy (in, argv [0]);
  40. Strcat (in, ". In ");
  41. Freopen (in, "r", stdin );
  42. Strcpy (Out, argv [0]);
  43. Strcat (Out, ". Out ");
  44. Freopen (Out, "W", stdout );
  45. # Endif
  46. Int costs [Max];
  47. Char Buf [20];
  48. Int Len;
  49. Char C;
  50. Int persons, I, j;
  51. Int exchange;
  52. For (scanf ("% d/N", & persons); persons! = 0; scanf ("% d/N", & persons )){
  53. For (I = 0; I <persons; ++ I ){
  54. For (j = 0; (C = getchar ())! = '/N'; ++ J ){
  55. If (C! = '.')
  56. Buf [J] = C;
  57. Else
  58. -- J;
  59. }
  60. Buf [J] = '/0 ';
  61. Costs [I] = atoi (BUF );
  62. }
  63. Exchange = calc_exchange (costs, persons );
  64. Sprintf (BUF, "% d", exchange );
  65. Len = strlen (BUF );
  66. If (LEN = 1)
  67. Printf ("$ 0.0% S/N", Buf );
  68. Else if (LEN = 2)
  69. Printf ("$0.% s/n", Buf );
  70. Else {
  71. Putchar ('$ ');
  72. For (I = 0; I <len-2; ++ I)
  73. Putchar (BUF [I]);
  74. Printf (". % s/n", BUF + len-2 );
  75. }
  76. }
  77. Return 0;
  78. }

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.