华为第二届 疯狂程序员比赛第二场

1.模拟触摸屏的手势判断 

描述: 如今电子产品触摸屏非常普及,触摸屏最重要的特点是要感知用户的手势,如,感知手指是向上下左右哪个方向滑动。
请编写程序,输入用户手势轨迹上两个点坐标x1,y1和x2,y2,输出表示手势方向字符串(UP,DOWN,LEFT或者RIGHT),如果两点坐标相同,则输出字符串CLICK,表示是点击操作。假设屏幕左上角为坐标原点(0, 0),横向为x坐标,竖向为y坐标,输入4个数字以空格隔开。如果手势刚好是45度角方向,则根据x坐标方向判定为LEFT或者RIGHT。    
运行时间限制: 无限制 
内存限制: 无限制 
输入: 输入两个点坐标x1 y1 x2 y2
输出: 输出表示手势的字符串UP,DOWN,LEFT,RIGHT或CLICK(大写)
样例输入: 10 10 20 30
样例输出: DOWN

import java.util.Scanner;

public class main_1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=0,b=0,c=0,d=0;
        Scanner sc=new Scanner(System.in);        
        if(sc.hasNext())
        {
            a=sc.nextInt();
            b=sc.nextInt();
            c=sc.nextInt();
            d=sc.nextInt();
        }
        if(a==c&&d==b)System.out.println("TOUCH");
        else if(c-a>=0)
        {
            if(Math.abs(d-b)<=Math.abs(c-a)&&d-b>=0)System.out.print("RIGHT");
            else if(Math.abs(d-b)<=Math.abs(c-a)&&d-b<0)System.out.print("LEFT");
            else System.out.print("DOWN"); 
        }else if(c-a<0)
        {
            if(Math.abs(d-b)<=Math.abs(c-a)&&d-b>=0)System.out.print("RIGHT");
            else if(Math.abs(d-b)<=Math.abs(c-a)&&d-b<0)System.out.print("LEFT");
            else System.out.print("UP"); 
        }
    }

}

有一个测试用例没过,不太清楚原因。

上面的算法先将点分为向上,向下;可以进一步简化:

        if(a==c&&d==b)System.out.println("TOUCH");
        else if(c>a&&Math.abs(c-a)>=Math.abs(d-b))System.out.println("RIGHT");
        else if(c<a&&Math.abs(c-a)>=Math.abs(d-b))System.out.println("LEFT");
        else if(d>b&&Math.abs(c-a)<Math.abs(d-b))System.out.println("DOWN");
        else if(d<b&&Math.abs(c-a)<Math.abs(d-b))System.out.println("UP");

2. 掉坑? 名字没记

小明在一条小路上行走,每步的步长为65厘米,这条路上有很多坑。小明只要一只脚踏入坑中就会掉下去,请判断小明是否能安全通过这条小路。
假设路的起点坐标为0,终点坐标为500,单位为米。输入偶数个正整数,以空格分隔,每2个正整数用于描述一个坑,第一个表示坑的起点坐标,单位为米,第二个整数表示坑的宽度,单位为厘米。输入的坑之间是无序的。
如输入11 87 20 50,表示在11米处有个80cm的坑,在20米处有个50cm的坑。如果小明能安全通过,则输出YES,如果会掉坑里,则输出NO。刚好踩在坑沿上不会掉下去。

运行时间限制: 无限制 
内存限制: 无限制 
输入: 输入偶数个正整数,以空格分隔,每2个正整数用于描述一个坑,第一个表示坑的起点坐标,单位为米,第二个整数表示坑的宽度,单位为厘米。输入的坑之间是无序的。
输出: 如果小明能安全通过,输出YES,否则输出NO。另外,刚好踩在坑的边沿上不会掉下去。
样例输入: 1 30 
样例输出: YES 
答案提示: 注意单位换,另外,刚好踩在坑的边沿上不会掉下去 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class main_2 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = null;
        StringBuilder sb = new StringBuilder();
        input = br.readLine();
        while(!input .equals("")){
            sb.append(input.trim());
            sb.append(" ");
            input = br.readLine();
        }
        String[] temp=sb.toString().trim().replaceAll("\\s+", " ").split(" ");
        for(int i=0;i<temp.length-1;)
        {
            if(Integer.parseInt(temp[i+1])>65)
            {
                System.out.println("NO");
                
            }else if((Integer.parseInt(temp[i])*100/65+1)*65>=
                (Integer.parseInt(temp[i])*100+Integer.parseInt(temp[i+1])))
            {
                System.out.println("YES");
            }else System.out.println("NO");
            i+=2;
        }
    }
}

程序的问题在于 需要多输入一个回车才能结束输入 实在不知道这种情况下应该怎样结束输入。如有朋友知道,请告诉我,这其实是一个非常重要的地方。


3.公司电脑设备管理 

