ACM中java的使用
|
這里指的java速成,只限于java語法,包括輸入輸出,運算處理,字符串和高精度的處理,進制之間的轉換等,能解決OJ上的一些高精度題目。 1. 輸入: 例程: public class Main
例程: public class Main 規格化的輸出:
例程: public class Main
例程: public class Main
import java.io.*; import java.util.*; import java.math.*;![]() public class Main![]() ![]() { public static void main(String[] args)![]() { int b; BigInteger p,m,ans; String str ; Scanner cin = new Scanner (new BufferedInputStream(System.in)); while(cin.hasNext())![]() { b=cin.nextInt(); if(b==0) break; p=cin.nextBigInteger(b); m=cin.nextBigInteger(b); ans=p.mod(m); str=ans.toString(b); System.out.println(str); } } }![]() //End by abilitytao 例程: public class Main
|
|
Chapter I. |
Java進制轉換~集錦
由于Unicode兼容ASCII(0~255),因此,上面得到的Unicode就是ASCII。
java中進行二進制,八進制,十六進制,十進制間進行相互轉換
Integer.toHexString(int i)
十進制轉成十六進制
Integer.toOctalString(int i)
十進制轉成八進制
Integer.toBinaryString(int i)
十進制轉成二進制
Integer.valueOf("FFFF",16).toString()
十六進制轉成十進制
Integer.valueOf("876",8).toString()
八進制轉成十進制
Integer.valueOf("0101",2).toString()
二進制轉十進制
至于轉換成二進制或其他進制,Java API提供了方便函數,你可以查Java的API手冊。
以字符a的ASCII為例:
int i = 'a';
String iBin = Integer.toBinaryString(i);//二進制
String iHex = Integer.toHexString(i);//十六進制
String iOct = Integer.toOctalString(i);//八進制
String iWoKao = Integer.toString(i,3);//三進制或任何你想要的35進制以下的進制
DEC
有什么方法可以直接將2,8,16進制直接轉換為10進制的嗎?
java.lang.Integer類
parseInt(String s, int radix)
使用第二個參數指定的基數,將字符串參數解析為有符號的整數。
examples from jdk:
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787
進制轉換如何寫(二,八,十六)不用算法
Integer.toBinaryString
Integer.toOctalString
Integer.toHexString
例一:
public class Test{
public static void main(String args[]){
int i=100;
String binStr=Integer.toBinaryString(i);
String otcStr=Integer.toOctalString(i);
String hexStr=Integer.toHexString(i);
System.out.println(binStr);
例二:
public class TestStringFormat {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("usage: java TestStringFormat <a number>");
System.exit(0);
}
Integer factor = Integer.valueOf(args[0]);
String s;
s = String.format("%d", factor);
System.out.println(s);
s = String.format("%x", factor);
System.out.println(s);
s = String.format("%o", factor);
System.out.println(s);
}
}
各種數字類型轉換成字符串型:
String s = String.valueOf( value); // 其中 value 為任意一種數字類型。
字符串型轉換成各種數字類型:
String s = "169";
byte b = Byte.parseByte( s );
short t = Short.parseShort( s );
int i = Integer.parseInt( s );
long l = Long.parseLong( s );
Float f = Float.parseFloat( s );
Double d = Double.parseDouble( s );
數字類型與數字類對象之間的轉換:
byte b = 169;
Byte bo = new Byte( b );
b = bo.byteValue();
short t = 169;
Short to = new Short( t );
t = to.shortValue();
int i = 169;
b = bo.byteValue();
short t = 169;
Short to = new Short( t );
t = to.shortValue();
int i = 169;
Integer io = new Integer( i );
i = io.intValue();
long l = 169;
Long lo = new Long( l );
l = lo.longValue();
float f = 169f;
Float fo = new Float( f );
f = fo.floatValue();
double d = 169f;
Double dObj = new Double( d );
d = dObj.doubleValue();
posted on 2009-10-16 19:13 abilitytao 閱讀(1304) 評論(4) 編輯 收藏 引用







}
}