进制转换
package ch2; import java.util.Scanner; public class CG0311_2 { public static void main(String [] args) { Scanner sc = new Scanner(System.in); long res=0 ,base = 1; String str; str = sc.next(); int len = str.length(); for(int i = len-1;i>=0;i--) { if( str.charAt(i)=='1' ) { res += base; } base*=2; } System.out.println(res); sc.close(); } }
double
package ch2; import java.util.Scanner; public class CG031101 { public static void main( String [] args ) { Scanner in = new Scanner(System.in); long a,b; a = in.nextInt(); b = in.nextInt(); System.out.printf("128-bit hexadecimal number: %016x%016x ",a,b); a = a<<1; if( b<=-1 ) a=a+1; b=b<<1; System.out.printf("After doubling: %016x%016x ",a,b); } }
找点:在本列和本行都是最大。
package CG; import java.util.Scanner; public class array_1 { public static void main(String [] args) { Scanner sc=new Scanner(System.in); int arr[][] = new int[12][12]; int m,n; n = sc.nextInt(); m = sc.nextInt(); for( int i = 1;i<=n ;i++ ) { for(int j = 1;j<=m;j++) { arr[i][j] = sc.nextInt(); } } for( int i = 1;i<=n ;i++ ) { for(int j = 1;j<=m;j++) { int flag = 1; for(int jj = 1;jj<=m;jj++) if( arr[i][jj]>arr[i][j] ) flag = 0; for(int ii = 1;ii<=n;ii++) if( arr[ii][j]>arr[i][j] ) flag = 0; if(flag==1) { System.out.println( arr[i][j]+" "+i+" "+j); } }System.out.println(); } } }
二分查找
package CG; import java.util.Scanner; public class array_2 { public static int find(int []arr,int l,int r,int res){ if( l == r ) { if( arr[l]==res ) return l; else return 0; } int mid = (l+r)/2; if( arr[mid] == res ) return mid; else if( arr[mid]<res ) return find(arr,mid+1,r,res); return find(arr,l,mid-1,res); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int []arr=new int[n+5]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } int res=sc.nextInt(); System.out.println(find(arr,0,n-1,res)); } }
表的“异或”
package CG; import java.util.*; public class array_3 { public static void main(String[] args) { HashSet<Integer> Set1 = new HashSet<>(); HashSet<Integer> Set2 = new HashSet<>(); int arr[] = new int[45]; int n,m; Scanner sc = new Scanner( System.in ); n = sc.nextInt(); for(int i = 1; i<= n; i++) { arr[i] = sc.nextInt(); Set1.add(arr[i]); } m = sc.nextInt(); for(int i = n+1; i<= m+n; i++) { arr[i] = sc.nextInt(); if( Set1.contains(arr[i]) ) Set2.add(arr[i]); } Arrays.sort(arr,1,m+n+1); for(int i = 1;i <= m+n ; i++) { if( !Set2.contains(arr[i]) ) System.out.print( arr[i]+" " ); } } }