描述: 本系统,可以管理公司电脑的一些常用信息,主要功能如下:
1)入库电脑,即公司通过其他电脑设备公司购买新电脑后,需要把这些电脑先进行编号,然后添加到管理库中,电脑编号长度小于等于10位,由字母和数字组成,一个电脑编号只能唯一对应一台电脑。
2)清除电脑,当电脑坏掉,或者年生太久不能再使用时,需要清理出管理库。
3)员工领用电脑。领用时需要输入员工ID,员工ID长度固定为8位阿拉伯数字组成的字符串。一台电脑只能由一名员工领用,但一名员工可以同时领多台电脑,员工ID是员工的唯一识别码。
4)员工退还电脑。
5)统计所有员工当前正在使用的电脑,并按照员工工号由小到大的顺序输出,如果一名员工拥有在多台电脑,还需要进行2次排序,即按照电脑的编号进行排序,排序规则为:字典排序法,按照字母编码从小到大进行。

 
运行时间限制: 无限制 
内存限制: 无限制 
输入: 整数M,增加的电脑数量;整数N,清除电脑数量;整数P,员工领用电脑数量;整数Q,员工退还的电脑数量

M行字符串,电脑标识号,每行标识一台增加的电脑;如果失败,输出"add computer error:"+电脑标识号;成功不输出

N行字符串,电脑标识号,每行标识一台清除的电脑;如果失败,输出"remove computer error:"+电脑标识号;成功不输出

P行字符串,电脑标识号和员工工号,每行标识员工领用一台电脑;如果失败,输出"assign new computer
error:"+电脑标识号+员工工号;成功不输出

Q行字符串,电脑标识号和员工工号,每行标识员工退还的一台电脑;如果失败,输出"send back computer error:"+电脑标识号和员工工号;成功不输出
输出: 统计所有员工当前正在使用的电脑,并按照员工工号由小到大的顺序输出,如果一名员工拥有在多台电脑,还需要进行2次排序,即按照电脑的编号进行排序,排序规则为:字典排序法,按照字母编码从小到大进行。
整数K,所有员工当前正在使用的电脑的数量字符串,电脑排序后的结果,"工号_电脑号"

样例输入: 3 2 2 1
SN001
SN002
SN003
SN003
SN004
SN001 12345678
SN002 12345678
SN002 12345678 
样例输出: remove computer error:SN004
1
12345678_SN001  

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.TreeMap;

public class main_3 {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = null;
        StringBuilder sb = new StringBuilder();
        input = br.readLine();
        while (!input.equals("")) {
            sb.append(input.trim());
            sb.append(" ");
            input = br.readLine();
        }
        TreeMap<String, Integer> a = new TreeMap();
        // jieshou shuru
        String[] temp = sb.toString().trim().replaceAll("\\s+", " ").split(" ");
        int m = Integer.parseInt(temp[0].toString());
        int n = Integer.parseInt(temp[1].toString());
        int p = Integer.parseInt(temp[2].toString());
        int q = Integer.parseInt(temp[3].toString());
        // insert
        for (int i = 4; i < m + 4; i++) {
            if (temp[i].length() > 10 || a.get(temp[i]) != null) {
                System.out.println("add computer error:" + temp[i]);
            } else
                a.put(temp[i], 0);
        }
        // delete
        for (int i = 4 + m; i < 4 + m + n; i++) {
            if (temp[i].length() > 10 || a.get(temp[i]) == null) {
                System.out.println("remove computer error:" + temp[i]);
            } else
                a.remove(temp[i]);
        }
        // borrow computer
        for (int i = 4 + m + n; i < 4 + m + n + 2 * p;) {
            if (temp[i].length() > 10 || a.get(temp[i]) == null
                    || Integer.parseInt(a.get(temp[i]).toString()) != 0) {
                System.out.println("assign new computer error:" + temp[i] + "_"
                        + temp[i + 1]);
            } else {
                a.remove(temp[i]);
                a.put(temp[i], Integer.parseInt(temp[i + 1]));
            }
            i = i + 2;
        }
        // return computer "send back computer error:"+电脑标识号和员工工号
        for (int i = 4 + m + n + 2 * p; i < temp.length;) {
            if (temp[i].length() > 10
                    || a.get(temp[i]) == null
                    || Integer.parseInt(a.get(temp[i]).toString()) != Integer
                            .parseInt(temp[i + 1])) {
                System.out.println("send back computer error:" + temp[i] + "_"
                        + temp[i + 1]);
            } else {
                a.remove(temp[i]);
                a.put(temp[i], 0);
            }
            i = i + 2;

        }
//        System.out.println(a.size());
        StringBuilder test = new StringBuilder();
        int temp2 = Integer.valueOf(a.size());
        int temp3 = 0;
        String[] container = null;
        for (int i = 0; i < temp2; i++) {
            // System.out.println(a.lastKey());
            if (Integer.parseInt(a.get(a.lastKey()).toString()) != 0) {
                test.append(a.get(a.lastKey()).toString() + "_"
                        + a.lastKey().toString().trim());
                test.append(" ");
                temp3++;
            }
            container = test.toString().trim().split(" ");
            a.remove(a.lastKey());
            // System.out.println(container[i]);
        }
        System.out.println(container.length);
        if (container.length == 1) {
            System.out.println(container[0]);
        } else if (container.length > 1) {
            sortStringArray(container);
            for (int i = 0; i < container.length; i++) {
                System.out.println(container[i]);
            }
        }
    }

    public static void sortStringArray(String[] arrStr) {
        String temp;
        for (int i = 0; i < arrStr.length; i++) {
            for (int j = arrStr.length - 1; j > i; j--) {
                if (arrStr[i].compareTo(arrStr[j]) > 0) {
                    temp = arrStr[i];
                    arrStr[i] = arrStr[j];
                    arrStr[j] = temp;
                }
            }
        }
    }
}

这道题样例输入可以过,但是测试用例没有过,同样不清楚原因。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值