题目1026:又一版 A+B
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:7654
解决:2069
-
题目描述:
-
输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。
-
输入:
-
输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
当m为0时输入结束。
-
输出:
-
输出格式:每个测试用例的输出占一行,输出A+B的m进制数。
-
样例输入:
-
8 1300 48 2 1 7 0
-
样例输出:
-
2504 1000
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main{ /** * @param args */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while( scanner.hasNext() ){ int m = scanner.nextInt(); if(m == 0){ break; } Long a = scanner.nextLong(); Long b = scanner.nextLong(); long result = a + b; List<Long> resultList = changeToMSystem(result, m); for (int i = 0; i < resultList.size(); i++) { char c = mapIntToChar(resultList.get(i)); System.out.print(c); } System.out.println(); } } public static char mapIntToChar(Long n){ if(n >=0 && n <=9){ return (char)('0'+n); }else{ return (char) ('A' + n - 10); } } private static List<Long> changeToMSystem(long a, int m) { List<Long> resultList = new ArrayList<Long>(); if(a == 0){ resultList.add(0L); return resultList; } while(a != 0){ resultList.add(a%m); a = a / m; } List<Long> list = new ArrayList<Long>(); for (int i = resultList.size()-1; i >=0; i--) { list.add(resultList.get(i)); } return list; } } /************************************************************** Problem: 1026 User: yihukurama Language: Java Result: Accepted Time:100 ms Memory:15504 kb ****************************************************************/