java水题集
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 5 public class Main { 6 public static int GetNum(char c) 7 { 8 if (Character.isDigit(c))return c-'0'; 9 if (Character.isUpperCase(c))return c-'A'+10; 10 if (Character.isLowerCase(c))return c-'a'+36; 11 return '0'; 12 } 13 public static char GetChar(int x) 14 { 15 if (x<10)return (char)(x+'0'); 16 else if (x<=35) return (char)(x+'A'-10); 17 else return (char)(x+'a'-36); 18 } 19 public static void main(String[] args) 20 { 21 Scanner cin= new Scanner(new BufferedInputStream(System.in)); 22 int T=cin.nextInt(); 23 while(T--!=0) 24 { 25 BigInteger base1=cin.nextBigInteger(); 26 BigInteger base2=cin.nextBigInteger(); 27 BigInteger n=BigInteger.ZERO; 28 String s=cin.next(); 29 for(int i=0;i<s.length();i++) 30 { 31 char c=s.charAt(i); 32 n=n.multiply(base1).add(BigInteger.valueOf(GetNum(c))); 33 } 34 String ans=new String(); 35 while(!n.equals(BigInteger.ZERO)) 36 { 37 ans=GetChar(n.mod(base2).intValue())+ans; 38 n=n.divide(base2); 39 } 40 if(ans.length()==0) ans+='0'; 41 System.out.println(base1+" "+s); 42 System.out.println(base2+" "+ans); 43 if (T>0)System.out.println(); 44 } 45 46 } 47 48 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 public class Main { 5 6 public static void main(String[] args) 7 { 8 Scanner cin=new Scanner(new BufferedInputStream(System.in)); 9 BigDecimal b= new BigDecimal("8"),k,ans; 10 String s; 11 while(cin.hasNext()) 12 { 13 ans=new BigDecimal(0); 14 k=new BigDecimal(1); 15 s=cin.nextLine(); 16 for(int i=2;i<s.length();i++) 17 { 18 k=k.divide(b); 19 ans=ans.add( BigDecimal.valueOf(s.charAt(i)-'0').multiply(k)); 20 } 21 System.out.println(s+" [8] = "+ans.toString()+" [10]"); 22 //System.out.println(s+" [8] = "+ans.toString()+" [10]"); 23 } 24 } 25 26 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 public class Main { 5 6 public static void main(String[] args) 7 { 8 Scanner cin=new Scanner(new BufferedInputStream(System.in)); 9 BigInteger f[]=new BigInteger[1005],Three=BigInteger.valueOf(3); 10 BigInteger tp[][]=new BigInteger[3][3],a[][]=new BigInteger[3][3]; 11 a[1][1]=Three; a[1][2]=BigInteger.valueOf(-1); 12 a[2][1]=BigInteger.ONE; a[2][2]=BigInteger.ZERO; 13 f[0]=BigInteger.ZERO; 14 f[1]=BigInteger.ONE; 15 for(int i=2;i<=1000;i++) 16 f[i]=f[i-1].multiply(Three).subtract(f[i-2]); 17 while(cin.hasNext()) 18 { 19 int n=cin.nextInt(); 20 tp=MatrixPower(a,2,n-1); 21 System.out.println(tp[1][1].toString()); 22 } 23 } 24 public static BigInteger[][] MatrixMultiply(BigInteger a[][],BigInteger b[][],int n,int p,int m) 25 { 26 BigInteger c[][]=new BigInteger[n+1][m+1]; 27 for(int i=1;i<=n;i++) 28 for(int j=1;j<=m;j++) 29 c[i][j]=BigInteger.ZERO; 30 for(int i=1;i<=n;i++) 31 for(int j=1;j<=m;j++) 32 for(int k=1;k<=p;k++) 33 c[i][j]=c[i][j].add(a[i][k].multiply(b[k][j])); 34 return c; 35 } 36 public static BigInteger[][] MatrixPower(BigInteger a[][],int n,int p) 37 { 38 BigInteger ans[][]=new BigInteger[n+1][n+1]; 39 for(int i=1;i<=n;i++) 40 for(int j=1;j<=n;j++) 41 if (i==j)ans[i][j]=BigInteger.ONE; 42 else ans[i][j]=BigInteger.ZERO; 43 while(p>0) 44 { 45 if ((p&1)==1)ans=MatrixMultiply(ans,a,n,n,n); 46 p=p/2; 47 a=MatrixMultiply(a,a,n,n,n); 48 } 49 return ans; 50 } 51 }