此题虽然简单,但是需啊哟注意的是,每个“*”后边有一个空格】
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner mScanner = new Scanner(System.in); 6 int n = mScanner.nextInt(); 7 for (int i = n; i > 0; i--) { 8 if (i != n) { 9 System.out.println(); 10 } 11 for (int j = 0; j < i; j++) { 12 System.out.print("* "); 13 } 14 } 15 } 16 }
二、排序
1 import java.util.Arrays; 2 import java.util.Scanner; 3 4 public class Main{ 5 6 public static void main(String[] args) { 7 Scanner mScanner = new Scanner(System.in); 8 int[] array = new int[3]; 9 for (int i = 0; i < array.length; i++) { 10 array[i] = mScanner.nextInt(); 11 } 12 Arrays.sort(array); 13 for (int i = array.length - 1; i >= 0; i--) { 14 System.out.print(array[i] + " "); 15 } 16 } 17 18 }
1 import java.util.Scanner; 2 3 public class Main{ 4 public void fun(double e) { 5 double d = e; 6 if (d == 1) { 7 System.out.print("2(0)"); 8 } else { 9 if (d == 2) { 10 System.out.print("2"); 11 } else { 12 int x = 1; 13 int m = 0; 14 while (m == 0) { 15 if ((Math.pow(2, x + 1) > d) && (d >= Math.pow(2, x))) { 16 m = 1; 17 if (d == Math.pow(2, x)) { 18 System.out.print("2"); 19 if (x == 1) { 20 } else { 21 System.out.print("("); 22 fun(x); 23 System.out.print(")"); 24 } 25 } else { 26 System.out.print("2"); 27 if (x == 1) { 28 } else { 29 System.out.print("("); 30 fun(x); 31 System.out.print(")"); 32 } 33 System.out.print("+"); 34 fun(d - Math.pow(2, x)); 35 } 36 } else { 37 x++; 38 } 39 } 40 } 41 } 42 } 43 44 public static void main(String[] args) { 45 Scanner mScanner = new Scanner(System.in); 46 double n = mScanner.nextInt(); 47 Main cm = new Main(); 48 cm.fun(n); 49 } 50 }
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner mScanner = new Scanner(System.in); 7 String mm = mScanner.next(); 8 int a = mScanner.nextInt(); 9 int b = mScanner.nextInt(); 10 if (mm.equals("+")) { 11 System.out.println(a+b); 12 }else if (mm.equals("-")) { 13 System.out.println(a-b); 14 }else if (mm.equals("*")) { 15 System.out.println(a*b); 16 }else if (mm.equals("/")) { 17 System.out.println(a/b); 18 } 19 } 20 }
1 import java.util.Arrays; 2 import java.util.Scanner; 3 4 public class Main{ 5 6 public static void main(String[] args) { 7 Scanner mScanner = new Scanner(System.in); 8 String str1 = mScanner.next().toLowerCase(); 9 String str2 = mScanner.next().toLowerCase(); 10 if (str1.length() == str2.length()) { 11 char[] arr1 = fun(str1); 12 char[] arr2 = fun(str2); 13 boolean tag = true; 14 for (int i = 0; i < arr1.length; i++) { 15 if (arr1[i] != arr2[i]) { 16 tag = false; 17 } 18 } 19 if (tag) { 20 System.out.println("Y"); 21 }else{ 22 System.out.println("N"); 23 } 24 } else { 25 System.out.println("N"); 26 } 27 } 28 29 public static char[] fun(String str) { 30 char[] array = str.toCharArray(); 31 Arrays.sort(array); 32 return array; 33 } 34 35 }
1 import java.util.Scanner; 2 3 public class Main{ 4 5 public static void main(String[] args) { 6 Scanner mScanner = new Scanner(System.in); 7 int m = mScanner.nextInt(); 8 if (m < 1 || m > 20) { 9 return; 10 } 11 int[] array = new int[m]; 12 for (int i = 0; i < m; i++) { 13 array[i] = mScanner.nextInt(); 14 } 15 16 int max = 0; 17 int tag = 0; 18 int num = 0; 19 for (int i = 1; i < array.length; i++) { 20 if (array[i - 1] == array[i]) { 21 tag++; 22 } else { 23 tag = 0; 24 } 25 if (tag > max) { 26 num = array[i]; 27 max = tag; 28 } 29 } 30 if (num==0) { 31 num = array[0]; 32 } 33 System.out.println(num); 34 } 35 36 }
1 import java.util.Scanner; 2 3 4 public class Main { 5 6 public static void main(String[] args) { 7 Scanner mScanner = new Scanner(System.in); 8 int m = mScanner.nextInt(); 9 int s = mScanner.nextInt(); 10 int n = mScanner.nextInt(); 11 int[][] a = new int[m][s]; 12 int[][] b = new int[s][n]; 13 int[][] c = new int[m][n]; 14 for (int i = 0; i < m; i++) { 15 for (int j = 0; j < s; j++) { 16 a[i][j] = mScanner.nextInt(); 17 } 18 } 19 for (int i = 0; i < s; i++) { 20 for (int j = 0; j < n; j++) { 21 b[i][j] = mScanner.nextInt(); 22 } 23 } 24 25 getMatrix(a, b); 26 27 } 28 29 public static void getMatrix(int[][] a, int[][] b) { 30 int[][] c = new int[a.length][b[0].length]; 31 for (int i = 0; i < a.length; i++) { 32 for (int j = 0; j < b[0].length; j++) { 33 int sum = 0; 34 for (int k = 0; k < b.length; k++) { 35 sum += a[i][k] * b[k][j]; 36 } 37 c[i][j] = sum; 38 } 39 } 40 41 for (int i = 0; i < a.length; i++) { 42 for (int j = 0; j < b[0].length; j++) { 43 System.out.print(c[i][j] + "\t"); 44 } 45 System.out.println(); 46 } 47 48 } 49 50 }
1 import java.util.Scanner; 2 public class Main { 3 4 public static void main(String[] args) { 5 Scanner mScanner = new Scanner(System.in); 6 String str = mScanner.next(); 7 char[] arr = str.toCharArray(); 8 for (int i = 0; i < arr.length; i++) { 9 if (Character.isLowerCase(arr[i])) { 10 str = String.valueOf(arr[i]).toUpperCase(); 11 } else { 12 str = String.valueOf(arr[i]).toLowerCase(); 13 } 14 System.out.print(str); 15 } 16 } 17 18 }
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static void main(String[] args) { 7 Scanner mScanner = new Scanner(System.in); 8 int length = mScanner.nextInt(); 9 10 int sum = 0; 11 int average = 0; 12 for (int i = 0; i < length; i++) { 13 sum = sum + mScanner.nextInt(); 14 } 15 System.out.println(sum + " " + sum / length); 16 17 } 18 19 }
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Scanner; 4 5 6 public class Main { 7 8 public static void main(String[] args) { 9 10 Scanner mScanner = new Scanner(System.in); 11 int count = mScanner.nextInt(); 12 13 List<Integer> list = new ArrayList<Integer>(); 14 15 // int[] arr = new int[count]; 16 for (int i = 0; i < count; i++) { 17 // arr[i] = mScanner.nextInt(); 18 list.add(mScanner.nextInt()); 19 } 20 int length = CompactIntegers(list, count); 21 System.out.println(length); 22 23 for (int i = 0; i < list.size(); i++) { 24 System.out.print(list.get(i) + " "); 25 } 26 27 } 28 29 public static int CompactIntegers(List<Integer> list, int count) { 30 int ss = count; 31 for (int i = 0; i < list.size(); i++) { 32 if (list.get(i) == 0) { 33 list.remove(i); 34 ss--; 35 i--; 36 } 37 } 38 return ss; 39 } 40 }
注:本文系原创,首发于博客园,转载请注明出处。[http://www.cnblogs.com/zhjsll/p/4412250.html]