题解 | #尼科彻斯定理#
尼科彻斯定理
http://www.nowcoder.com/practice/dbace3a5b3c4480e86ee3277f3fe1e85
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int a = in.nextInt();
System.out.println(getIndex(frequency(a),a).toString());
}
}
//获取相加后的字符串
private static StringBuilder getIndex(int fer,int a) {
int p = getHerd(1, fer-a);
StringBuilder str = new StringBuilder();
for (int i = 0; i < a; i++) {
str.append(p+=2).append("+");
}
return str.delete(str.length()-1,str.length());
}
/**
* 计算循环次数
*/
private static int frequency(int number){
if(number == 1){
return 1;
}
return frequency(number-1)+number;
}
//获取头节点
private static int getHerd(int origin, int number) {
if (number == 1) {
return origin;
}
return getHerd(origin + 2, number - 1);
}
}