求搜狗第一题大佬的解法

有没有大佬ac的,分享一下思路和代码,学习一下。#笔试题目#
全部评论
我这个小菜鸡,有幸暴力A了🤣 public static int numberofprize(int a, int b, int c) {         // write code here         int ans = Math.min(a, Math.min(b, c));         a = a - ans;         b = b - ans;         c = c - ans;         int[] nums = {a, b, c};         Arrays.sort(nums);         while (nums[2] > 2 || (nums[1] > 1 && nums[2] > 1)) {             if(nums[2]>2){                 nums[2] -= 2;                 if (nums[1] == 0) {                     nums[1]++;                     continue;                 } else {                     nums[0]++;                 }             }else{                 nums[1]--;                 nums[2]--;                 nums[0]++;             }             if(nums[1]>nums[2]){                 int tmp = nums[1];                 nums[1] = nums[2];                 nums[2] = tmp;             }             nums[0]--;             nums[1]--;             nums[2]--;             ans++;         }         return ans;     }
点赞 回复
分享
发布于 2020-09-05 21:20
abc互相匀是一个思路。 还可以从0开始暴搜,或者二分搜,去找到一个极限数字,使得abc比这个数字多的数>少的数的两倍
点赞 回复
分享
发布于 2020-09-05 21:24
滴滴
校招火热招聘中
官网直投
void swap(int &x, int &y) { int tmp = x; x = y; y = tmp; } int numberofprize(int a, int b, int c) { // write code here int res = 0; int minval = min(a, min(b, c)); if (a == minval) { if(b > c)swap(b, c); } else if (b == minval) { if (a > c) { swap(a, c); } swap(a, b); } else { if (a > b) { swap(a, b); } swap(b, c); swap(a, b); } res += a; b = b - a; c = c - a; while (c - 1 > b && b > 0 && c >= 3) { c = c - 3; b = b - 1; res += 1; } while (b >= 2) { b = b - 2; c = c - 2; res += 1; } while (c >= 5) { c = c - 5; res += 1; } return res; }
点赞 回复
分享
发布于 2020-09-05 21:42

相关推荐

头像
03-18 09:09
Java
点赞 评论 收藏
转发
1 2 评论
分享
牛客网
牛客企业服务