拼多多笔试:满减优惠

拼多多,满减优惠问题,感觉结果没错啊,为什么通过率为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);
        }
}
#拼多多#
全部评论
同0,自测了好几个case都没问题,不管怎么说都有个%***。。看不懂
点赞
送花
回复
分享
发布于 2018-09-21 17:05
我是15,也很迷
点赞
送花
回复
分享
发布于 2018-09-21 17:06
秋招专场
校招火热招聘中
官网直投
那个缓存命中的题目没读懂。。
点赞
送花
回复
分享
发布于 2018-09-21 17:07
95 45 15 算了算满分100能得27分
点赞
送花
回复
分享
发布于 2018-09-21 17:08
同0 
点赞
送花
回复
分享
发布于 2018-09-21 17:09
我看有人说贪心,不知道对不对,我是想着能拼单的,两个商品用一个优惠券
点赞
送花
回复
分享
发布于 2018-09-21 17:09
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
唯一一题完全ac。先把商品按价格排序,先从价格小点的买起,然后买每个商品再按满减最多的买(不能满减就按原价买),买完这张优惠券就不能再用了。
点赞
送花
回复
分享
发布于 2018-09-21 17:19
商品价钱从小到大 满减优惠从大到小 优惠一致满减上限从小到大 贪心 用过了标记
点赞
送花
回复
分享
发布于 2018-09-21 17:24
#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
我也是0,这个题目做了好久
点赞
送花
回复
分享
发布于 2018-09-21 17:29
算法岗都是只考编程题么.感觉不应该投这个岗,太难考了
点赞
送花
回复
分享
发布于 2018-09-21 17:31

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务