煤氣竈

鏈接:https://ac.nowcoder.com/acm/contest/332/B
來源:牛客網

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
小j開始打工,準備賺錢買煤氣竈。
第一天,小j的工資爲n元,之後每天他的工資都比前一天多d元。
已知煤氣竈需要m元,求小j最少工作幾天才能買到煤氣竈。
輸入描述:
四個整數 n,m,d,x
分別表示小j第一天的工資,煤氣竈的價格,工資每天的增長量,答案不超過x
輸出描述:
一個數表示答案
示例1
輸入
複製
10 100 20 100
輸出
複製
4
說明
10+30+50+70>=100
備註:
[Math Processing Error]0≤n,d≤109,n+d>0
[Math Processing Error]1≤m≤1018
[Math Processing Error]

思路:這題也是超級水的,但就是因爲對數據範圍的不掌握導致沒有一次AC,特別遺憾

錯誤代碼:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        BigInteger n=sc.nextBigInteger();
        BigInteger m=sc.nextBigInteger();
        BigInteger d=sc.nextBigInteger();
        BigInteger x=sc.nextBigInteger();
        BigInteger sum=new BigInteger("0");
        BigInteger ans=new BigInteger("0");
        BigInteger tmp=new BigInteger("1");
        while(sum.compareTo(m)<0){
            sum=sum.add(n);
            ans=ans.add(tmp);
            n=n.add(d);
        }
        System.out.println(ans);
        sc.close();
    }
}

AC代碼:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        long n=sc.nextLong();
        long m=sc.nextLong();
        long d=sc.nextLong();
        long x=sc.nextLong();
        long sum=n;
        long ans=1;
        while(sum<m) {
            n+=d;
            sum+=n;
            ans++;
        }
        System.out.println(ans>x?x:ans);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章