JAVA 数进制转换小练
真是JAVA在手 万事无忧啊。。。
POJ 2798 二进制到十六进制转换
import
java.util.
*
;
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("1");
int t;
t=cin.nextInt();
int i;
for(i=1;i<=t;i++)
{
a=cin.nextBigInteger(2);
String ans=new String("");
ans=a.toString(16);
ans=ans.toUpperCase();
System.out.println(ans);
}
}
}
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("1");
int t;
t=cin.nextInt();
int i;
for(i=1;i<=t;i++)
{
a=cin.nextBigInteger(2);
String ans=new String("");
ans=a.toString(16);
ans=ans.toUpperCase();
System.out.println(ans);
}
}
}
POJ 2735 八进制到十进制
import
java.util.
*
;
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("1");
a=cin.nextBigInteger(8);
String ans=new String("");
ans=a.toString(10);
System.out.println(ans);
}
}
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("1");
a=cin.nextBigInteger(8);
String ans=new String("");
ans=a.toString(10);
System.out.println(ans);
}
}
POJ 2734 十进制到八进制
import
java.util.
*
;
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("1");
a=cin.nextBigInteger(10);
String ans=new String("");
ans=a.toString(8);
System.out.println(ans);
}
}
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("1");
a=cin.nextBigInteger(10);
String ans=new String("");
ans=a.toString(8);
System.out.println(ans);
}
}
POJ 2972 数进制判定
import
java.util.
*
;
import java.math. * ;
public class Main {
static boolean check(String s,int radix)
{
int i;
int len=s.length();
for(i=0;i<len;i++)
{
if((s.charAt(i)-'0')>=radix)
return false;
}
return true;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner cin = new Scanner( System.in );
String a=new String ("");
String b=new String ("");
String c=new String ("");
int t;
t=cin.nextInt();
int i,j,k;
int f=0;
for(i=1;i<=t;i++)
{
f=0;
a=cin.next();
b=cin.next();
c=cin.next();
for(j=2;j<=16;j++)
{
if(check(a,j)==false||check(b,j)==false||check(c,j)==false)
continue;
BigInteger a1=new BigInteger(a,j);
BigInteger b1=new BigInteger(b,j);
BigInteger c1=new BigInteger(c,j);
if(c1.compareTo(a1.multiply(b1))==0)
{
f=1;
break;
}
}
if(f==1)
{
System.out.println(j);
}
else
System.out.println("0");
}
}
}
import java.math. * ;
public class Main {
static boolean check(String s,int radix)
{
int i;
int len=s.length();
for(i=0;i<len;i++)
{
if((s.charAt(i)-'0')>=radix)
return false;
}
return true;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner cin = new Scanner( System.in );
String a=new String ("");
String b=new String ("");
String c=new String ("");
int t;
t=cin.nextInt();
int i,j,k;
int f=0;
for(i=1;i<=t;i++)
{
f=0;
a=cin.next();
b=cin.next();
c=cin.next();
for(j=2;j<=16;j++)
{
if(check(a,j)==false||check(b,j)==false||check(c,j)==false)
continue;
BigInteger a1=new BigInteger(a,j);
BigInteger b1=new BigInteger(b,j);
BigInteger c1=new BigInteger(c,j);
if(c1.compareTo(a1.multiply(b1))==0)
{
f=1;
break;
}
}
if(f==1)
{
System.out.println(j);
}
else
System.out.println("0");
}
}
}
POJ 2981 大整数加法
import
java.util.
*
;
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("0");
BigInteger b=new BigInteger("0");
a=cin.nextBigInteger();
b=cin.nextBigInteger();
System.out.println(a.add(b));
}
}
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("0");
BigInteger b=new BigInteger("0");
a=cin.nextBigInteger();
b=cin.nextBigInteger();
System.out.println(a.add(b));
}
}
POJ 2980 大整数乘法
import
java.util.
*
;
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("0");
BigInteger b=new BigInteger("0");
a=cin.nextBigInteger();
b=cin.nextBigInteger();
System.out.println(a.multiply(b));
}
}
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("0");
BigInteger b=new BigInteger("0");
a=cin.nextBigInteger();
b=cin.nextBigInteger();
System.out.println(a.multiply(b));
}
}
POJ 2737 大整数 除法
import
java.util.
*
;
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
int t;
Scanner cin = new Scanner( System.in );
t=cin.nextInt();
BigInteger a=new BigInteger("0");
BigInteger b=new BigInteger("0");
int i;
for(i=1;i<=t;i++)
{
a=cin.nextBigInteger();
b=cin.nextBigInteger();
System.out.println(a.divide(b));
}
}
}
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
int t;
Scanner cin = new Scanner( System.in );
t=cin.nextInt();
BigInteger a=new BigInteger("0");
BigInteger b=new BigInteger("0");
int i;
for(i=1;i<=t;i++)
{
a=cin.nextBigInteger();
b=cin.nextBigInteger();
System.out.println(a.divide(b));
}
}
}
POJ 2738 高精度加法
import
java.util.
*
;
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
int t;
Scanner cin = new Scanner( System.in );
t=cin.nextInt();
BigDecimal a=new BigDecimal("0");
BigDecimal b=new BigDecimal("0");
int i;
for(i=1;i<=t;i++)
{
a=cin.nextBigDecimal();
b=cin.nextBigDecimal();
System.out.println(a.add(b).stripTrailingZeros());
}
}
}
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
int t;
Scanner cin = new Scanner( System.in );
t=cin.nextInt();
BigDecimal a=new BigDecimal("0");
BigDecimal b=new BigDecimal("0");
int i;
for(i=1;i<=t;i++)
{
a=cin.nextBigDecimal();
b=cin.nextBigDecimal();
System.out.println(a.add(b).stripTrailingZeros());
}
}
}
POJ 2809 2^N次方
import
java.util.
*
;
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
int t;
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("2");
int exp;
exp=cin.nextInt();
System.out.println(a.pow(exp));
}
}
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
int t;
Scanner cin = new Scanner( System.in );
BigInteger a=new BigInteger("2");
int exp;
exp=cin.nextInt();
System.out.println(a.pow(exp));
}
}
POJ 2951 POJ 1001 高精度幂
import
java.util.
*
;
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigDecimal a=new BigDecimal("0");
int exp;
while(cin.hasNext())
{
a=cin.nextBigDecimal();
exp=cin.nextInt();
a=a.pow(exp);
a=a.stripTrailingZeros();
String ans=new String ("");
ans=a.toPlainString();
int i;
int len=ans.length();
for(i=0;i<len;i++)
{
if(ans.charAt(i)=='.')
{
ans=ans.substring(i);
break;
}
if(ans.charAt(i)!='0')
break;
}
System.out.println(ans);
}
}
}
import java.math. * ;
/** */ /**
*
* @author weitao
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner( System.in );
BigDecimal a=new BigDecimal("0");
int exp;
while(cin.hasNext())
{
a=cin.nextBigDecimal();
exp=cin.nextInt();
a=a.pow(exp);
a=a.stripTrailingZeros();
String ans=new String ("");
ans=a.toPlainString();
int i;
int len=ans.length();
for(i=0;i<len;i++)
{
if(ans.charAt(i)=='.')
{
ans=ans.substring(i);
break;
}
if(ans.charAt(i)!='0')
break;
}
System.out.println(ans);
}
}
}