CSP认证2019年3月份java语言

201903-1 小中大

水题

import java.util.Scanner;

public class Main {
	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 < arr.length; i++) {
			arr[i] = sc.nextInt();
		}
		int max= 0;
		int min = 0;
		if (arr[0] >= arr[n-1]) {
			max = arr[0];
			min = arr[n-1];
		}else {
			max = arr[n-1];
			min = arr[0];
		
		}
		double mid = 0;
		if (n % 2 ==0) {
			mid = (arr[n/2-1] + arr[n/2]) /2.0; 
		}else {
			mid = arr[n/2];
		}
		if (mid *2 % 2 == 0) {
			System.out.print(max+" " +(int)mid+" " +min);
		}else {
			System.out.print(max+" " +mid+" " +min);
		}
	
	}
		
		
}
	

201903-2 二十四点

这道题考的是字符串的处理,我用到的是双链表,一个存放计算数字,一个存放±运算符,因为乘除运算优先,所以先把乘除运算解决以后,再把计算后的数字存放在链表中,然后将可以进行顺序计算啦。


import java.util.ArrayList;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws ParseException {
		
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		String result[] = new String[n];
		scanner.nextLine();
		
		
		for (int i = 0; i < n; i++) {
			String s = scanner.nextLine();
			s+= "+";
			ArrayList<Integer> listNum = new ArrayList<Integer>();
			ArrayList<Character> listPo = new ArrayList<Character>();
			char[] chars = s.toCharArray();
			
			for (int j = 1; j < chars.length; j+=2) {
				int t = chars[j-1]-'0';
				for (; j < chars.length && chars[j] == 'x' || chars[j]=='/'; j+=2) {
					t = (chars[j] == 'x')? t * (chars[j+1] -'0'):t/(chars[j+1]-'0');
				}
				listNum.add(t);
				listPo.add(chars[j]);
			}
			listNum.add(0);
			int t = listNum.get(0);
			for (int j = 1, k = 0; j < listNum.size() && k<listPo.size() ; j++,k++) {
				t =listPo.get(k) == '+'? (t+listNum.get(j)):(t-listNum.get(j));
					
				}
				result[i] = (t==24)?"Yes":"No";
			}
			
		for (int i = 0; i < result.length; i++) {
			System.out.println(result[i]);
		}
		}
		
	}
		
		/*
		 * 10
9+3+4x3
5+4x5x5
7-9-9+8
5x6/5x4
3+5+7+9
1x1+9-9
1x9-5/9
8/5+6x9
6x7-3x6
6x4+4/5
		 */
	

损坏的RAID5

主要是找到块对应的位置,然后注意当缺少的硬盘大于一个时,就不能推导出答案了。


代码样例能过,但是总是超时,没有找到原因。

package suanfa;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException{
		
		char temp[] = new char[8];
		char disks[][] = new char[1005][85000];
		int n = Reader.nextInt();
		int s = Reader.nextInt();
		int l = Reader.nextInt();
		int maxBlock = 0;
		int maxBlock1 = 0;
		 for(int i=0;i<l;++i){//读取l块磁盘的数据
		        int a = Reader.nextInt();
		        String str1 = Reader.next();
		        disks[a] = str1.toCharArray();
		      
		        maxBlock1 = disks[a].length/8;
		        if (maxBlock1 > maxBlock) {
					maxBlock = maxBlock1;
				}
		     
		    }
		 int m = Reader.nextInt();
		 int a =0;
		 String str1 = "";
		 while(m-- > 0) {
			 a = Reader.nextInt();
			 
			 int band=a/s,row=band/(n-1);//计算条带号、单磁盘上的条带号
			 int diskNo=(n-row%n+band%(n-1))%n,block=row*s+a%s;//计算所在磁盘号、所在磁盘上的块号
			 
			 if(block>=maxBlock||(disks[diskNo][0]=='\0'&&n-l>1))//块号超过磁盘上的块数或者该磁盘被损坏且坏掉的磁盘超过1个
		            System.out.println("-");
			 else if(disks[diskNo][0]!='\0'){//该磁盘数据完好,直接输出对应块的数据即可
		            for(int i=0;i<8;++i)
		                System.out.print(disks[diskNo][block*8+i]);
			 }else{//该磁盘被损坏,但数据可恢复
		            long ans=0;
		            for(int i=0;i<n;++i)//遍历其他的块
		                if(diskNo!=i){
		                    for(int j=0;j<8;++j)//将对应的8个字符复制粘贴到temp中
		                        temp[j]=disks[i][block*8+j];
		                   
		                   String str = new String(temp);
		                   long k = Long.parseLong(str, 16);
		                   
		                    ans^=k;//进行异或运算
		                
		                }
		            str1 = Integer.toHexString((int) ans);
                    str1 = str1.toUpperCase();
		            System.out.printf(str1);//输出8位16进制字符串,不够8位在高位补0  
		 }	
	}
		
	}
	
	}
class Reader {
	static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer tokenizer = new StringTokenizer("");
	static String nextLine() throws IOException{// 读取下一行字符串
		return reader.readLine();
	}
	static String next() throws IOException {// 读取下一个字符串
		while (!tokenizer.hasMoreTokens()) {
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static int nextInt() throws IOException {// 读取下一个int型数值
		return Integer.parseInt(next());
	}

	static double nextDouble() throws IOException {// 读取下一个double型数值
		return Double.parseDouble(next());
	}
}


	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值