pat1010
来源:互联网 发布:显示二维码的数据 编辑:程序博客网 时间:2024/06/11 23:30
package AdvancedLevelPractice;import java.util.Scanner;public class pat1010 {public static void main(String[] args) {// TODO Auto-generated method stubScanner scanner = new Scanner(System.in);String N1 = scanner.next();String N2 = scanner.next();long tag = scanner.nextInt();long radix = scanner.nextInt();String tmpString;if(tag == 2){tmpString = N1;N1 = N2;N2 = tmpString;}long N1Decimal = getDecimal(N1,radix);long low = getLow(N2);long high = N1Decimal;long radix2 = -1;if(low >= high){radix2 = compare(N1Decimal, N2, low);}else {radix2 = getResults(radix2, N1Decimal, N2, low, high);}if(radix2 == -1){System.out.print("Impossible");}else {System.out.print(radix2);}scanner.close();}private static long compare(long n1Decimal, String n2, long radix) {// TODO Auto-generated method stubwhile(true){long sum = 0;long h = 1;for(int i = n2.length() - 1; i >= 0; i--){char ch = n2.charAt(i);if(ch <= '9'){sum += (ch - '0') * h;}else {sum += (ch - 'a' + 10) * h;}h *= radix;if(sum > n1Decimal){return -1;}}if(sum == n1Decimal){return radix;}radix++;}}private static long getResults(long radix2, long n1Decimal, String n2, long low, long high) {// TODO Auto-generated method stublong radix = (low + high) / 2;//System.out.println(low+"\t"+high+"\t"+"radix = "+radix);long sum = 0;long h = 1;for(int i = n2.length() - 1; i >= 0; i--){char ch = n2.charAt(i);if(ch <= '9'){sum += (ch - '0') * h;}else {sum += (ch - 'a' + 10) * h;}if(sum > n1Decimal || sum < 0){high = radix - 1;return getResults(radix2, n1Decimal, n2, low, high);}h *= radix;}if(sum == n1Decimal){radix2 = radix;high = radix - 1;return getResults(radix2, n1Decimal, n2, low, high);}if(low >= high){return radix2;}if (sum < n1Decimal) {low = radix + 1;return getResults(radix2, n1Decimal, n2, low, high);}return radix2;}private static long getLow(String n2) {// TODO Auto-generated method stublong radix = 0;long tmp;for(int i = n2.length() - 1; i >= 0; i--){char ch = n2.charAt(i);if(ch <= '9'){tmp = ch - '0';}else {tmp = ch - 'a' + 10;}if(tmp > radix){radix = tmp;}}radix++;return radix;}private static long getDecimal(String n1, long radix) {// TODO Auto-generated method stublong sum = 0;long h = 1;for(int i = n1.length() - 1; i >= 0; i--){char ch = n1.charAt(i);if(ch <= '9'){sum += (ch - '0') * h;}else {sum += (ch - 'a' + 10) * h;}h *= radix;}return sum;}}
0 0
- pat1010
- PAT1010
- PAT1010
- pat1010
- pat1010
- PAT1010
- PAT1010
- pat1010
- PAT1010
- pat1010 Radix
- PAT1010(BUG)
- pat1010. Radix
- PAT1010 Radix
- PAT1010. 一元多项式求导
- pat1010. Radix (25) BUG!!!
- pat1010.Radix(25)
- PAT1010完美数列
- pat1010:一元多项式求导
- HttpClient使用详解
- PMP考试六大管理学定律
- boa服务器移植使用
- Android 文件下载
- Lua中的操作系统库
- pat1010
- 归档日志满导致数据库挂起故障处理
- (10) Linux ----- 使用pscp实现Windows 和 Linux服务器间远程拷贝文件
- 关于chrome插件开发(二)
- 最高法审结海富投资诉甘肃世恒案:确立PE投资对赌原则
- 最大堆的插入/删除/调整/排序操作(图解+程序)(JAVA)
- ffmpeg之rtsp分析流程
- Android Drawable Resource学习(十二)、ShapeDrawable还是GradientDrawable?
- DG搭建Windows 下注意