POJ 刷题日记

POJ 刷题日记

1. a + b 问题,题号 1000

package org.poj;

import java.util.Scanner;

/**
 * Calculate a+b
 * Two integer a,b (0<=a,b<=10)
 *
 * Q: Where are the input and the output?
 *
 * A: Your program shall always read input from stdin (Standard Input) and write output to stdout (Standard Output).
 * For example, you can use 'scanf' in C or 'cin' in C++ to read from stdin, and use 'printf' in C or 'cout' in C++ to write to stdout.
 *
 * You shall not output any extra data to standard output other than that required by the problem, 
 * otherwise you will get a "Wrong Answer".
 *
 * User programs are not allowed to open and read from/write to files. 
 * You will get a "Runtime Error" or a "Wrong Answer"if you try to do so.
 */
public class SumAAndB {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            System.out.println("请输入两个 int 类型数 a 和 b,输入每一个数后以回车结束!");
            System.out.println("其中 0 ≤ a,b ≤ 10");
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            System.out.println(a + b);
        } finally {
            scanner.close();
        }
    }
}

2. Exponentiation(求幂),题号 1001

package org.poj;

import java.math.BigDecimal;
import java.util.*;

/**
 * Problems involving the computation of exact values of very large magnitude and precision are common.
 * For example, the computation of the national debt is a taxing experience for many computer systems.
 *
 * This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
 *
 * Input:
 * The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
 *
 * Output:
 * The output will consist of one line for each line of input giving the exact value of R^n.
 * Leading zeros should be suppressed in the output.
 * Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.
 *
 * Sample Input
 *
 * 95.123 12
 * 0.4321 20
 * 5.1234 15
 * 6.7592  9
 * 98.999 10
 * 1.0100 12
 *
 * Sample Output
 *
 * 548815620517731830194541.899025343415715973535967221869852721
 * .00000005148554641076956121994511276767154838481760200726351203835429763013462401
 * 43992025569.928573701266488041146654993318703707511666295476720493953024
 * 29448126.764121021618164430206909037173276672
 * 90429072743629540498.107596019456651774561044010001
 * 1.126825030131969720661201
 *
 * Hint
 *
 * If you don't know how to determine wheather encounted the end of input:
 * s is a string and n is an integer
 */
public class Exponentiation {

    public static void main(String[] args) throws Exception {
        System.out.println("请输入您要计算的元数据与幂数!");
        System.out.println("要求元数据的值占第一列到第六列,幂数占第八列和第九列!");
        Scanner scanner = new Scanner(System.in);
        List<BigDecimal> array = new ArrayList<>();
        try {
            boolean flag = true;
            while (flag) {
                String text = scanner.nextLine();
                if ("end!".equals(text)) flag = false;
                if (text.length() != 9) {
                    continue;
                }
                BigDecimal metaBigDecimal = new BigDecimal(text.substring(0, 6));
                if (metaBigDecimal.doubleValue() < 0 || metaBigDecimal.doubleValue() > 99.999d) {
                    System.out.println("您输入的数据有问题,请重新输入,如想结束,请换行输入 \"end!\" 回车结束!");
                    continue;
                }
                int power = Integer.parseInt(
                    text.substring(7).startsWith(" ") ? text.substring(7).replace(" ", "0") : text.substring(7));
                if (power <= 0 || power > 25) {
                    System.out.println("您输入的数据有问题,请重新输入,如想结束,请换行输入 \"end!\" 回车结束!");
                    continue;
                }
                BigDecimal element = metaBigDecimal.pow(power);
                array.add(element);
            }

            for (BigDecimal bigDecimal : array) {
                // 这里是为了不使用科学计数法输出
                String elementStr = bigDecimal.stripTrailingZeros().toPlainString();
                if (elementStr.startsWith("0.")) {
                    elementStr = elementStr.substring(1);
                }
                System.out.println(elementStr);
            }
        } finally {
            scanner.close();
        }
    }

}

3. 电话号码别名,题号 1002

package org.poj;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.regex.Pattern;

