题目一
package demo;
/**
* 8
0 0
0 1
0 2
0 3
1 1
1 2
-1 1
-1 2
*/
import java.util.*;
public class Main6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Point> list = new ArrayList<>();
int n = sc.nextInt();
int x,y;
for (int i=0;i<n;i++){
x = sc.nextInt();
y = sc.nextInt();
list.add(new Point(x,y));
}
int count = getResult(list);
System.out.println(count);
}
private static int getResult(List<Point> list) {
int x,y,x1,y1;
int up,down,left,right,count=0;
for (Point point : list) {
up = down = left = right=0;
x = point.getX();
y = point.getY();
for (Point point2 : list) {
x1 = point2.getX();
y1 = point2.getY();
if (x1==x&&y1>y){
up++;
}else if(x1==x&&y1<y) {
down++;
}
if (y1==y&&x1<x){
left++;
}else if (y1==y&&x1>x){
right++;
}
}
if (up!=0&&down!=0&&left!=0&&right!=0){
count++;
}
}
return count;
}
}
class Point{
int x;
int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public int getX(){
return x;
}
public void setX(int x){
this.x=x;
}
public int getY(){
return y;
}
public void setY(int y){
this.y=y;
}
}
题目二
package demo;
/**
203323
0.0
0.000000
0.009212121
343444323.32432
-12344.1
-12345678.9
*/
import java.math.BigDecimal;
import java.util.*;
public class Main7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Double b = null;
while (sc.hasNext()) {
b = sc.nextDouble();
String bStr = b.toString();
int flag = 0; //用于记录是正数还是负数
String s = null;
if (b < 0){
flag = -1; //小于0时
}else {
flag = 1; //大于等于0时
}
if (flag == 1) {
BigDecimal bigDecimal = new BigDecimal(b);
s = bigDecimal.setScale(2,BigDecimal.ROUND_DOWN).toString(); //正数保留两位转成字符串
} else {
BigDecimal bigDecimal = new BigDecimal(b);
s = bigDecimal.setScale(2,BigDecimal.ROUND_DOWN).toString().substring(1); //负数保留两位并去掉符号
}
List<String> list = new ArrayList<>();
for (int i = 0; i < s.length(); i++) { //把每个字符转成字符串放进list
list.add(String.valueOf(s.charAt(i)));
}
String str = getResult(list, flag);
System.out.println(str);
}
}
private static String getResult(List<String> list, int flag) {
for (int i=list.size()-3-3;i>0;i=i-3){ //从小数点往前数3位开始加“,”依次往前加
list.add(i,",");
}
StringBuffer sb = new StringBuffer();
if (flag==1){
list.add(0,"$");
}
if (flag==-1){
list.add(0,"$");
list.add(0,"(");
list.add(")");
}
for (String s2 : list) {
sb.append(s2);
}
return sb.toString();
}
}
题目三
package demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
int n = sc.nextInt();
for(int i=0;i<n;i++){
list.add(sc.nextInt());
}
getResult(list,n);
}
private static void getResult(List<Integer> list, int n) {
List<String> listB = new ArrayList<>();
// String nB = Integer.toBinaryString(n);
for (Integer integer : list) {
listB.add(Integer.toBinaryString(integer));
}
int len=0;
for (String str : listB) {
boolean flag = false;
for (String str2 : listB) {
int countdiff=0;
len=Math.min(str.length(),str2.length());
for (int i=0;i<len;i++){
if (!(str.charAt(str.length()-1-i)=='1'&&str2.charAt(str2.length()-1-i)=='1')) {//不同时为1
countdiff++;
}
}
if (countdiff==len){//二进制从后往前与之后0的个数==len,说明与之后结果为0,不相似
flag = true;
System.out.print(1+" ");
break;
}
}
if (!flag){
System.out.print(-1+" ");
}
}
}
}
更简单的一种:
package demo;
import java.util.Scanner;
public class Main9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
getResult(arr);
}
private static void getResult(int[] arr) {
for (int a : arr) {
boolean flag = false;
for (int b : arr) {
if ((a&b)==0){ //不相似
System.out.print(1+" ");
flag = true;
break;
}
}
if (!flag){
System.out.print(-1+" ");
}
}
}
}
题目四:
package demo;
import java.util.*;
public class Main11 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> listA = new ArrayList<>();
for (int i=0;i<n;i++){
listA.add(sc.nextInt());
}
listA.add(0,null); //首位加null,是因为第i(1,2,3,4.....)轮时与A[i]比较
getResult(listA,n);
}
private static void getResult(List<Integer> listA,int n) {
List<Integer> validNolist = new ArrayList<>();
HashMap<Integer,Integer> map = new HashMap<>();
for (int i=1;i<=n;i++){
validNolist.add(i); //存编号
}
int count; //报的数字
for (int i=1;i<=n;i++){ //n轮游戏
boolean flag = true;
count=-1; //每次报数之前的数值设为-1(因为要从0开始报数)
while (flag) { //这里是为了如果所有人都没有报到A[i]时,则从头接着报数
for (Integer memberNO : validNolist) {
++count; //每名成员报数时+1
if (count == listA.get(i)) {
validNolist.remove(memberNO); //移出该编号
map.put(memberNO, n - i + 1); //把该编号和对应的名次加进map
flag = false; //报到A[i]之后,设置为false
break; //跳出for
}
}
}
}
//map中的key如果是Integer类型且是连续的则会自动是有序的排列
//所以直接遍历就是按序号输出的
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println(entry.getValue());
}
}
}
题目五
package demo;
/**
5 2
10 1
2 2
5 2
8 1
9 1
*/
import java.util.*;
public class Main14 {
public static void main(String[] args) {
List<Double> listA = new ArrayList<>();
List<Double> listB = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
double price;
int type;
for (int i=0;i<n;i++){
price = sc.nextInt();
type = sc.nextInt();
if (type==1){
listA.add(price);
}
if (type==2){
listB.add(price);
}
}
String sumPriceStr = getResult(listA,listB,k);
System.out.println(sumPriceStr);
}
private static String getResult(List<Double> listA, List<Double> listB, int k) {
double sumPrice = 0.0;
Collections.sort(listA); //价格升序排序一下,则第一个是价格最低的一个
Collections.sort(listB);
// System.out.println(listA);
// System.out.println(listB);
if (k==1){ //一个人买全部
if (listA.size()!=0){ //如果有A类商品,则找出所有中价格最低的一个半价
if (listA.get(0)<=listB.get(0)){
sumPrice += listA.get(0)/2;
listA.remove(0);
}else {
sumPrice += listB.get(0)/2;
listB.remove(0);
}
for (Double price : listA) {
sumPrice += price;
}
}
if (listB.size()!=0){
for (Double price : listB) {
sumPrice += price;
}
}
}
if (k<=listA.size()){ //k-1个人买A中前k-1个高价格的,剩下一个人买剩下所有
for (int i=1;i<=k-1;i++) {
sumPrice += listA.get(listA.size()-1)/2;
listA.remove(listA.size()-1);
}
if (listA.get(0)<=listB.get(0)){
sumPrice += listA.get(0)/2;
listA.remove(0);
for (Double price : listA) {
sumPrice += price;
}
for (Double price : listB) {
sumPrice += price;
}
}else {
sumPrice += listB.get(0)/2;
listB.remove(0);
for (Double price : listA) {
sumPrice += price;
}
for (Double price : listB) {
sumPrice += price;
}
}
}
if (k>listA.size()){ //当k大于A物品数时,逐个买A物品,然后剩下的人买所有的B
for (Double price : listA) { //当listA为空时,不会循环的,已经初始化,也不会抛空指针
sumPrice += price/2;
}
for (Double price : listB) {
sumPrice += price;
}
}
return String.format("%.2f",sumPrice);
}
}