附:友情链接之 Tsinsen 第一卷 题解 1000-1024(已完结)
1025 字符串对比
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
sc.close();
int result = f(a, b);
System.out.println(result);
}
public static int f(String a, String b) {
if (a.length() != b.length()) {
return 1;
} else {
if (a.equals(b)) {
return 2;
} else if (a.equalsIgnoreCase(b)) {
return 3;
} else {
return 4;
}
}
}
}
1026 字符统计
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
sc.close();
int len = s.length();
char[] a = s.toCharArray();
int[] count = new int[26];
for (int i = 0; i < len; i++) {
count[a[i] - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (count[i] != 0) {
System.out.println((char) ('a' + i) + " " + count[i]);
}
}
}
}
1028 选择计算
import java.util.*;
public class Main {
public static int f1(int m,int n) {
if(m < n) {
int temp = m;
m = n;
n = temp;
}
if(m % n == 0) {
return n;
}else {
return f1(n, m % n);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
sc.close();
if(z==1) {
System.out.println(x+y);
}
if(z==2) {
System.out.println(x-y);
}
if(z==3) {
System.out.println(x * y);
}
if(z==4) {
System.out.println((int)(x/y));
}
if(z==5) {
System.out.println(x % y);
}
if(z==6) {
System.out.println(f1(x, y));
}
if(z==7) {
System.out.println(x*y/f1(x, y));
}
}
}
1029 补写函数 (本题不能用java提交)
#include<iostream>
using namespace std;
int maxmin_0(int a, int b, int c, int *max, int *min)
{
*max=a>b?(a>c?a:c):(b>c?b:c);
*min=a<b?(a<c?a:c):(b<c?b:c);
return 0;
}
int maxmin_1(int a, int b, int c, int &max, int &min)
{
max=a>b?(a>c?a:c):(b>c?b:c);
min=a<b?(a<c?a:c):(b<c?b:c);
return 0;
}
1031 画三角形1
public class Main {
public static void main(String args[]){
for(int i=1;i<=15;i++){
for(int j=1;j<=2*i-1;j++){
char result=(char)(Math.abs(i-j)+'A');
System.out.print(result);
}
System.out.println();
}
}
}
1032 画三角形2
import java.util.*;
public class Main {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
for(int i=1;i<=n;i++){
for(int j=1;j<=2*i-1;j++){
char result=(char)(Math.abs(i-j)+'A');
System.out.print(result);
}
System.out.println();
}
}
}
1033 绘制图形
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n + 1][n + 1];
a[0][0] = 1;
int count = 1;
int i = 0;
int j = 0;
while (count < n * n) {
while (j + 1 < n && a[i][j + 1] == 0) {
a[i][++j] = ++count;
}
while (i + 1 < n && a[i + 1][j] == 0) {
a[++i][j] = ++count;
}
while (j - 1 >= 0 && a[i][j - 1] == 0) {
a[i][--j] = ++count;
}
while (i - 1 >= 0 && a[i - 1][j] == 0) {
a[--i][j] = ++count;
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
本文精选了Tsinsen题库中的多个编程题目解答,包括字符串对比、字符统计、数学运算等功能实现,涵盖Java和C++语言,展示了不同类型的算法问题解决思路。
125

被折叠的 条评论
为什么被折叠?



