大家都说题简单,可能公司在刷kpi,或者全员面试…
1.求矩阵两个数乘积的最大值
要求:两个数不在同行同列
裂开…4个for循环,看大家好像都是这样a的
import java.util.Scanner;
/**
* @author liu
* @Description
*/
public class Main1 {
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
int n = ss.nextInt();
int m = ss.nextInt();
int[][] a = new int[n][m];
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
a[i][j] = ss.nextInt();
}
}
int max = -1;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
//求局部最大值
int temp = -1;
for(int x = 0;x<n;x++){
for(int y = 0;y<m;y++){
if(x!=i&&y!=j){
temp = Math.max(temp , a[x][y]);
}
}
}
max = Math.max(max,a[i][j] * temp);
}
}
System.out.print(max);
}
}
2.数组排序
就简单的数组排序
/**
* @author liu
* @Description
*/
public class Main2 {
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
int n = ss.nextInt();
int[] a = new int[n];
for(int i = 0;i<n;i++){
a[i] = ss.nextInt();
}
Arrays.sort(a);
for (int num: a){
System.out.print(num + " ");
}
}
}
3.二进制数中1的个数
求二进制数中1的个数
/**
* @author liu
* @Description
*/
public class Main3 {
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
int n = ss.nextInt();
int count = 0;
while (n!=0){
if(n%2!=0){
count++;
}
n = n/2;
}
System.out.println(count);
}
}
4.字符串向前移动n位
一直是80%,好像说是还要考虑字符串里面有空格的情况…
看到大佬们的代码好像是移动n要对字符串长度m取模,我是当n>m时,输出原字符串
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String m = scanner.nextLine();
int n = scanner.nextInt();
n = n % m.length();
System.out.println(m.substring(n,m.length()) + m.substring(0,n));
}
/**
* @author liu
* @Description
*/
public class Main4 {
public static void main(String[] args) {
Scanner ss1 = new Scanner(System.in);
String str = ss1.nextLine();
long n = ss1.nextLong();
if(str.length() == 0) return;
if((int)n >= str.length()){
System.out.print(str);
return;
}
for(int i = (int)n;i<str.length();i++){
System.out.print(str.charAt((int)i));
}
for(int i = 0;i<n;i++){
System.out.print(str.charAt((int)i));
}
// String newStr = str + str + "";
// char[] s = newStr.toCharArray();
// for (int i = (int)n;i<str.length()+(int)n;i++){
// System.out.print(s[i]);
// }
// char[] s = str.toCharArray();
// char[] b = new char[(int)n];
// char[] c = new char[(int)n];
//
// for(int i = 0;i<(int)n;i++){
// b[i] = s[i];
// }
// for(int i = (int)n;i<s.length;i++){
// c[i-(int)n] = s[i];
// }
//
// str = String.valueOf(c) + String.valueOf(b) + "";
// System.out.print(str);
}
}