摘花生
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class 摘花生 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(bReader.readLine());
while((T--)>0) {
String []splitStrings=bReader.readLine().split(" ");
int r=Integer.parseInt(splitStrings[0]);
int c=Integer.parseInt(splitStrings[1]);
int[][] arr=new int[r+1][c+1];
for(int i=0;i<r;i++) {
splitStrings=bReader.readLine().split(" ");
for(int j=0;j<c;j++) {
arr[i+1][j+1]=Integer.parseInt(splitStrings[j]);
}
}
int[][] dp=new int[r+1][c+1];
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++) {
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]+arr[i][j]);
}
}
System.out.println(dp[r][c]);
}
}
}
排序与去重
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class 排序与去重 {
public static void main(String[] args) throws IOException {
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(bReader.readLine());
String[] splitStrings=bReader.readLine().split(" ");
int[] arr=new int[n+1];
int count=0;
for(int i=1;i<=n;i++) {
arr[i]=Integer.parseInt(splitStrings[i-1]);
}
Set set=new HashSet();
for(int i=1;i<arr.length;i++) {
set.add(arr[i]);
}
Object[] array=set.toArray();
Arrays.sort(array);
count=array.length;
System.out.println(count);
for(int i=0;i<array.length;i++) {
System.out.print(array[i]+" ");
}
}
}
数组相乘是否为0
import java.util.Scanner;
public class 数组相乘是否为0 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int[] arr=new int[n+1];
int[] brr=new int[n+1];
int sum=0;
for(int i=0;i<n;i++) {
arr[i]=scanner.nextInt();
}
for(int i=0;i<n;i++) {
brr[i]=scanner.nextInt();
}
for(int i=0;i<n;i++) {
sum+=arr[i]*brr[i];
}
if(sum==0) {
System.out.println("Yes");
}else {
System.out.println("No");
}
}
}
K倍区间
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class K倍区间 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
String[] splitStrings=bReader.readLine().split(" ");
int N=Integer.parseInt(splitStrings[0]);
int K=Integer.parseInt(splitStrings[1]);
int[] arr=new int[N+1];
for(int i=1;i<=N;i++) {
arr[i]=Integer.parseInt(bReader.readLine());
}
long ans=0;
for(int r=1;r<=N;r++) {
for(int l=1;l<=r;l++) {
long sum=0;
for(int i=l;i<=r;i++) {
sum+=arr[i];
}
if(sum%K==0) {
ans++;
}
}
}
System.out.println(ans);
}
}
递增三元组
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class 递增三元组 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
int N=Integer.parseInt(bReader.readLine());
int[] a=new int[N+1];
int[] b=new int[N+1];
int[] c=new int[N+1];
String[] splitStrings=bReader.readLine().split(" ");
for(int i=1;i<=N;i++) {
a[i]=Integer.parseInt(splitStrings[i-1])+1;
}
splitStrings=bReader.readLine().split(" ");
for(int i=1;i<=N;i++) {
b[i]=Integer.parseInt(splitStrings[i-1])+1;
}
splitStrings=bReader.readLine().split(" ");
for(int i=1;i<=N;i++) {
c[i]=Integer.parseInt(splitStrings[i-1])+1;
}
Arrays.sort(a);
Arrays.sort(b);
Arrays.sort(c);
int i=1,k=1;
long ans=0;
for(int j=1;j<=N;j++) {
while(i<=N&&b[j]>a[i]) {
i++;
}
while(k<=N&&c[k]<=b[j]) {
k++;
}
ans+=(long)(i-1)*(N-k+1);
}
System.out.println(ans);
}
}