----------------------------------------------------------------------------------------------------------
第一题
public class Main {
public static void main(String[] args) {
//答案:1905111
int sum=0;
for (int i = 1; i <=2019; i++) {//1到2019
String s=String.valueOf(i);
for (int j = 0; j <s.length(); j++) {//判断是否包含 2,0,1,9
if (s.charAt(j)=='2'||s.charAt(j)=='0'||s.charAt(j)=='1'||s.charAt(j)=='9') {
sum+=i;
break;
}
}
}
System.out.println(sum);
}
}
----------------------------------------------------------------------------------------------------------
第二题
public class Main {
public static void main(String[] args) {
//答案:21
int h=2019,w=324;//长和宽
int count=0;
while (h!=w) {//当矩形为正方形时,停止切割
int t=h; //长等于宽,宽等于长减宽
h=w;
w=(t-w);
if (w>h) {//如果宽大于长,则交换
t=w;
w=h;
h=t;
}
count++;
}
System.out.println(++count);
}
}
----------------------------------------------------------------------------------------------------------
第三题
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
//答案:100
String s="0100110001010001";
Set<String> set=new HashSet<String>();
for (int i = 0; i <s.length(); i++) {
String str=String.valueOf(s.charAt(i));
set.add(str);
for (int j = i+1; j <s.length(); j++) {
str+=String.valueOf(s.charAt(j));
set.add(str);
}
}
System.out.println(set.size());
}
}
----------------------------------------------------------------------------------------------------------
第四题
public class Main {
public static void main(String[] args) {
//答案:17569
int count=0;
for (int i = 2; ; i++) {
boolean bo=true;
for (int j = 2; j <i; j++) {
if (i%j==0) {
bo=false;
break;
}
}
if (bo) {
count++;
}
if (count==2019) {
System.out.println(i);
break;
}
}
}
}
----------------------------------------------------------------------------------------------------------
第五题
public class Main {
public static void main(String[] args) {
//答案:34
/*一共七组,中间组最中间的值为最大值x
* 那么第5,6,7的中间值都要比x大,并且4,5,6,7组中间值后的数都比x大
* 比x大的值有 4*4-1=15个
* 49-15=34*/
}
}
----------------------------------------------------------------------------------------------------------
第六题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int [][]a=new int[n][m];
for (int i = 0; i <n; i++) {
for (int j = 0; j <m; j++) {
a[i][j]=sc.nextInt();
}
}
int j=0;
for (int x =0; x<m;x++,j++) {
int i=n-1;
for (int k = 0; k <n; k++,i--) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------
第七题
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main57 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();//店的个数
int []Nnum=new int[N+1];//店的优先级数 (1~N)
int M=sc.nextInt();//订单数
int T=sc.nextInt();//T时刻优先店数
int [][]TS=new int[M][2];//每个时刻 哪个店的订单
//输入订单
for (int i = 0; i <M; i++) {
int ts=sc.nextInt();
int id=sc.nextInt();
TS[i][0]=ts;
TS[i][1]=id;
}
//订单按时间排序
Arrays.sort(TS,new Comparator<int[]>() {
public int compare(int []a,int []b){
if (a[0]>b[0]) {
return 1;
}else {
return 0;
}
}
});
//根据TS时刻订单表,改变Nnum店的优先级
for (int i = 1; i <=T;i++) {
int tid=0;//存储第i时刻,哪个店加了2
for (int[] is : TS) {
if (is[0]==i) {//时刻为i的订单
Nnum[is[1]]+=2;//优先级+2
tid=is[1];//存储第i时刻,哪个店加了2
}
}
for (int j = 1; j < Nnum.length; j++) {
if (j!=tid) {
Nnum[j]--;
if (Nnum[j]<0) {
Nnum[j]=0;
}
}
}
}
int count=0;//T时刻优先级>5的店数量
for (int is : Nnum) {
if (is>5) {
count++;
}
}
System.out.println(count);
}
}
//这题不难,但是在将想法实现成代码中,会出现一些bug,导致在这题上用的大量时间
//基础不牢固
----------------------------------------------------------------------------------------------------------
第八题
膈了一个月后我才真正看清楚题目的意思,原来没有仔细看“注意”的第二条,查找的字符串前后不能有其余字符,如果前后有其他字符,就看不明白题解代码,今天重复看了多次才明白题目的意思,重新提交一次代码
import java.util.Scanner;
public class Main67 {
//递归实现逆序输出
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int res=0;
int K=sc.nextInt();
sc.nextLine();
String str=sc.nextLine();
String []words=str.split("\\s+|\\.");//空格或点分隔,以点分隔的为空字符串
//Alice------>Bob
for (int i = 0; i < words.length; i++) {
if (words[i].equals("Alice")) {
for (int j =i+1; j < words.length; j++) {
int sum=1;
if (words[j].equals("Bob")) {
for (int k = i+1; k <j; k++) {
sum+=words[k].length()+1;
}
if (sum<=K) {
res++;
}
}
}
}
}
//Bob------>Alice
for (int i = 0; i < words.length; i++) {
if (words[i].equals("Bob")) {
for (int j = i+1; j < words.length; j++) {
int sum=1;
if (words[j].equals("Alice")) {
for (int k = i+1; k < j; k++) {
sum+=words[k].length()+1;
}
if (sum<=K) {
res++;
}
}
}
}
}
System.out.println(res);
}
}
----------------------------------------------------------------------------------------------------------
第九题
import java.util.Arrays;
import java.util.Scanner;
public class Main58 {
public static void main(String[] args) {
/*
这题思路很清晰,将输入的n个数升序排列arr[],
arr[]后一个减前一个得到公差d,存入ds[]
求ds[]的最大公约数即最终的公差d,a1+(n-1)*d=an,得到n即等差数列个数
需要注意的地方:
a1=d时,a1=d
d=0时,count=n
*/
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int []arr=new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i]=sc.nextInt();
}
Arrays.sort(arr);//将输入的排序
int []ds=new int[n-1];//所有的公差
for (int i = 0; i < ds.length; i++) {
ds[i]=arr[i+1]-arr[i];
}
Arrays.sort(ds);//公差排序
//求公差最大公约数
int gcd=0;
if (ds[0]!=0)//当公差为零时不必找最大公约数
{
for (int i = ds[0]; i >0;i-- ) {
boolean bo=true;
for (int j = 0; j < ds.length; j++) {
if (ds[j]%i!=0) {
bo=false;
break;
}
}
if (bo) {
gcd=i;
break;
}
}
}
int a1=arr[0]-gcd;
if(gcd==arr[0])a1=gcd; //当d==a1时,a1=d;
int an=arr[arr.length-1];
int count=n;//当公差为零时,count=n
if(gcd!=0)
{
count = (an-a1)/gcd+1;
}
System.out.println(count);
}
}
----------------------------------------------------------------------------------------------------------
第十题
- 这题毫无头绪 查看大佬题解