第2章 字符串和日期
2020-03-29 本文已影响0人
得力小泡泡
1、字符串中A的数量
算法分析
直接模拟
时间复杂度
Java 代码
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char[] charArray = scan.next().toCharArray();
int res = 0;
for(int i = 0;i < charArray.length;i ++)
if(charArray[i] == 'A')
res ++;
System.out.println(res);
}
}
2、最长的名字
算法分析
直接模拟
时间复杂度
Java 代码
import java.util.Scanner;
public class Main{
static int N = 110;
static String[] s = new String[N];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int res = 0;
for(int i = 0;i < n;i ++)
{
s[i] = scan.next();
res = Math.max(res, s[i].length());
}
for(int i = 0;i < n;i ++)
{
if(s[i].length() == res) System.out.println(s[i]);
}
}
}
3、字符串
算法分析
直接模拟
时间复杂度
Java 代码
import java.util.Scanner;
public class Main{
static int N = 110;
static String[] s = new String[N];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char[] charArray = scan.next().toCharArray();
String s = "";
for(int i = 0;i < charArray.length;i ++)
{
char c ;
if(charArray[i] - '0' >= 0 && charArray[i] - '0' <= 9) c = charArray[i];
else if(charArray[i] == 'z') c = 'a';
else if(charArray[i] == 'Z') c = 'A';
else c = (char)((int)(charArray[i]) + 1);
s += c;
}
System.out.println(s);
}
}
4、大数的奇偶性判断
算法分析
判断最后一位
Java 代码
import java.util.Scanner;
public class Main{
static int N = 110;
static String[] s = new String[N];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char[] charArray = scan.next().toCharArray();
int len = charArray.length;
if((charArray[len - 1] - '0') % 2 == 0) System.out.println("YES");
else System.out.println("NO");
}
}
5、字符反转
算法分析
直接翻转
Java代码
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char[] charArray = scan.next().toCharArray();
int len = charArray.length;
for(int i = len - 1;i >= 0;i --)
System.out.print(charArray[i]);
}
}