输入两个数作为区间(Low<=两数<high),其中是质数的所有数,十位、个位分别相加,输出较小值
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
while(input.hasNext()) {
int low=input.nextInt();
int high=input.nextInt();
System.out.println(result(low,high));
}
}
//判断是否为质数
private static boolean isPrime(int i) {
if(i==2||i==3) {
return true;
}
for(int j=2;j<=(int)Math.sqrt(i);j++) {
if(i%j==0) {
return false;
}
}
return true;
}
//找到质数十位数和与个位数和的最小数
private static int result(int low,int high) {
int num1=0;//记录个位数之和
int num2=0;//记录十位数之和
for(int i=low;i<high;i++) {
if(isPrime(i)) {
num1+=i%10;
if(i<10) {
continue;
}
num2+=(i/10)%10;
}
}
return Math.min(num1, num2);
}
}
转义报文,见A转义为12 34,见B转义为AB CD,输出每部分用空格隔开
import java.util.ArrayList;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> al=new ArrayList<String>();
Scanner input=new Scanner(System.in);
while(input.hasNext()) {
int len=input.nextInt();
for(int i=0;i<len-1;i++) {
String str=input.next();//返回每一个完整标记,与nextInt(),nextLine()区分
if(str.equals("A")) {
al.add("12");
al.add("34");
}else if(str.equals("B")) {
al.add("AB");
al.add("CD");
}else {
al.add(str);
}
}
int len1=al.size();
al.add(0,String.valueOf(len1+1));//int转String用String.valueOf();或""
for(int i=0;i<al.size();i++) {//String转int用Integer.parseInt()
if(i==0) {
System.out.print(al.get(i));
}else {
System.out.print(" "+al.get(i));
}
}
}
}
}
//方法2
public class Main3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
while(input.hasNext()) {
String s=input.nextLine();
System.out.print(solution1(s));
}
}
public static String solution1(String s) {
StringBuilder sb = new StringBuilder();
String[] strs = s.split(" ");
int len = strs.length;
for (int i = 1; i < strs.length; i++) {
if ("A".equals(strs[i])) {
sb.append("12 34");
len++;
} else if ("B".equals(strs[i])) {
sb.append("AB CD");
len++;
} else {
sb.append(strs[i]);
}
sb.append(" ");
}
return sb.insert(0, Integer.toHexString(len)).insert(1," ").delete(sb.length() - 1, sb.length()).toString();
}
}
消息群转发,求最多收到消息人数
import java.util.ArrayList;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Set;
//暴力
public class Main4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<String> set = new HashSet<>();
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
String first = "";
int n = 0;
/*
* try {//判断输入异常 first=input.next(); n=input.nextInt();
* }catch(InputMismatchException e){ e.printStackTrace();
* //System.out.println("输入的内容有点小问题"); System.out.print(0); }
*/
if(first.isEmpty()||n<=0) {//判断输入异常 throw InputMismatchException;
System.out.print(0);
}else {
for (int i = 0; i < n; i++) {
String crowd = input.next();
String[] name = crowd.split(",");
ArrayList<String> al = new ArrayList<>();
for (int j = 0; j < name.length; j++) {
al.add(name[j]);
}
// 针对有顺序的输入,后行中的人名至少有一个在前面的行中出现
if (al.contains(first)) {
for (String s : al) {
set.add(s);
}
} else {
int index = 0;
for (String s : al) {
if (set.contains(s))
break;
index++;
}
if (index < al.size()) {
for (String s : al) {
set.add(s);
}
}
}
}
System.out.print(set.size());
}
Exception exception = new Exception();
}
}
}