网易笔试第二题求ABpq问题,大佬们帮看一下为什么我ac0

网易笔试第二题求ABpq问题
public class Main {
	    public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        int n = sc.nextInt();
	        long[][] ans = new long[n][4];
	        for(int i=0;i<n;i++){
	            ans[i][0] = sc.nextInt();
	            ans[i][1] = sc.nextInt();
	            ans[i][2] = sc.nextInt();
	            ans[i][3] = sc.nextInt();
	        }
	        long[] res = new long[n];
	        long count = 0;
	        for(int i=0;i<n;i++){
	                while(ans[i][0]<ans[i][1]){
	                    if(ans[i][0]>ans[i][1]-ans[i][2]){
	                        ans[i][0]+=ans[i][2];
	                        count++;
	                    }else{
	                       ans[i][2]=ans[i][2]*ans[i][3];
	                       ans[i][0]+=ans[i][2];
	                       count+=2;
	                    }
//	                    ans[i][0]+=ans[i][2];
//	                    ans[i][2]=ans[i][2]*ans[i][3];
//	                    count++;
	            }
	            res[i]=count;
	            count=0;
	        }
	        for(int i=0;i<n;i++){
	           System.out.println(res[i]);
	        }
	    }
	}

#网易##笔试题目#
全部评论
package Test4; import java.util.ArrayList; import java.util.Scanner; // 之前用int 好像是90%,改了long 就100%了,应该大概是这样的 public class Main6 {     public static void main(String[] args) {         Scanner scanner=new Scanner(System.in);         int t=scanner.nextInt();         for (int i=0;i<t;i++){             long A=scanner.nextLong();             long B=scanner.nextLong();             long p=scanner.nextLong();             long q=scanner.nextLong();                       int sum=0;             while (A<B){                 if(A+p<B){                     while (A+p<B){                         p=p*q;                         sum++;                     }                     A=A+p;                     sum++;                 }else {                     A=A+p;                                     }             }             System.out.println(sum);         }     } }
点赞 回复 分享
发布于 2019-09-21 17:13
😂,感觉想复杂了吧。 先算一下  a 要加 p 几次才能超过 a,然后在这个次数 - 1 内 p = p*q,如果某一次超过了就直接返回。 AC 代码:
点赞 回复 分享
发布于 2019-09-21 17:13
递归求解,过90%
点赞 回复 分享
发布于 2019-09-21 17:08
我直接让他乘到加a大于b就能ac。。。
点赞 回复 分享
发布于 2019-09-21 17:10
直接乘,q>=2指数增加用不了几次。 function solve(a, b, p, q) { let gap = b - a, count = 0; while (p < gap) { p*=q; count++; } return count+1; } let n = parseInt(readline()); while (n--) { let lines = readline().split(" ").map(Number); console.log(solve(...lines)); }
点赞 回复 分享
发布于 2019-09-21 17:28

相关推荐

26牛牛不会梦到感谢信:羡慕离职了还能吃吗现在就赶回去
点赞 评论 收藏
分享
Cassifa:发的字比你都多的一律视为骗子或者想白嫖压榨实习生的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务