编程题目1

水果店的水果要么8个一袋,要么6个一袋。小明想去水果店买水果,但是如果水果的数目n(0<n<100)刚好可以打包成一袋袋。那么求出最少的袋数。

如果不可以,那么小明就不卖水果。


public class Test2 {
    public static void main(String[] args){
        System.out.println(show(16));

    }
    public static int show(int n)
    {
            int c;
            for (int z = n / 8; n >= 0; z--) {     
                c = (n - z * 8) % 6;
                if (c == 0)
                    return z + (n - z * 8) / 6;
                else
                    continue;
            }
            return -1;
    }

}


猜你喜欢

转载自blog.csdn.net/kunfd/article/details/78128101