NYIST 708 ones java

来源:互联网 发布:成都工业学院软件 编辑:程序博客网 时间:2024/04/29 20:25
描述
Given a positive integer N (0<=N<=10000), you are to find an expression equals to N using only 1,+,*,(,). 1 should not appear continuously, i.e. 11+1 is not allowed.
输入
There are multiple test cases. Each case contains only one line containing a integer N
输出
For each case, output the minimal number of 1s you need to get N.
样例输入
210
样例输出
2

7

使用单组数据进行测试

import java.util.Scanner;/** *  * @author admin *描述 *Given a positive integer N (0<=N<=10000), you are to find an expression equals to N  *using only 1,+,*,(,). 1 should not appear continuously, i.e. 11+1 is not allowed. *输入 *There are multiple test cases. Each case contains only one line containing a integer N *输出 *For each case, output the minimal number of 1s you need to get N. *样例输入 *2 *10 *样例输出 *2 *7 */public class Ones {public static void main(String[] args) {Scanner input=new Scanner(System.in);int n=input.nextInt();input.close();System.out.println(computer(n, 2));}public static int computer(int n,int m){for (int i = m; i < n/2; i++) {if(n%i==0){return i+computer(n/i, i);}}return n;}}


0 0