28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?
- /*思路:1.老板是好人(即当3个人时,只需买2瓶,第三瓶用前面的2个瓶盖和第三瓶的瓶盖换)
- * 人数m 买的可乐数n 剩余瓶盖
- * 1 1 1
- * 2 2 2
- * 3 2 0 m==1||m==2 n=m
- * 4 3 1 规律 m>2 n=m-(m/3)
- * 5 4 2 观察发现 n=m-(m/3)同样适用m==1||m==2
- * 6 4 0 即 n=m-(m/3)
- * 7 5 1
- * ... ... ...
- *
- * 2.老板是个生意人(即不满足不给钱就用期货瓶盖换)
- * 人数m 买的可乐数n 剩余瓶盖
- * 1 1 1
- * 2 2 2
- * 3 3 3 m%3!=0 n=m-(m/3)
- * 4 3 1 规律 m%3!==0 n=m-(m/3)+1
- * 5 4 2
- * 6 5 3
- * 7 5 1
- * 8 6 2
- * 9 7 3
- * 10 7 1
- *
- * */
- public class Test10
- {
- public static void main(String[] args)
- {
- int cole1 = goodBoss(28);
- System.out.println("28个人需要"+cole1+"瓶可乐");
-
- int cole2 = goodBoss(50);
- System.out.println("28个人需要"+cole2+"瓶可乐");
-
- int cole3 = businessBoss(28);
- System.out.println("28个人需要"+cole3+"瓶可乐");
-
- int cole4 = businessBoss(50);
- System.out.println("28个人需要"+cole4+"瓶可乐");
- }
-
- public static int goodBoss(int person)
- {
- return (person-(person/3));
- }
-
- public static int businessBoss(int person)
- {
- if(person%3!=0)
- {
- return (person-(person/3));
- }
- else
- return (person-(person/3)+1);
- }
- }
复制代码 |
|