Try to not use floating point to solve this problem.
XYZT <[email protected]> 于2018年12月19日周三 下午7:40写道:
> Hi,
>
> I am trying to write a solution for "Rounding Error" in Round 1B of
> Codejam 2018 using Python 3 and I am getting a strange "Runtime Error"
> message which I can't seem to replicate locally.
>
> I've tried several different inputs and failed to generate a runtime error.
>
> Here is my code:
>
> # Codejam 2018, Round 1B: Rounding Error
>
> from math import floor
>
> def round_half_up(x):
> """Rounding to nearest integer with tie-breaker rounding up."""
> return floor(x + 0.5)
>
> def round_up_set(N):
> """Returns set of vote counts that round UP."""
> return set([i for i in range(1, N + 1)
> if round_half_up(100*i/N) != floor(100*i/N)])
>
> def get_percentage(num_voters, lang_votes):
> """Gets total percentage given list of vote counts and total voters."""
> return sum([round_half_up(100*i/num_voters) for i in lang_votes])
>
> def get_max_percentage(num_voters, lang_votes):
> """Find max sum of votes when rounding percentages of votes."""
> voters_left = num_voters - sum(lang_votes)
> round_up_votes = round_up_set(num_voters)
>
> # If there exist any values that will round up
> if len(round_up_votes) > 0:
> min_votes = min(round_up_votes)
>
> for i, votes in enumerate(lang_votes):
> # If vote count is currently not rounding up, add more votes
> if votes not in round_up_votes:
> extra_votes = min([i for i in round_up_votes
> if i > votes]) - votes
>
> if extra_votes < min_votes and extra_votes <= voters_left:
> lang_votes[i] += extra_votes
> voters_left -= extra_votes
>
> # Keep adding new languages with minimum votes for rounding up
> while voters_left >= min_votes:
> lang_votes.append(min_votes)
> voters_left -= min_votes
>
> # If there are voters left, get them all to vote on a new language
> if voters_left > 0:
> lang_votes.append(voters_left)
>
> return get_percentage(num_voters, lang_votes)
>
> # I/O Code
> num_cases = int(input())
>
> for case in range(1, num_cases + 1):
> num_voters, num_lang = [int(i) for i in input().split()]
> lang_votes = [int(i) for i in input().split()]
>
> max_percentage = get_max_percentage(num_voters, lang_votes)
> print("Case #{}: {}".format(case, max_percentage))
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Code Jam" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/google-code/ac2a2cba-51ab-48d0-94fc-0f10e57c4d6b%40googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/CAGDEU-JPLdGJUHsCFQ22VDp-Lh02g2GKZjnXS8mJuPHrZ6-gdA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.