解题思路:
只要看(A-V)*K 这个公式的更新值是否大于等于A ,大于的话继续循环,否则报错
注意一点,数据会爆int
WA代码:
#include<stdio.h>
int main(){
long long n ,m, v, k;
int t;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld%lld",&n,&m,&v,&k);
int cnt = ;
while(m < n){
if( (m - v) * k <= m ){
cnt = -;
break;
}
m = (m - v) * k ;
++cnt;
}
printf("%d\n",cnt);
}
return ;
}
AC代码:
#include <iostream>
using namespace std;
int main(){
long long n ,m, v, k;
int t;
cin >> t;
while(t--){
cin >> n >> m >> v >> k;
int cnt = ;
while(m < n){
if( (m - v) * k <= m ){
cnt = -;
break;
}
m = (m - v) * k ;
++cnt;
}
cout << cnt << endl;
}
return ;
}
AC代码(比较复杂):
#include<stdio.h>
#include<math.h>
int main(){
long long n ,m, v, k;
int t;
scanf("%d",&t);
while(t--){
scanf("%I64d%I64d%I64d%I64d",&n,&m,&v,&k);
long long ans = m;
if(ans >= n){
printf("0\n");
continue;
}
if((ans - v) * k <= ans || ans - v <= || k <= ||(ans - v) * k < ){
printf("-1\n");
continue;
}
int cnt = ;
while(ans < n){
++cnt;
if(ans - v <= || (ans - v) * k < ){
cnt = -;
break;
}
ans = (ans - v) * k ;
}
printf("%d\n",cnt);
}
return ;
}