/**
 * <p> Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable 
 * word or phrase.
 * For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP.
 * Sometimes only part of the number is used to spell a word.
 * When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO.
 * Another way to make a telephone number memorable is to group the digits in a memorable way.
 * You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10.
 *
 * The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200).
 * The keypad of a phone supplies the mapping of letters to numbers, as follows:
 *
 * A, B, and C map to 2
 * D, E, and F map to 3
 * G, H, and I map to 4
 * J, K, and L map to 5
 * M, N, and O map to 6
 * P, R, and S map to 7
 * T, U, and V map to 8
 * W, X, and Y map to 9
 *
 * There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary.
 * The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.
 *
 * Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)
 *
 * Your company is compiling a directory of telephone numbers from local businesses.
 * As part of the quality control process you want to check that no two (or more) businesses in the directory have
 * the same telephone number.
 *
 * Input:
 * The input will consist of one case. The first line of the input specifies 
 * the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line.
 * The remaining lines list the telephone numbers in the directory, 
 * with each number alone on a line. Each telephone number consists of a string composed of decimal digits,
 * uppercase letters (excluding Q and Z) and hyphens.
 * Exactly seven of the characters in the string will be digits or letters.
 *
 * Output
 *
 * Generate a line of output for each telephone number that appears more than once in any form.
 * The line should give the telephone number in standard form, followed by a space, 
 * followed by the number of times the telephone number appears in the directory.
 * Arrange the output lines by telephone number in ascending lexicographical order. 
 * If there are no duplicates in the input print the line:
 *
 * No duplicates.
 *
 * Sample Input
 *
 * 12
 * 4873279
 * ITS-EASY
 * 888-4567
 * 3-10-10-10
 * 888-GLOP
 * TUT-GLOP
 * 967-11-11
 * 310-GINO
 * F101010
 * 888-1200
 * -4-8-7-3-2-7-9-
 * 487-3279
 *
 * Sample Output
 *
 * 310-1010 2
 * 487-3279 4
 * 888-4567 3
 */
public class Telephone {

    static Map<Character, Integer> mapper = new HashMap<>(33);

    static {
        mapper.put('A', 2); mapper.put('B', 2); mapper.put('C', 2);
        mapper.put('D', 3); mapper.put('E', 3); mapper.put('F', 3);
        mapper.put('G', 4); mapper.put('H', 4); mapper.put('I', 4);
        mapper.put('J', 5); mapper.put('K', 5); mapper.put('L', 5);
        mapper.put('M', 6); mapper.put('N', 6); mapper.put('O', 6);
        mapper.put('P', 7); mapper.put('R', 7); mapper.put('S', 7);
        mapper.put('T', 8); mapper.put('U', 8); mapper.put('V', 8);
        mapper.put('W', 9); mapper.put('X', 9); mapper.put('Y', 9);
    }

    public static void main(String[] args) throws Exception {
        System.out.println("请输入电话号码数量和电话号码,第一行为号码的数量,下面的每一行都为电话号码!");
        System.out.println("输入的格式是,第一行是一个正整数,指定电话号码薄中号码的数量(最多100000)。余下的每行是一个电话号码。每个电话号码由数字,大写字母(除了Q和Z)以及连接符组成。每个电话号码中只会刚好有7个数字或者字母。");
        Scanner scanner = new Scanner(System.in);
        Pattern pattern = Pattern.compile("^\\d$");
        Map<String, Integer> res = new TreeMap<>();
        try {
            int numberCount = scanner.nextInt();
            String[] telephoneAliasArray = new String[numberCount];
            for (int i = 0; i < numberCount; i++) {
                String telephoneAlias = scanner.nextLine();
                if ("".equals(telephoneAlias) || telephoneAlias.contains("Q") || telephoneAlias.contains("Z")) {
                    i--;
                    continue;
                }
                telephoneAliasArray[i] = telephoneAlias;
            }

            for (int i = 0; i < telephoneAliasArray.length; i++) {
                char[] charArray = telephoneAliasArray[i].toCharArray();
                StringBuilder telephone = new StringBuilder();
                for (char c : charArray) {
                    if ('-' == c) {
                        continue;
                    }
                    if (pattern.matcher(String.valueOf(c)).matches()) {
                        telephone.append(c);
                        continue;
                    }
                    Integer integer = mapper.get(c);
                    telephone.append(integer);
                }
                telephone.insert(3, '-');
                Integer repeat;
                if (null != (repeat = res.get(telephone.toString()))) {
                    res.put(telephone.toString(), ++repeat);
                    continue;
                }
                res.put(telephone.toString(), 1);
            }

            for (Map.Entry<String, Integer> entry : res.entrySet()) {
                if (entry.getValue() != 1) {
                    System.out.println(entry.getKey() + " " + entry.getValue());
                }
            }

        } finally {
            scanner.close();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值