BUPT2012计院机试Java题解

题目搜集于各位前辈,侵删。

第一题

在这里插入图片描述

import java.util.Scanner;

public class c1201 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int t=0;t<T;t++) {
			int n = sc.nextInt();
			System.out.println(Integer.toBinaryString(n));
		}
		sc.close();
	}

}

第二题

在这里插入图片描述
在这里插入图片描述

import java.util.Scanner;
//貌似第二组数据是有问题的
public class c1202 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int t=0;t<T;t++) {
			int n = sc.nextInt();
			int K = sc.nextInt();
			int[][] arr = new int[n][n];
			int[][] temp = new int[n][n];
			int[][] ans = new int[n][n];
			for(int i = 0;i<n;i++) {
				for(int j = 0;j<n;j++) {
					arr[i][j] = sc.nextInt();
					temp[i][j] = arr[i][j];
				}
			}
			for(int k = 2;k<=K;k++) {
				for(int i = 0;i<n;i++) {
					for(int j = 0;j<n;j++) {
						int sum = 0;
						for(int p = 0;p<n;p++) {
							sum+=arr[i][p]*arr[p][j];
						}
						ans[i][j] = sum;
					}
				}
				
				for(int i = 0;i<n;i++) {
					for(int j = 0;j<n;j++) {
						temp[i][j] = ans[i][j];
					}
				}
			}
			
			for(int i = 0;i<n;i++) {
				for(int j = 0;j<n;j++) {
					if(j == 0) {
						System.out.print(ans[i][j]);
					}else {
						System.out.print(" "+ans[i][j]);
					}
					
				}
				System.out.println();
			}
			
		}
		sc.close();
	}

}

第三题

在这里插入图片描述

import java.util.Scanner;

public class c1201 {

	static Node root ;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int t=0;t<T;t++) {
			int a = sc.nextInt();
			Node node = search(root,a);
			if(node == null) {
				root = new Node(a);
				System.out.print(-1);
			}else {
				System.out.print(node.value);
				if(a<node.value) {
					Node n = new Node(a);
					node.left = n;
				}else {
					Node n = new Node(a);
					node.right = n;
				}
			}
			
			
		}
		sc.close();
	}
	
	static class Node{
		int value;
		Node left = null;
		Node right = null;
		public Node(int value) {
			super();
			this.value = value;
		}
		
	}
	
	static Node search(Node root,int a) {
		if(root == null) {
			return root;
		}
		if(a < root.value) {
			if(root.left == null) {
				return root;
			}else {
				return search(root.left,a);
			}
		}else {
			if(root.right == null) {
				return root;
			}else {
				return search(root.right,a);
			}
		}
	}
}

第四题

在这里插入图片描述
在这里插入图片描述

import java.util.Scanner;
//45 00 00 34 7a 67 40 00 40 06 63 5a 0a cd 0a f4 7d 38 ca 09 cd f6 00 50 b4 d7 ae 1c
//45 00 00 c6 56 5a 40 00 34 06 e0 45 cb d0 2e 01 0a cd 0a f4 00 50 ce 61 e1 e9 b9 ee 47 c7 37 34
public class c1204 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int T = Integer.parseInt(sc.nextLine());
		for(int t=1;t<=T;t++) {
			String s = sc.nextLine();
			String[] ss = s.split(" ");
			int len = Integer.parseInt(ss[2]+ss[3], 16);
			int[] source = new int[4];
			int[] des = new int[4];
			for(int i = 0;i<4;i++) {
				source[i] = Integer.parseInt(ss[12+i],16);
				des[i] = Integer.parseInt(ss[16+i],16);
			}
			int sourcePort = Integer.parseInt(ss[20]+ss[21], 16);
			int desPort = Integer.parseInt(ss[22]+ss[23], 16);
			
			System.out.println("Case #"+t);
			System.out.println("Total length = "+len+" bytes");
			System.out.println("Source = "+source[0]+"."+source[1]+"."+source[2]+"."+source[3]);
			System.out.println("Destination = "+des[0]+"."+des[1]+"."+des[2]+"."+des[3]);
			System.out.println("Source Port = "+sourcePort);
			System.out.println("Destination Port = "+desPort);
		}
		sc.close();
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值