SRM145_DIV2

希望可以坚持下来,一天写一章博客

250

题目乱糟糟没怎么懂,看答案发下就是一个简单的集合判断存在不存在的问题

import java.util.*;
public class ImageDithering{
    public int count(String dithered, String[] screen){
        Set<Character> set = new HashSet<>();
        for (char i : dithered.toCharArray()){
            set.add(i);
        }
        int result = 0;
        for (String i : screen){
            for (char j : i.toCharArray()){
                if (set.contains(j)) result++;
            }
        }
        return result;
    }
}

500

还是题没怎么读懂,看答案懂了。。就是看看限定时间内,以1S为最小单位有几个整数百分比出现

public class ExerciseMachine{
    public int getPercentages(String time){
        String[] split = time.split(":");
        int h = Integer.valueOf(split[0]);
        int m = Integer.valueOf(split[1]);
        int s = Integer.valueOf(split[2]);
        int seconds = s + m * 60 + h * 3600;
        int sum;
        for (int i = 1; i < seconds; i++){
            if (i * 100 % seconds == 0){
                sum++;
            }
        }
        return sum;
    }
}

1100

做个编程题跟阅读理解似得。。。问题不难,写两个功能函数,一个是获取最大列位置,一个是判断是顺时针还是逆时针旋转。

public class VendingMachine{
    private int[][] machine;
    private int rows, cols;
    private int sumi;
    public int motorUse(String[] prices, String[] purchases){
        rows = prices.length;
        machine = new int[rows + 1][];
        sumi = prices.length;
        for ( int i = 0; i < rows; i++){
            String[] strVals = prices[i].split(" ");
            machine[i] = new int[strVals.length];
            for (int j = 0; j < strVals.length; j++){
                machine[i][j] = Integer.valueOf(strVals[j]);
            }
        }
        cols = machine[0].length;
        machine[sumi] = new int[cols];

        for (int j = 0; j < cols; j++){
            int sum = 0;
            for (int i = 0; i < rows; i++){
                sum += machine[i][j];
            }
            machine[sumi][j] = sum;
        }

        int max = getMaxColumn();
        int seconds = getRotateTime(0 , max);
        int currentColumn = max;
        int lastMinute = Integer.valueOf(purchases[0].split(":")[1]);

        for (int i = 0; i < purchases.length; i++){
            String[] coordsMinute = purchases[i].split(":");
            String[] coords = coordsMinute[0].split(",");
            int row = Integer.valueOf(coords[0]);
            int col = Integer.valueOf(coords[1]);
            int minute = Integer.valueOf(coordsMinute[1]);

            if (minute - lastMinute >= 5){
                max = getMaxColumn();
                seconds += getRotateTime(currentColumn, max);
                currentColumn = max;
            }

            if (machine[row][col] == 0){
                return -1;
            }
            seconds += getRotateTime(currentColumn, col);
            currentColumn = col;
            machine[sumi][col] -= machine[row][col];
            machine[row][col] = 0;
            lastMinute = minute;
        }
            seconds += getRotateTime(currentColumn, getMaxColumn());

            return seconds;
    }

    private int getRotateTime(int currentColumn, int destinationColumn){
        int abs = Math.abs(destinationColumn - currentColumn);
        return Math.min(abs, cols - abs);
    }

    private int getMaxColumn(){
        int maxi = 0;
        int max = machine[sumi][0];
        for (int i = 1; i < machine[0].length; i++){
            if (machine[sumi][i] > max){
                maxi = i;
                max = machine[sumi][i];
            }
        }
        return maxi;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值