一、圆和坐标轴交点个数
输入
第一行 圆的数量
其余 x y r
输入
3
1 1 1
1 1 2
3 4 5
输出 交点个数
2
4
3
package sort;
import java.util.*;
public class AxisCircle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int circleNum = scanner.nextInt();
int[] xAbs = new int[circleNum];
int[] yAbs = new int[circleNum];
int[] r = new int[circleNum];
for(int i = 0; i < circleNum; i++) {
xAbs[i] = Math.abs(scanner.nextInt());
yAbs[i] = Math.abs(scanner.nextInt());
r[i] = scanner.nextInt();
}
for(int i = 0; i < circleNum; i++) {
if(r[i] < xAbs[i] && r[i] < yAbs[i]){
System.out.println(0);
continue;
}
if((r[i] == xAbs[i] && r[i] < yAbs[i]) || (r[i] < xAbs[i] && r[i] == yAbs[i])){
System.out.println(1);
continue;
}
if((r[i] == xAbs[i] && r[i] == yAbs[i]) || (r[i] < xAbs[i] && r[i] > yAbs[i]) || (r[i] > xAbs[i] && r[i] < yAbs[i])){
System.out.println(2);
continue;
}
if((r[i] == xAbs[i] && r[i] == yAbs[i]) || (r[i] < xAbs[i] && r[i] > yAbs[i]) || (r[i] > xAbs[i] && r[i] < yAbs[i])){
System.out.println(2);
continue;
}
if((r[i] == xAbs[i] && r[i] > yAbs[i]) || (r[i] > xAbs[i] && r[i] == yAbs[i]) || (r[i]*r[i] == xAbs[i]*xAbs[i] + yAbs[i]*yAbs[i])){
System.out.println(3);
continue;
}
if( (r[i]*r[i]) > (xAbs[i]*xAbs[i] + yAbs[i]*yAbs[i])){
System.out.println(4);
continue;
}
}
}
}
上面最后一个条件判断错误,只通过百分之四的案例,最后条件应为(r>x && r>y && r^2 != x^2 + y^2), 并且2的情况写了两次,下面为根据别人通过代码测试随机案例,鉴定出以上错误
package zsh;
import java.util.*;
public class AxisCircle {
public static int[] circle1(int[] xAbs, int[] yAbs, int[] r){
int[] res = new int[r.length];
for(int i = 0; i < r.length; i++) {
if(r[i] < xAbs[i] && r[i] < yAbs[i]){
res[i] = 0;
continue;
}
if((r[i] == xAbs[i] && r[i] < yAbs[i]) || (r[i] < xAbs[i] && r[i] == yAbs[i])){
res[i] = 1;
continue;
}
if((r[i] == xAbs[i] && r[i] == yAbs[i]) || (r[i] < xAbs[i] && r[i] > yAbs[i]) || (r[i] > xAbs[i] && r[i] < yAbs[i])){
res[i] = 2;
continue;
}
// if((r[i] == xAbs[i] && r[i] == yAbs[i]) || (r[i] < xAbs[i] && r[i] > yAbs[i]) || (r[i] > xAbs[i] && r[i] < yAbs[i])){
// res[i] = 2;
// continue;
// }
if((r[i] == xAbs[i] && r[i] > yAbs[i]) || (r[i] > xAbs[i] && r[i] == yAbs[i]) || (r[i]*r[i] == xAbs[i]*xAbs[i] + yAbs[i]*yAbs[i])){
res[i] = 3;
continue;
}
if( (r[i] > xAbs[i] && r[i] > yAbs[i] && r[i]*r[i] != xAbs[i]*xAbs[i] + yAbs[i]*yAbs[i])){
res[i] = 4;
continue;
}
}
return res;
}
public static int[] circle2(int[] xAbs, int[] yAbs, int[] r){
int[] res = new int[r.length];
for(int i = 0; i < r.length; i++) {
if (xAbs[i] < r[i]){
res[i] += 2;
}
else if(xAbs[i] == r[i]){
res[i]++;
}
if (yAbs[i] < r[i]){
res[i] += 2;
}
else if(yAbs[i] == r[i]){
res[i]++;
}
if(xAbs[i]*xAbs[i] + yAbs[i]*yAbs[i] == r[i]*r[i]){
res[i]--;
}
}
return res;
}
public static int index = -1; //错误索引
// 对比两个数组(用于测试)
public static boolean isEqual(int[] arr1, int[] arr2) {
if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
return false;
}
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
System.out.println(arr1[i]);
System.out.println(arr2[i]);
index = i;
return false;
}
}
return true;
}
public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// int circleNum = scanner.nextInt();
int num = 10000;
int[] x = new int[num];
//生成随机数组
for (int i = 0; i < x.length; i++) {
x[i] = (int) ((10000 + 1) * Math.random()) - (int) (10000 * Math.random());
}
int[] y = new int[num];
for (int i = 0; i < x.length; i++) {
y[i] = (int) ((10000 + 1) * Math.random()) - (int) (10000 * Math.random());
}
int[] r = new int[num];
for (int i = 0; i < x.length; i++) {
r[i] = (int) ((10000 + 1) * Math.random()) - (int) (10000 * Math.random());
}
//
//
// int num = 1;
// int[] x = {-3574};
// int[] y = {-2864};
// int[] r = {3780};
// int a = r[0]*r[0];
// long b = r[0]*r[0];
int[] xAbs = new int[num];
int[] yAbs = new int[num];
for(int i = 0; i < num; i++) {
xAbs[i] = Math.abs(x[i]);
yAbs[i] = Math.abs(y[i]);
}
// int c = xAbs[0]*xAbs[0] + yAbs[0]*yAbs[0];
int[] res1 = circle1(xAbs, yAbs, r);
int[] res2 = circle2(xAbs, yAbs, r);
boolean succeed = true;
if (!isEqual(res1, res2)) {
succeed = false;
}
if(index >= 0){
System.out.println("x "+x[index]);
System.out.println("y "+y[index]);
System.out.println("r "+r[index]);
}
System.out.println(succeed);
}
}
三、键入括号序列,文本框初始为空白,随即进行N次操作:
1. L:光标向前移动一个字符,(光标前没有字符则保持不动)(左移)
2. R:光标向后移动一个字符,(光标后没有字符则保持不动)(右移)
3. D:删除光标前的字符(光标前没有字符则保持不动)
4. ‘(’ 或 ‘)’:键入括号
评分标准:
1. 如果括号序列合法,得分为括号最大嵌套深度。
2. 如果括号序列不合法,得分为-x,x为使得该序列合法需要插入的最少括号字符个数。
输入
第一行 操作数 整数 N
第二行 长度为N字符串,每个字符代表一次操作,字符均合法;
输出:一行,空格隔开N个整数,第i个整数,代表第i次操作后的实时分数。
输入
4
()DD
输出
-1 1 -1 0
分析:leecode921,20,1111融合一起 括号的有效性判断+括号的最大深度+有效的最小添加
不能一轮完成,因为涉及回退等,每回操作完的序列,经上述三步操作完成。
第一个为参考答案,第二个自己微改的
package zsh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Brackets2 {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //和scanner区别
int num = 1;
char[] inputs2 = new char[num];
try {
num = Integer.valueOf(br.readLine());
System.out.println(num);
inputs2 = new char[num];
for (int i = 0; i < num; i++){
inputs2[i] = (char)br.read();
}
}
catch (IOException e) { //学习
e.printStackTrace();
}
List<Character> list = new ArrayList<>();
int cur = 0;
List<Integer> ans = new ArrayList<>();
for (char c : inputs2){
if (c == '('){
list.add(cur, '(');
cur++;
}
else if (c == ')')
{
list.add(cur,')');
cur++;
}
else if (c == 'L'){
if (cur > 0){
cur--;
}
}
else if (c == 'R'){
if (cur < list.size()){
cur++;
}
}
else {
if (cur >0){
list.remove(cur -1);
cur -- ;
}
}
if (isValid(list)){
ans.add(maxDepth(list));
}
else {
ans.add(minAddToMakeValid(list));
}
}
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < ans.size(); i++){
stringBuffer.append(String.valueOf(ans.get(i)));
if (i != ans.size()-1){
stringBuffer.append(' ');
}
}
System.out.println(stringBuffer.toString());
}
static boolean isValid(List<Character> s){
Deque<Character> stack = new LinkedList<>();
for (int i = 0; i < s.size(); i++){
if (s.get(i) == '('){
stack.push(s.get(i));
}else {
if (stack.isEmpty()){
return false;
}
else if (stack.peek() == '('){
stack.pop();
}
else {
return false;
}
}
}
if (stack.isEmpty()){
return true;
}
return false;
}
static int maxDepth(List<Character> s){
int depth = 0;
int max = 0;
for (Character c: s){
if (c == '('){
depth++;
max = Math.max(depth, max);
}
else {
depth--;
}
}
return max;
}
static int minAddToMakeValid(List<Character> s){
int ans = 0; // 匹配 ))(,记录)数量
int bal = 0; // 正向匹配,如((()
for (int i = 0; i < s.size(); i++){
bal += s.get(i) == '('?1:-1;
if (bal == -1){
ans++;
bal++;
}
}
return -(ans + bal);
}
}
package zsh;
import java.util.*;
public class Brackets {
//判断是否有效括号
public static boolean isValid(List<Character> list){
Deque<Character> stack = new LinkedList<Character>();
for(char c : list){
if(c == '('){
stack.push(c);
}
else {
if(stack.isEmpty()){
return false;
}
if(stack.peek() == '('){
stack.pop();
}
else{
return false;
}
}
}
if(stack.isEmpty()){
return true;
}
return false;
}
//括号嵌套深度
public static int maxDepth(List<Character> list){
int depth = 0;
int maxDepth = 0;
for(char c : list){
if(c == '('){
depth++;
maxDepth = Math.max(maxDepth, depth);
}
else {
depth--;
}
}
return maxDepth;
}
//差几个使之有效
public static int makeValid(List<Character> list){ //平衡法
int left = 0; //正向匹配,如((()
int right = 0; // 匹配 ))(,记录)数量
for(char c : list){
if(c == '('){
left++;
}
else{
left--;
}
if(left < 0){
left++;
right++;
}
}
return -(left+right);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int optionNum = Integer.valueOf(scanner.nextLine());
String s = scanner.nextLine();
int cur = 0; //光标位置
// int[] score = new int[Integer.valueOf(optionNum)];
List<Integer> score = new ArrayList<Integer>();
List<Character> list = new ArrayList<Character>();
for(Character c : s.toCharArray()){
switch(c){
case 'L': //后退
if(cur > 0){
cur--;
}
break;
case 'R': //前进
if(cur < list.size()){
cur++;
}
break;
case 'D':
if(!list.isEmpty()){
list.remove(cur-1);
cur--;
}
break;
case '(':
list.add(cur, c);
cur++;
break;
case ')':
list.add(cur, c);
cur++;
break;
}
if(isValid(list)){
score.add(maxDepth(list));
}
else{
score.add(makeValid(list));
}
}
for(int i = 0; i < optionNum; i++){
System.out.print(score.get(i));
if(i != optionNum-1){
System.out.print(" ");
}
}
}
}