Exam09_StrangeDonate(java)

description

Q Mr. MONOPOLY dying wishes is: to come up with one million yuan to the residents of the community of X draw a little comfort to the hearts of guilt.
The trouble is, he has a very strange request:

  1. 1,000,000 yuan must be exactly divided into several parts (not the residual). Each party must be several dollars 7. For example: 1, 7-, 49 yuan, 343 yuan, ...
  2. Copies of the same amount no more than 5 copies.
  3. In the case of satisfying the above requirements, the better divided parts!
    Please help calculate, can be divided into a maximum of how many?

Entry

Fixed input: 1000000

Export

How many copies can be divided into a maximum of

Sample input

1000000

Sample Output

16

My code

import java.util.Scanner;

public class Exam09_StrangeDonate {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String s = Integer.toString(n, 7);
		int count = 0;
		for (int i = 0; i < s.length(); i++) {
			//将每一位进行累加,巧妙得出答案
			/*将1000000转为7进制11333311
			第一位上的1代表1个7^7,
			第二位上的1代表1个7^6,
			第三位上的3代表3个7^5,
			……
			最后一个1代表一个7^0,
			所以相加得最终份数,但是若有一位大于5,就不行了*/
			count += s.charAt(i) - '0';
		}
		System.out.println(count);

	}

}

Published 60 original articles · won praise 4 · Views 1290

Guess you like

Origin blog.csdn.net/qq_43966129/article/details/104942498