一、最小乘积(基本型)【这个题需要认真阅读试题,内容量较大,刚开始的时候,由于练习系统上给出的输入输出的格式有问题,没看懂,最后在MikCu的博客上看到了正确的格式,参考了代码,最终得到正确的结果。为了让结果最小,可以先分别对两组数进行排序,然后对其中的一组数据逆序,逆序后,把两组数据最大的与最小的相乘,最后求得的和最小!】
1 import java.util.Arrays; 2 import java.util.Scanner; 3 4 public class Main { 5 static Scanner mScanner; 6 7 public static void main(String[] args) { 8 9 mScanner = new Scanner(System.in); 10 int n = mScanner.nextInt(); 11 12 int[] sum = new int[n]; 13 for (int i = 0; i < n; i++) { 14 sum[i] = getMaxSum(); 15 } 16 for (int i = 0; i < n; i++) { 17 System.out.println(sum[i]); 18 } 19 20 } 21 22 public static int getMaxSum() { 23 int count = mScanner.nextInt(); 24 int[] array1 = new int[count]; 25 int[] array2 = new int[count]; 26 for (int i = 0; i < count; i++) { 27 array1[i] = mScanner.nextInt(); 28 } 29 for (int j = 0; j < count; j++) { 30 array2[j] = mScanner.nextInt(); 31 } 32 Arrays.sort(array1); 33 Arrays.sort(array2); 34 array2 = getReverse(array2); 35 36 int sum = 0; 37 for (int i = 0; i < count; i++) { 38 sum = array1[i] * array2[i] + sum; 39 } 40 41 return sum; 42 } 43 44 public static int[] getReverse(int[] arr) { 45 int length = arr.length; 46 int[] array = new int[length]; 47 for (int i = 0; i < arr.length; i++) { 48 array[i] = arr[length - 1]; 49 length--; 50 } 51 52 return array; 53 } 54 55 }
二、Torry的困惑(基本型)【此题的关键是保存素数的积对50000取余的结果,如果保留乘积的话,会造成数据过大,内存溢出等问题……】
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 Scanner mScanner = new Scanner(System.in); 8 int n = mScanner.nextInt(); 9 int i = 0; 10 int start = 2; 11 int ss = 1; 12 while(i<n){ 13 if (isSushu(start)) { 14 ss = (ss*start)%50000; 15 i++; 16 } 17 start++; 18 } 19 System.out.println(ss); 20 } 21 22 public static boolean isSushu(int m){ 23 for (int i = 2; i*i <= m; i++) { 24 if (m%i==0) { 25 return false; 26 } 27 } 28 return true; 29 30 } 31 32 }
三、寻找数组中最大值【此题求最大值不能使用sort()函数了,因为要输出其下标,所以此处使用的是循环,不过我刚刚想到了一个方法,就是先使用sort函数对原数组的备份进行排序,然后得到最大值,在原数组上使用getIndex()方法得到其索引,此方法应该可行。】
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 Scanner mScanner = new Scanner(System.in); 8 int n = mScanner.nextInt(); 9 int[] arrays = new int[n]; 10 for (int i = 0; i < n; i++) { 11 arrays[i] = mScanner.nextInt(); 12 } 13 14 int max = Integer.MIN_VALUE; 15 int maxIndex =0; 16 for (int i = 0; i < arrays.length; i++) { 17 if (arrays[i] > max) { 18 max = arrays[i]; 19 maxIndex = i; 20 } 21 } 22 23 System.out.println(max+" "+maxIndex); 24 25 } 26 27 }
四、关联矩阵【】
1 import java.util.Scanner; 2 3 public class Main{ 4 5 public static void main(String[] args) { 6 7 Scanner mScanner = new Scanner(System.in); 8 int n = mScanner.nextInt();//dian 9 int m = mScanner.nextInt();//bian 10 int[][] array = new int[n][m]; 11 int x,y; 12 for (int i = 0; i < m; i++) { 13 x = mScanner.nextInt(); 14 y = mScanner.nextInt(); 15 array[x-1][i] =1; 16 array[y-1][i] =-1; 17 } 18 for (int i = 0; i < n; i++) { 19 for (int j = 0; j < m; j++) { 20 System.out.print(array[i][j]+" "); 21 } 22 System.out.println(); 23 } 24 25 } 26 27 }
注:本文系原创,首发于博客园,转载请注明出处。
原文:http://www.cnblogs.com/zhjsll/p/4412547.html