首页 > 专题系列 > Java解POJ > POJ 2521 How much did the businessman lose [解题报告] Java
2013
11-11

POJ 2521 How much did the businessman lose [解题报告] Java

How much did the businessman lose

问题描述 :

Businessmen, of course, can make much money. However, sometimes, they would lose money in trading. For example, Jame, a businessman, brought in some goods each cost him 40 yuan and he decided to sell at the price of 70 yuan. Then a customer came to buy one, gave Jame 100 yuan, and of course got back 30 yuan. You may said, “Jame earned 30 yuan.” But unfortunately, Jame found the 100 yuan from the customer was fake. What a poor man! In this case Jame lost 70 yuan (40 (the goods price) + 30 (the money paid back to the customer)).

Now your task is to calculate how much Jame would lose in this kind of trade. Of course in this case, sometimes Jame may still earn.

输入:

The input consists of several instances. Each instance contains four integers N, M, P and C, which are all in the range [0, 50000], separated by a single space.

N is the cost price of Jame good.

M is the sell price.

P is the total fake money the customer gave Jame.

C is how much Jame paid back to the customer.

Here N < M. And P may be less than M, as customer may pay with some fake money and some true money.

The input is terminated by a line containing 4 zeros.

输出:

For one instance, output one line contains exactly one integer, which tells how much Jame lost in the trade. If Jame still earned, just output a negative integer, the absolute value of which tells the money Jame earned.

样例输入:

40 70 100 30
40 70 50 20
14604 32391 3902 153
0 0 0 0

样例输出:

70
20
-13885

解题代码:

import java.util.*;  
      
    public class Main {  
      
        public static void main(String[] args) {  
            Scanner cin = new Scanner(System.in);  
            while(cin.hasNext())  
            {  
                String tmp = cin.nextLine();  
               if(tmp.equals("0 0 0 0"))  
                   break;  
               String[] tmp1 = tmp.split(" ");  
               int N = Integer.valueOf(tmp1[0]);  
               int M = Integer.valueOf(tmp1[1]);  
               int P = Integer.valueOf(tmp1[2]);  
               int C = Integer.valueOf(tmp1[3]);  
                 
               int totalPaid = M + C;  
              int totalTrue = totalPaid - P;  
                 
               int earn = totalTrue - N - C;  
                
              System.out.println(earn*-1);  
                 
           }  
    
       }  
     
   }

  1. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