[Daily Practice·Programming Questions] Jinjin's savings plan: where is the remaining 30% case?

topic description

Jinjin’s pocket money has always been managed by himself. At the beginning of each month, my mother gives Jinjin 300 yuan, and Jinjin will budget the expenses of this month, and the actual expenses will always be the same as the budget. In order for Jinjin to learn how to save, her mother proposed that Jinjin can deposit the entire hundred dollars with her at any time, and she will add 20% to Jinjin at the end of the year. Therefore, Jinjin made a savings plan: at the beginning of each month, after receiving the pocket money from her mother, if she expects to have more than 100 yuan or exactly 100 yuan by the end of the month, she will put the whole hundred Most of the money is deposited with my mother, and the rest of the money is kept in my own hands. For example, at the beginning of November, Jinjin still had 83 yuan in his hands, and his mother gave Jinjin 300 yuan. Jinjin expects to spend 180 yuan in November, so she will save 200 yuan with her mother and keep 183 yuan for herself. By the end of November, Jinjin will have 3 yuan left in his hands. Jinjin found that the main risk of this savings plan is that the money deposited with her mother cannot be withdrawn before the end of the year. It is possible that at the beginning of a certain month, the money in Jinjin’s hands plus the money given by her mother this month is not enough for the original budget for this month. If this happens, Jinjin will have to save money and squeeze the budget this month. Now please judge whether this will happen based on the monthly budget of Jinjin from January to December 2004. If not, calculate how much money Jinjin will have in his hands by the end of 2004 after his mother returns the money Jinjin usually saves plus 20% to Jinjin.

my code

class Solution:
    def __init__(self) -> None:
        pass
    
    def solution(self, budgets):
        result = None

        # TODO: 请在此编写代码
        mom = 0
        my = 0
        f = 0
        for i in range(12):
            new = my + 300 - budgets[i]
            if new < 0 :
                f = -1-i
                return f
            yu = new//100*100
            mom += yu
            my = new - yu
            # print(i+1,mom,my,yu)
        result =  mom * 1.2 +my 
        # 考虑整数和小数
        left = str(result).split('.')[1]
        if int(left) == 0:
            result = int(result)
        return result

if __name__ == "__main__":
    budgets = []
    for i in range(12):
        try:
            budgets.append(float(input().strip().split()[0]))
        except: # 这个操作:case通过率:70.0%
            budgets.append(0.0)
    
    sol = Solution()
    result = sol.solution(budgets)

    print(result)

Input and output description

enter:

12 lines of data, each line contains a non-negative integer less than 350, respectively representing the budget of Jinjin from January to December.

output:

an integer. If there is insufficient money in a certain month during the implementation of the savings plan, output -X, and X indicates the first month when this situation occurs; otherwise, output how much money will be in the hands of Jinjin at the end of 2004.

Where is the remaining 30% case?

I have encountered this problem twice. The pass rate was 50% last time. This time, the processing of empty input was added, and the pass rate increased to 70%.
Then, where is the remaining 30%!

Guess you like

Origin blog.csdn.net/qq_46319397/article/details/129460860