1015 - 挂盐水
挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下;然后滴二滴,停一下;再滴三滴,停一下...,现在有一个问题:这瓶盐水一共有VUL毫升,每一滴是D毫升,每一滴的速度是一秒(假设最后一滴不到D毫升,则花费的时间也算一秒),停一下的时间也是一秒这瓶水什么时候能挂完呢?
题目输入
输入数据包含多个测试实例,每个实例占一行,由VUL和D组成,其中 0<D<VUL<5000。
题目输出
对于每组测试数据,请输出挂完盐水需要的时间,每个实例的输出占一行。
输入/输出样例
题目输入
10 1
题目输出
13
C语言解答
#include<stdio.h> int main() { int VUL,D; while(scanf("%d%d",&VUL,&D)!=EOF) { int i=1,t=0,s=0,count=0; while(1) { s+=i*D; count+=i; if(s==VUL) { t=count+i-1; break; } if((s+(i+1)*D)>VUL) { int d; d=(VUL-s)%D?((VUL-s)/D+1):(VUL-s)/D; t=count+i+d; break; } else i++; } printf("%d\n",t); } return 0; }
C++解答
#include<iostream> #include<cstdio> #include<cstring> #include<map> #include<queue> #include<stack> #include<set> #include<vector> #include<algorithm> #include<cmath> using namespace std; #define Max(a,b) (a > b ? a : b) #define Min(a,b) (a < b ? a : b) #define INF 0x3f3f3f3f #define M 100 int judge(double n ,double d) { double ans = 0; for(int i = 1; ; i++) { ans += i * d; if(ans >= n) { return i; } } } int main() { double n,d; while(scanf("%lf%lf",&n,&d)!=EOF) { int ans = n / d + 0.9; ans += judge(n,d) - 1; printf("%d\n",ans ); } return 0; }