题目来源:赛码网
网址:http://exercise.acmcoder.com/online/online_judge_ques?ques_id=1664&konwledgeId=134
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int temp;
while(in.hasNext()) {
temp=in.nextInt();
int n=num(temp);
System.out.println(n);
}
}
public static int num(int n) {
if (n==1) return 1;
int i=0,j=1,max=1,sum=1;//i:用于判断每段递增; j:计数器; max:用于记录每一段递增后的值; sum:返回值;
do{
j++;
i++;
if(i-max==1) {
max=i;
i=0;
sum-=2;
}
sum++;
}while(j<n);
return sum;
}
}