第一天 1金币
第二天 2金币
第三天 2金币
第四天 3金币
第五天 3金币
第六天 3金币
第一种方法,模拟每一天
public static void main(String[] args) {
int n = 0;
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
int total = 0;//总金币
int nowCoin=1;//当前给几个金币
int nowDay = 0;//过了几天
// 执行n此,模拟每一天的情况
for (int i=0;i<n;i++) {
nowDay++;//过了一天
total+=nowCoin;//累加金币
if(nowDay==nowCoin) {
// 时间点和金币数相同
nowCoin++;
nowDay=0;//过了几天又从0开始
}
}
System.out.println(total);
}
第二种方法
int n = 0;
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
int coin = 0;
while (n>0) {
coin++;
n-=coin;
}
int total=0;
for (int i=1;i<=coin;i++) {
total+=(i*i);
}
System.out.println(total+n*coin);