解题思路:
只要看(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 = 0; while(m < n){ if( (m - v) * k <= m ){ cnt = -1; break; } m = (m - v) * k ; ++cnt; } printf("%d ",cnt); } return 0; }
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 = 0; while(m < n){ if( (m - v) * k <= m ){ cnt = -1; break; } m = (m - v) * k ; ++cnt; } cout << cnt << endl; } return 0; }
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 "); continue; } if((ans - v) * k <= ans || ans - v <= 0 || k <= 0 ||(ans - v) * k < 0){ printf("-1 "); continue; } int cnt = 0; while(ans < n){ ++cnt; if(ans - v <= 0 || (ans - v) * k < 0){ cnt = -1; break; } ans = (ans - v) * k ; } printf("%d ",cnt); } return 0; }