JAVA基础金币游戏算总数

第一天 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);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值