字符串匹配
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
while(scan.hasNextLine()){
String line1 = scan.nextLine(); // 题目已规定第1个是短的
String line2 = scan.nextLine(); // 题目已规定第2个是短的
allshow(line1,line2);
}
}
public static void allshow(String strShort,String strLong){
int count = 0;
for(int i = 0;i<strShort.length();i++){
if(strLong.contains(String.valueOf(strShort.charAt(i)))){
count++; // 长的包含短的就计数
}
}
if(count == strShort.length()){ // count 等于 短的长度,就是全部包含
System.out.println("true");
}else{
System.out.println("false");
}
}
}
2 测试
fhsi
fdsljfls
false
统计大写字母个数
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
while(scan.hasNextLine()){
String line = scan.nextLine();
System.out.println(CalcCapital(line));
}
}
public static int CalcCapital(String line){
int num =0 ;
for(int i=0;i<line.length();i++){
char ch = line.charAt(i);
if(ch>='A'&&ch<='Z'){ // 大写字母统计
num++;
}
}
return num;
}
}
2 测试
EaPAaNfzmnCfnEzSUAFKfXDfbWlYorJHkAmSbZltSetyWSLGfkWdzZjCmKIvFcRIRvuxpuASNdCfrXCQCOlmoaVPzvDegpSxAywfLlzZWkpPCFXUWwkugIMIopzKfdvFianTqwjeGSSfBqSMXnuADdBSXDQjVRAdVaiDOOkCoUXAqeCVomEjZieNvsVGgRQxH
98
求最大连续bit数
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan=new Scanner(System.in);
while(scan.hasNextLine()){
int max = 0;
int temp = 0;
String num = Integer.toBinaryString(Integer.parseInt(scan.nextLine()));
char[] ch = num.toCharArray();
for(int i=0;i<ch.length;i++){
if(ch[i] == '1'){
temp ++; // 记录连续为1的个数
max = Math.max(max,temp);
}else{
temp = 0;
}
}
System.out.println(max);
}
}
}
2 测试
200
2
合法IP
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan=new Scanner(System.in);
while(scan.hasNextLine()){
String[] str=scan.nextLine().split("\\.");
int count = 0; // 对点分十进制进行计数
try{
for(int i=0;i<4;i++){
int num = Integer.parseInt(str[i]);
if((num>=0)&&(num<=255)){
count++;
}
}
}catch(Exception e){
System.out.println("NO");
return;
}
if(count ==4){
System.out.println("YES"); // 只有count =4 才说明它是合法IP
}else{
System.out.println("NO");
}
}
}
}
2 测试
10.10.10.10
YES
在字符串中找出连续最长的数字串
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str;
while(scan.hasNextLine()){
str=scan.nextLine();
int max = 0;
String res = "";
int temp = 0;
int cnt = 0;
String tstr = "";
for(int i=0; i<str.length(); i++){
char c = str.charAt(i); //从0开始
if(c >= '0' && c <= '9'){
if(temp == 0){
temp = 1;
}
tstr += c;
cnt++;
continue; //直接continue
}
else{ //遇到第一个非数字字符重置相关的中间变量
if(cnt >= max){
if(cnt > max){ //大于的情况
max = cnt;
res = tstr;
}
else{ // 相等的情况
res += tstr;
}
}
temp = 0;
cnt = 0;
tstr = "";
}
}
if(temp == 1){
if(cnt >= max){
if(cnt > max){
max = cnt;
res = tstr;
}
else{
res += tstr;
}
}
}
System.out.println(res + "," + max);
}
}
}
2 测试
abcd12345ed125ss123058789
123058789,9
记票统计
1 程序
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()){
// 被投票人
int n = Integer.parseInt(scan.nextLine());
String[] nstr = scan.nextLine().split(" ");
int[] count = new int[n];
int valid = 0;
// 投票人
int m = Integer.parseInt(scan.nextLine());
String[] mstr = scan.nextLine().split(" ");
// 外层:遍历投票人 内层:遍历被投票人
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(mstr[i].equals(nstr[j])){
count[j]++; // 被投票人被投中
valid++; // 有效票
break;
}
}
}
for(int j=0;j<n;j++){
System.out.println(nstr[j]+" : "+count[j]);
}
System.out.println("Invalid : "+(m-valid));
}
}
}
2 测试
4
A B C D
8
A B C D E F G H
A : 1
B : 1
C : 1
D : 1
Invalid : 4
表示数字
1 程序
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()){
String str = scan.nextLine();
StringBuffer sb = new StringBuffer(); // 输出串
StringBuffer sb1 = new StringBuffer(); // 代表临时要加的串
boolean isdigit = false;
char[] ch = str.toCharArray();
// 遍历字符数组
for(int i=0;i<ch.length;i++){
// 处理数字的情况
if((ch[i]>='0')&&(ch[i]<='9')){
isdigit = true;
sb1.append(ch[i]);
if(((i+1)<ch.length)&&(ch[i+1]>='0')&&(ch[i+1]<='9')){
continue;
}
}else{ // 处理非数字的情况
isdigit = false;
sb1.append(ch[i]);
}
if(true == isdigit){ // 将数字加到临时串中
sb.append("*");
sb.append(sb1.toString());
sb.append("*");
}else{
sb.append(sb1.toString()); // 将非数字加到临时串中
}
// 当加完数字或加完非数字,要清空临时串
sb1.setLength(0);
}
System.out.println(sb.toString());
}
}
}
2 测试
Jkdi234klowe90a3
Jkdi*234*klowe*90*a*3*
记负均正
1 程序
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()){
int num = Integer.parseInt(scan.nextLine());
int num1[] = new int[num];
String[] strs = scan.nextLine().split(" ");
// 记录负数
int count = 0;
// 记录正整数
int count1 = 0;
int avreage ;
float sum = 0;
for(int i= 0;i<num;i++){
int temp = Integer.parseInt(strs[i]);
// 统计负数
if(temp<0){
count++;
}
// 计算非负数
if(temp>0){
count1++;
sum = sum + temp;
}
}
System.out.printf("%d %.1f\n",count,sum/count1);
}
}
}
2 测试
5
1
2
3
4
5
0 3.0
自守数
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
while(scan.hasNextLine()){
int n =Integer.parseInt(scan.nextLine());
int count = 0;
int m = 0;
String strM;
String strI;
// 遍历0到n之间的自然数
for(int i=0;i<=n;i++){
m = i*i;
strM = String.valueOf(m); // 该自然数平方对应的字符串strM
strI = String.valueOf(i); // 该自然数对应的字符串strI
// strM的尾巴是否包含字符串strI,如果包含就说明该自然数是自守数,进行计数
if(strM.substring(strM.length()-strI.length()).equals(strI)){
count++;
}
}
System.out.println(count);
}
}
}
2 测试
2000
8
等差数列
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan=new Scanner(System.in);
while(scan.hasNextLine()){
int n =Integer.parseInt(scan.nextLine());
// 针对2,5,8,11,14,它的计算公式是 (1+3*n)*n/2
System.out.println((1+3*n)*n/2);
}
}
}
2 测试
7
77
输入整型数组和排序标识,对其元素按照升序或降序进行排序
1 程序
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan=new Scanner(System.in);
while(scan.hasNextLine()){
String strCount = scan.nextLine();
// count个数进行排序
int count = Integer.parseInt(strCount);
// 输入count个字符串
String[] num = scan.nextLine().split(" ");
StringBuffer sb=new StringBuffer();
int[] inum= new int[count];
// 将字符串转为数字
for(int i = 0;i<count;i++){
inum[i] = Integer.parseInt(num[i]);
}
// 0 是升序 1 是降序
int sorttype = Integer.parseInt(scan.nextLine());
Arrays.sort(inum); // 默认是升序
if(0 == sorttype){ // 升序排序
int i=0;
for(i = 0;i<count-1;i++){
sb.append(inum[i]+" ");
}
sb.append(inum[count-1]);
}else if(1 == sorttype) // 降序排序
{
int i=0;
for(i = count -1;i>0;i--){
sb.append(inum[i]+" ");
}
sb.append(inum[0]);
}
System.out.println(sb.toString());
}
}
}
2 测试
8
1 2 4 9 3 55 64 25
1
64 55 25 9 4 3 2 1
字符统计
1 程序
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan=new Scanner(System.in);
String line=null;
while(scan.hasNextLine()){
line = scan.nextLine();
System.out.println(fun(line));
}
}
private static String fun(String str) {
char[] chs=str.toCharArray(); // 转数组
int[] num=new int[200]; //必须大于128
for (char c : chs) { // 统计每个字符的个数,保存到num中
int i=(int)c;
num[i]++;
}
int max=0;
// 找到统计最大字符
for(int i=0;i<num.length;i++){
if(max<num[i]){
max=num[i];
}
}
StringBuffer sb=new StringBuffer();
while(max!=0){
// 遍历统计数组,先看有没有max个的字符,再看有没有max-1个的字符,依次找到各个字符
for(int i=0;i<num.length;i++){
if(max==num[i]){
sb.append((char)i);
}
}
max--;
}
return sb.toString();
}
}
2 测试
aadddccddceeff
dcaef
字符串分割
1 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
// 要输入字符串的个数
int count = Integer.parseInt(in.nextLine());
for (int i = 0; i < count; i++) {
// 读入一个字符串
String input = in.nextLine();
// 字符串不能为空
if (input == null || input.length() == 0) {
continue;
}
// 去掉字符串两边的空格
StringBuilder sb = new StringBuilder(input.trim());
// 当字符串中字符的个数不是8个倍数,需要加零的个数
if (sb.length() % 8 != 0) {
int more = 8 - sb.length() % 8;
for (int j = 0; j < more; j++) {
sb.append("0");
}
}
// 每一行输出8个字符
for (int j = 0; j < sb.length(); j += 8) {
System.out.println(sb.substring(j, j + 8));
}
}
}
}
}
2 测试
2
abc
abc00000
123456789
12345678
90000000
记负均正II
1 程序
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String[] str = scan.nextLine().split(" ");
// 记录负数
int count = 0;
// 记录非负数
int count1 = 0;
int avreage ;
float sum = 0;
for(int i= 0;i<str.length;i++){
int num = Integer.parseInt(str[i]);
// 统计负数
if(num<0){
count++;
}
// 计算非负数
if(num>=0){
count1++;
sum = sum + num;
}
}
System.out.println(count);
// 小数保留一位
System.out.format("%.1f",sum/count1);
}
}
2 测试
-13 45 65 34
1
48.0
字符逆序
1 程序
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
// 将字符串转换为字符数组
char[] ch = str.toCharArray();
// 将数组逆序输出
for(int i=ch.length-1;i>=0;i--){
System.out.print(ch[i]);
}
}
}
2 测试
I am a student
tneduts a ma I
求最小公倍数
1 程序
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int num1 = scan.nextInt();
int num2 = scan.nextInt();
// 先求得两个数中大的一个数
int temp = Math.max(num1,num2);
// 然后依次递增该数,如果该数可以同时整除这两个数,则说明该数是最小公倍数
do{
if((temp%num1==0)&&(temp%num2==0)){
System.out.println(temp);
break;
}
temp++;
}
while(true);
}
}
2 测试
5 7
35