判断线段是否有重叠部分
import java.util.ArrayList;
import java.util.Scanner;
//线段是否有重复 送分签到题 暴力解决
public class Main1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList arrayList = new ArrayList(n);
for (int i = 0; i < n; i++) {
int l = sc.nextInt();
int r = sc.nextInt();
arrayList.add(new Line(l, r));
}
arrayList.sort((a, b) -> (a.l - b.l));
boolean contain = false;
for (int i = 1; i < n; i++) {
Line line1 = arrayList.get(i);
for (int j = 0; j < i; j++) {
Line line2 = arrayList.get(j);
if(line1.l<=line2.l && line2.r <= line1.r){
contain = true;
break;
}
}
if(contain){
break;
}
}
String res = contain ? "true" : "false";
System.out.println(res);
}
static class Line {
int l;
int r;
public Line(int l, int r) {
this.l = l;
this.r = r;
}
}
}
import java.util.*;
public class Main2 {
//纸牌游戏 钉钩钓鱼
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HashSet<Integer> set = new HashSet<>();
int[] a = new int[n];
int[] b = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
b[i] = sc.nextInt();
}
LinkedList<Integer> list = new LinkedList<>();
int indexA = 0;
int indexB = 0;
int countA = 0;
int countB = 0;
boolean isA = true;
while (indexA <= n && indexB <= n) {
if (indexA == n && indexB == n) {
break;
}
isA = (isA && indexA < n) || indexB == n;
int num = isA ? a[indexA] : b[indexB];
if (isA) {
indexA++;
} else {
indexB++;
}
int count = 0;
if (set.contains(num)) {
while (list.getLast() != num) {
int temp = list.getLast();
list.removeLast();
set.remove(temp);
count++;
}
count+=2;
set.remove(list.getLast());
list.removeLast();
if (isA) {
countA += count;
} else {
countB += count;
}
continue;
} else {
list.add(num);
set.add(num);
}
isA = !isA;
}
for (Integer i : list) {
if (i % 2 == 1) {
countA++;
} else {
countB++;
}
}
System.out.println(countA + " " + countB);
}
}
数学题 判断 对于给定的A,B,C 是否存在大于等于0的整数m,n 满足C^m*A+nB = q
import java.util.Scanner;
public class Main3 {
// C^n*A+m*B
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 0; i < T; i++) {
long a = sc.nextInt();
long b = sc.nextInt();
long c = sc.nextInt();
long q = sc.nextInt();
long index = 0;
boolean can = false;
while (true) {
long count = 1;
for (int j = 0; j < index; j++) {
count *= c;
}
index++;
long temp = a * count;
if (temp > q) {
break;
}
can = (q - temp) % b == 0;
if (can) {
break;
}
if(c==1){
break;
}
}
System.out.println(can ? 1 : 0);
}
}
}
第四题 没有搞懂 感觉自己的做法没有问题 给一堆数字 怎么组合使得乘积最大 只过了16% 不知道原因
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Main4 {
//一堆数字 如何排列组合使得结果最大
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//9876 5432 54646432 84
//大数字相乘
int[] nums = new int[10];
for (int i = 0; i < 10; i++) {
nums[i] = sc.nextInt();
}
int maxIndex = 9;
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
int c = 0;
while (maxIndex > 0) {
if (nums[maxIndex] > 0) {
int n = nums[maxIndex] / 2;
for (int i = 0; i < n; i++) {
sb1.append(maxIndex);
sb2.append(maxIndex);
}
nums[maxIndex] -= n * 2;
c += n * 2;
if (nums[maxIndex] > 0) {
if (c % 2 == 0) {
sb1.append(maxIndex);
} else {
sb2.append(maxIndex);
}
c += 1;
}
}
maxIndex--;
}
String s1 = sb1.toString();
String s2 = sb2.toString();
BigInteger b1 = new BigInteger(s1);
BigInteger b2 = new BigInteger(s2);
BigInteger res = b1.multiply(b2);
if(res.equals(BigInteger.ZERO)){
System.out.println("0");
return;
}
String ans = res.toString();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nums[0]; i++) {
sb.append("0");
}
ans = ans + sb;
System.out.println(ans);
}
}
全部评论
(1) 回帖