链接: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);
}
}