拼多多笔试:满减优惠

拼多多,满减优惠问题,感觉结果没错啊,为什么通过率为0,求大神指点

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] s1 = sc.nextLine().split(" ");
        int n = Integer.parseInt(s1[0]);
        int m = Integer.parseInt(s1[1]);
        String[] s2 = sc.nextLine().split(" ");
        int resRaw = 0;
        for (String str: s2){
            resRaw += Integer.parseInt(str);
        }
        Map<Integer,Integer> map = new HashMap<>();
        for (int i=0; i<m; i++){
            String[] s3 = sc.nextLine().split(" ");
            map.put(Integer.parseInt(s3[0]), Integer.parseInt(s3[1]));
        }
        int resCur = 0;
        for (String str2: s2){
            int item = Integer.parseInt(str2);
            int min = 0;
            List<Integer> list = new ArrayList<>();
            for (Map.Entry<Integer, Integer> entry : map.entrySet()){
                if ( item >= entry.getKey() ){//满足满减要求,选一个减得最多的
                    int tmp = item - entry.getValue();
                    list.add(tmp);
                }
            }
            if ( list.size() != 0){
                Collections.sort(list);
                int item_cur = list.get(0);
                resCur += item_cur;
            }else{
                resCur += item;
            }
        }
        if (resCur < resRaw ){
            System.out.println(resCur);
        }else{
            System.out.println(resRaw);
        }
}
#拼多多#
全部评论
算法岗都是只考编程题么.感觉不应该投这个岗,太难考了
点赞 回复 分享
发布于 2018-09-21 17:31
我也是0,这个题目做了好久
点赞 回复 分享
发布于 2018-09-21 17:29
#include using namespace std; const int maxn = 1e3 + 5; int main() { int n, m, price[maxn], X[maxn], Y[maxn]; cin >> n >> m; for (int i = 0; i > price[i]; for (int i = 0; i > X[i] >> Y[i]; sort(price, price + n); int ans = 0; int vis[maxn]; memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { int maxY = 0, dex = 0; for (int j = 0; j < m; j++) { if (!vis[j] && price[i] >= X[j]) { if (Y[j] > maxY) { maxY = Y[j]; dex = j; } } } if (maxY) vis[dex] = 1; ans += price[i] - maxY; } cout << ans << endl; return 0; }
点赞 回复 分享
发布于 2018-09-21 17:28
商品价钱从小到大 满减优惠从大到小 优惠一致满减上限从小到大 贪心 用过了标记
点赞 回复 分享
发布于 2018-09-21 17:24
唯一一题完全ac。先把商品按价格排序,先从价格小点的买起,然后买每个商品再按满减最多的买(不能满减就按原价买),买完这张优惠券就不能再用了。
点赞 回复 分享
发布于 2018-09-21 17:19
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] base = in.nextLine().split(" "); int n = Integer.valueOf(base[0]); int m = Integer.valueOf(base[1]); //货物 String[] shops = in.nextLine().split(" "); String[] helpers = new String[m]; for (int i = 0; i < m; i ++){ helpers[i] = in.nextLine(); } int result = 0; int[][] money = new int[m][4]; Set<Integer> check = new HashSet<>(); for (int i = 0; i < m; i ++){ String[] temp = helpers[i].split(" "); money[i][1] = Integer.valueOf(temp[0]); //优惠 money[i][0] = Integer.valueOf(temp[1]); } sortIntArray(money,new int[] {0,1}); int[] helpShow = new int[n]; for (int i = 0; i < n; i ++) { helpShow[i] = Integer.valueOf(shops[i]); result += helpShow[i]; } Arrays.sort(helpShow); for (int i = 0; i < n; i ++){ for (int j = m - 1; j >= 0; j --){ if (money[j][1] <= helpShow[i] && !check.contains(j)){ result -= money[j][0]; check.add(j); break; } } } System.out.println(result); } private static void sortIntArray(int[][] arObjects, final int[] arOrders) { Arrays.sort(arObjects, new Comparator<Object>() { public int compare(Object oObjectA, Object oObjectB) { int[] arTempOne = (int[])oObjectA; int[] arTempTwo = (int[])oObjectB; for (int i = 0; i < arOrders.length; i++) { int k = arOrders[i]; if (arTempOne[k] > arTempTwo[k]) { return 1; } else if (arTempOne[k] < arTempTwo[k]) { return -1; } else { continue; } } return 0; } }); } } 好渔夫的一个方法
点赞 回复 分享
发布于 2018-09-21 17:17
我看有人说贪心,不知道对不对,我是想着能拼单的,两个商品用一个优惠券
点赞 回复 分享
发布于 2018-09-21 17:09
同0 
点赞 回复 分享
发布于 2018-09-21 17:09
95 45 15 算了算满分100能得27分
点赞 回复 分享
发布于 2018-09-21 17:08
那个缓存命中的题目没读懂。。
点赞 回复 分享
发布于 2018-09-21 17:07
我是15,也很迷
点赞 回复 分享
发布于 2018-09-21 17:06
同0,自测了好几个case都没问题,不管怎么说都有个%***。。看不懂
点赞 回复 分享
发布于 2018-09-21 17:05

相关推荐

不愿透露姓名的神秘牛友
12-17 16:48
今天九点半到公司,我跟往常一样先扫了眼电脑,屁活儿没有。寻思着没事干,就去蹲了个厕所,回来摸出手机刷了会儿。结果老板刚好路过,拍了我一下说上班别玩手机,我吓得赶紧揣兜里。也就过了四十分钟吧,我的直属领导把我叫到小隔间,上来就给我一句:“你玩手机这事儿把老板惹毛了,说白了,你可以重新找工作了,等下&nbsp;HR&nbsp;会来跟你谈。”&nbsp;我当时脑子直接宕机,一句话都没憋出来。后面&nbsp;HR&nbsp;找我谈话,直属领导也在旁边。HR&nbsp;说我这毛病不是一次两次了,属于屡教不改,不光上班玩手机,还用公司电脑看论文、弄学校的事儿。我当时人都傻了,上班摸鱼是不对,可我都是闲得发慌的时候才摸啊!而且玩手机这事儿,从来没人跟我说过后果这么严重,更没人告诉我在公司学个习也算犯错!连一次口头提醒都没有,哪儿来的屡教不改啊?更让我膈应的是,昨天部门刚开了会,说四个实习生里留一个转正,让大家好好表现。结果今天我就因为玩手机被开了。但搞笑的是,开会前直属领导就把我叫去小会议室,明明白白告诉我:“转正这事儿你就别想了,你的学历达不到我们部门要求,当初招你进来也没打算给你这个机会。”合着我没入贵厂的眼是吧?可我都已经被排除在转正名单外了,摸个鱼至于直接把我开了吗?真的太离谱了!
rush$0522:转正名单没进,大概率本来就没打算留你
摸鱼被leader发现了...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务