点击打开链接
盐水的故事
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22530 Accepted Submission(s): 5703
Problem Description
挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下;然后滴二滴,停一下;再滴三滴,停一下...,现在有一个问题:这瓶盐水一共有VUL毫升,每一滴是D毫升,每一滴的速度是一秒(假设最后一滴不到D毫升,则花费的时间也算一秒),停一下的时间也是一秒这瓶水什么时候能挂完呢?
Input
输入数据包含多个测试实例,每个实例占一行,由VUL和D组成,其中 0<D<VUL<5000。
Output
对于每组测试数据,请输出挂完盐水需要的时间,每个实例的输出占一行。
Sample Input
10 1
Sample Output
13
Author
lcy
Source
Recommend
错误代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
//double v,d;
//while(scanf("%lf%lf",&v,&d)!=EOF){
int v,d;
while(scanf("%d%d",&v,&d)!=EOF){
int time=0,cnt=1,flag=0;
while(v>0){
for(int i=0;i<cnt;i++)
{
v-=d;
time++;
if(v<=0) {
flag=1;
break;
}
}
time++;
cnt++;
if(flag) break;
}
printf("%d\n",time-1);
}
return 0;
}
模拟滴水过程:
200MS
#include<bits/stdc++.h>
using namespace std;
int main()
{
double v,d;
while(scanf("%lf%lf",&v,&d)!=EOF){
int time=0,cnt=1,flag=0;
while(v>0){
for(int i=0;i<cnt;i++)
{
v-=d;
time++;
if(v<=0) {
flag=1;
break;
}
}
time++;
cnt++;
if(flag) break;
}
printf("%d\n",time-1);
}
return 0;
}
首先判断vul是否能被d整除,如果能整除,总滴数sum为vul/d,
否则,总滴数sum为(vul/d)+1,
然后计算使得1+2+3....n<=sum成立的最大项数
0MS
#include<bits/stdc++.h>
using namespace std;
#define eps 1e-6
int main()
{
double v,d;
while(scanf("%lf%lf",&v,&d)!=EOF){
int sum=v/d;
double cnt=v/d;
if(fabs(cnt-sum)>eps) sum++;//cnt!=sum,不能整除
int s=0,time=0;
while(s<sum){
time++;
s+=time;
}
printf("%d\n",sum+time-1);
}
return 0;
}