package day12;
import java.util.Scanner;
/*
输入一个正整数 repeat (0<repeat<10),做 repeat次下列运算:
输入两个正整数 m和 n(1<=m, 1<=n),然后输入该 m行 n列矩阵 a中的元素,分别求出各行元素之和,并存入一维数组 row中,再输出 row。
例:括号内是说明
输入:
1 (repeat=1)
3 2 (m=3, n=2)
6 3
1 -8
3 12
输出:
sum of row 0 is 9
sum of row 1 is -7
sum of row 2 is 15
*/
public class Test57 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入运算次数:");
int repeat = sc.nextInt();
for (int i = 1; i <= repeat; i++) {
System.out.println("请输入两个正整数作为矩阵的行和列:(例如:a b)");
int m = sc.nextInt();
int n = sc.nextInt();
int[][] array = new int[m][n];
System.out.println("请输入m行n列矩阵中的元素:");
for (int j = 0; j < m; j++) {
for (int k = 0; k < n; k++) {
array[j][k] = sc.nextInt();
}
}
int[] row = new int[m];
for (int j = 0; j < m; j++) {
for (int k = 0; k < n; k++) {
row[j] += array[j][k];
}
}
for (int j = 0; j < m; j++) {
System.out.println("sum of row " + j + " is " + row[j]);
}
}
}
}
输入一个正整数 n和 n阶方阵 a中的元素,如果找到 a的鞍点。
package day12;
import java.util.Scanner;
/*
输入一个正整数 repeat (0<repeat<10),做 repeat次下列运算:
输入一个正整数 n和 n阶方阵 a中的元素,如果找到 a的鞍点
(鞍点的元素值在该行上最大,在该列上最小),
就输出它的下标,否则,输出"NO"(设 a最多有1个鞍点)。
例:括号内是说明
输入:
2 (repeat=2)
4 1 7 4 1 4 8 3 6 1 6 1 2 0 7 8 9 (n=4)
2 1 7 4 1 (n=2)
输出:
a[2][1]=6
NO
*/
public class Test58 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入运算次数:");
int repeat = sc.nextInt();
for (int i = 1; i <= repeat; i++) {
System.out.println("请输入一个正整数n:");
int n = sc.nextInt();
int[][] a = new int[n][n];
System.out.println("请输入一个n阶方阵:");
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
a[j][k] = sc.nextInt();
}
}
boolean found = false;
for (int j = 0; j < n; j++) {
int maxInRow = a[j][0];
int maxIndex = 0;
for (int k = 1; k < n; k++) {
if (a[j][k] > maxInRow) {
maxInRow = a[j][k];
maxIndex = k;
}
}
boolean isSaddlePoint = true;
for (int jj = 0; jj < n; jj++) {
if (a[jj][maxIndex] < maxInRow) {
isSaddlePoint = false;
break;
}
}
if (isSaddlePoint) {
found = true;
System.out.println("a[" + j + "][" + maxIndex + "]=" + maxInRow);
break;
}
}
if (!found) {
System.out.println("NO");
}
}
}
}
连续输入一批以 # 结束的多串字符。统计并输出每串字符的长度。
package day12;
import java.util.Scanner;
/*
连续输入一批以 # 结束的多串字符。统计并输出每串字符的长度。
输出使用:System.out.println(len);
例:括号内是说明
输入:
hello 12#abc+0# (连续输入2串字符“hello 12”和“abc+0”)
输出:
8 (“hello 12”的长度是8)
5 (“abc+0”的长度是5)
*/
public class Test59 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一批以 # 结束的多串字符:");
String s = sc.nextLine();
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c != '#')
count++;
else {
System.out.println(count);
count = 0;
}
}
}
}
输入一行字符串,统计并输出字符串中大写辅音字母的个数。
package day12;
import java.util.Scanner;
/*
输入一个正整数n (0<n<10),做 n次下列运算:
输入一行字符串,统计并输出字符串中大写辅音字母的个数
(大写辅音字母:除“A”,“E”,“I”,“O”,“U”以外的大写字母)。
例:括号内是说明
输入:
3 (输入3个字符串)
HELLO
IT12
uieiaba
输出:
3 ("HELLO"中有3个大写辅音字母)
1 ("IT12"中有1个大写辅音字母)
0 ("uieiaba"中没有大写辅音字母)
*/
public class Test60 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入运算次数:");
int n = sc.nextInt();
sc.nextLine();
for (int i = 1; i <= n; i++) {
System.out.println("请输入一行字符串:");
String s = sc.nextLine();
int count = 0;
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
if (c >= 'A' && c <= 'Z' && c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U')
count++;
}
System.out.println(count);
}
}
}