zoukankan      html  css  js  c++  java
  • OJ刷题——我爱淘宝

    题目描述

    最近superbin爱上了淘宝,一口气花重金买了好多好多的宝贝,O(∩_∩)O~ 
    淘宝确实是个很方便的网上购物平台。首先,你需要注册一个淘宝账号,然后开通支付宝,再开通网上银行。选一件自己喜欢的宝贝,付款给淘宝。收到货后满意了,然后淘宝再把钱付给卖方,听起来挺不错的,还很安全。有空一定要试试哦。 
    superbin去逛淘宝,里面的宝贝实在是太多了,不过还好有很多排序方式: 
    1)按价格从低到高排序; 
    2)按价格从高到低排序; 
    3)按销量从高到低排序。 
    其实还有很多,就列举这三个吧,现在就请你编程实现这个功能吧!

    输入

    有多组测试数据,每组数据的第一行是两个整数n,m,n表示有n件物品,1≤n≤100。M表示排序的方式(1≤m≤3)接下来的n行每一行是一个物品的信息,包括:
    a.物品的名称name,由字母和数字组成,长度不超过100,且不为空;
    b.价格 price,为正整数;
    c.销量 sale,为非负整数;
    保证数据都在int范围内。
    然后排序的关键字相等,就按name的字典序排序,保证没有重复的name。

    输出

    排序后的结果

    样例输入
    3 1
    Tshirt1 39 100
    Tshirt2 49 10
    Tshirt3 129 3
    
    
    样例输出
    Tshirt1 39 100
    Tshirt2 49 10
    Tshirt3 129 3

    解题思路:把商品抽象为Item类,重写compareTo方法实现该类对象的可比较性。数据读入过程中,将这些对象存入一个ArrayList中,对这些元素进行一次根据名字的排序。同时把每个对象的价格和销量分别存入pri数组和sal数组中,根据读入的排序操作类别对对应的数组进行排序,遍历ArrayList中的对象按要求输出。注意:输出过的对象要移除,避免重复输出。
    附上代码:
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Scanner;
    
    public class Main{
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner scan = new Scanner(System.in);
            while (scan.hasNext()) {
                int n = scan.nextInt();
                int m = scan.nextInt();
                scan.nextLine();
                ArrayList<Item> al = new ArrayList();
                int pri[] = new int[n];
                int sal[] = new int[n];
                for (int i = 0; i < n; i++) {
                    String str[] = scan.nextLine().split(" +");
                    String name = str[0];
                    int price = Integer.valueOf(str[1]);
                    int sale = Integer.valueOf(str[2]);
                    al.add(new Item(name, price, sale));
                    sal[i] = sale;
                    pri[i] = price;
                }
                Collections.sort(al);
                if(m==1) {
                    //按价格从低到高排序
                    Arrays.parallelSort(pri);
                    for(int i = 0;i<n;i++) {
                        for(int j = 0;j<al.size();j++) {
                            if(pri[i]==al.get(j).price) {
                                System.out.println(al.get(j).toString());
                                al.remove(j);
                            }
                        }
                    }
                }else if(m==2) {
                    //按价格从高到低排序
                    Arrays.parallelSort(pri);
                    for(int i = n-1;i>=0;i--) {
                        for(int j = 0;j<al.size();j++) {
                            if(pri[i]==al.get(j).price) {
                                System.out.println(al.get(j).toString());
                                al.remove(j);
                            }
                        }
                    }
                }else if(m==3) {
                    //按销量从高到低排序
                    Arrays.parallelSort(sal);
                    for(int i = n-1;i>=0;i--) {
                        for(int j = 0;j<al.size();j++) {
                            if(sal[i]==al.get(j).sale) {
                                System.out.println(al.get(j).toString());
                                al.remove(j);
                            }
                        }
                    }
                }
            }
        }
    
    }
    
    class Item implements Comparable<Item> {
        String name;
        int price;
        int sale;
    
        Item(String name, int price, int sale) {
            this.name = name;
            this.price = price;
            this.sale = sale;
        }
        
        @Override
        public String toString() {
            return name + " " + price + " " + sale;
        }
    
        @Override
        public int compareTo(Item o) {
            // 按名称从小到大排序
            if (this.name.compareTo(o.name) > 0) {
                return 1;
            } else if (this.name.compareTo(o.name) < 0) {
                return -1;
            }
            // 相等情况
            return 0;
        }
    }
    
    
    
     
  • 相关阅读:
    UVa 12235 状压DP Help Bubu
    UVa 1407 树形背包 Caves
    UVa 11552 DP Fewest Flops
    webgl helloworld
    webgl 初识2
    laya3d 文件格式
    webgl 初识1
    HTTP消息头(HTTP headers)-常用的HTTP请求头与响应头
    gulp 定义依赖关系
    你真的了解内存泄露吗?
  • 原文地址:https://www.cnblogs.com/lzhxue/p/12741612.html
Copyright © 2011-2022 走看看