目录
A:奇数倍数
一个简单的签到题,把2019的每一个奇数倍进行枚举计算就行了,然后判断一下每一个数位上是否存在偶数
import java.io.*;
/**
* @ClassName 奇数倍数
* @Description TODO
* @Author 小怂很怂
* @Date 2023/3/4 21:20
* @Version 1.0
**/
public class 奇数倍数 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(br);
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
int n=2019;
for (int i=1;;i+=2){
int t=i*n;
int x=0;
while (t!=0){
if (t % 2 == 0) {
x = 1;
break;
}
t/=10;
}
if (x==0){
pw.println(i*n);
pw.flush();
return;
}
}
}
public static int nextInt() throws Exception {//int型
st.nextToken();
return (int) st.nval;
}
public static long nextLong() throws Exception {//long型
st.nextToken();
return (long) st.nval;
}
}
B:求值
和第一题一样,直接枚举吧,今天签到有点多了哈
import java.io.*;
/**
* @ClassName Main
* @Description TODO
* @Author 小怂很怂
* @Date 2023/3/4 21:05
* @Version 1.0
**/
public class 求值 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(br);
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
for(int i=5;;i++){
int t=0;
int k= (int) Math.sqrt(i);
for (int j=1;j<=k;j++){
if (i%j==0&&j!=k) t+=2;
else if (i%j==0&&j==k) t+=1;
}
if (t==100){
pw.println(i);
pw.flush();
return;
}
}
}
public static int nextInt() throws Exception {//int型
st.nextToken();
return (int) st.nval;
}
public static long nextLong() throws Exception {//long型
st.nextToken();
return (long) st.nval;
}
}
C:求和
来自于去年省赛的真题,暴力是行不通了,那么就用前缀和吧,因为每一个数都要乘后面所有的数,对于arr[i]来说总和即(arr[n]-arr[i])*(arr[i]-arr[i-1]),那我们设定一个变量来求得它们的总和,再逐个计算就可以了
import java.io.*;
/**
* @ClassName 求和
* @Description TODO
* @Author 小怂很怂
* @Date 2023/3/4 21:11
* @Version 1.0
**/
public class 求和 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(br);
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
int n=nextInt();
long s=0;
int []arr=new int[n];
long count=0;
for (int i=0;i<n;i++){
arr[i]=nextInt();
s+=arr[i];
}
for (int i=0;i<n;i++){
count=count+(s-arr[i])*arr[i];
s=s-arr[i];
}
pw.println(count);
pw.flush();
}
public static int nextInt() throws Exception {//int型
st.nextToken();
return (int) st.nval;
}
public static long nextLong() throws Exception {//long型
st.nextToken();
return (long) st.nval;
}
}
D:数位排序
好像也是去年的一个真题,一个简单的二维排序解决
arr[i][0]存放原数,arr[i][1]存放各数位之和
import java.io.*;
import java.util.Arrays;
/**
* @ClassName 数位排序
* @Description TODO
* @Author 小怂很怂
* @Date 2023/3/4 21:14
* @Version 1.0
**/
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(br);
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
int n=nextInt();
int m=nextInt();
int [][]arr=new int[n][2];
for (int i=0;i<n;i++){
arr[i][0]=i+1;
int t=arr[i][0];
int s=0;
while (t!=0){
s=s+t%10;
t/=10;
}
arr[i][1]=s;
}
Arrays.sort(arr,((a,b)->{
if (a[1]!=b[1])
return a[1]-b[1];
return a[0]-b[0];
}));
pw.println(arr[m-1][0]);
pw.flush();
}
public static int nextInt() throws Exception {//int型
st.nextToken();
return (int) st.nval;
}
public static long nextLong() throws Exception {//long型
st.nextToken();
return (long) st.nval;
}
}