BUPT2011计院机试Java题解

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

第一题

在这里插入图片描述

import java.util.Scanner;

public class c1101 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			String s = sc.nextLine();
			String[] ss = s.split(" ");
			for(int i = ss.length-1;i>=0;i--) {
				if(i==ss.length-1) {
					System.out.print(ss[i].toLowerCase());
				}else {
					System.out.print(" "+ss[i].toLowerCase());
				}
			}
			System.out.println();
		}
		sc.close();
	}

}

第二题

在这里插入图片描述

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class c1102 {

	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();
			Map<Integer,Integer> map = new HashMap<Integer,Integer>();
			for(int i = 0;i<n;i++) {
				String op = sc.next();
				if(op.equals("Insert")) {
					int key = sc.nextInt();
					int value = sc.nextInt();
					map.put(key, value);
				}else {
					int key = sc.nextInt();
					if(!map.containsKey(key)) {
						System.out.println("No result!");
					}else{
						int value = map.get(key);
						System.out.println("name:"+key+" score:"+value);
					}
				}
			}
		
		}
		sc.close();
	}

}

第三题

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

6 5 5
0 1
0 2
1 3
2 4
2 5

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class c1103 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		int l = sc.nextInt();
		Node root = new Node(0);
		for(int i = 0;i<n;i++) {
			int fatherValue = sc.nextInt();
			int sonValue = sc.nextInt();
			Node father = search(root,fatherValue);
			Node son = new Node(sonValue);
			son.father = father;
			father.sons.add(son);
		}
		
		double ans = 1;
		Node node = search(root,l);
		while(node.father!=null) {
			ans*=node.father.sons.size();
			node = node.father;
		}
		System.out.println(String.format("%.6f", 1.0/ans));
		
	}
	
	static class Node{
		int value;
		ArrayList<Node> sons = new ArrayList<Node>();
		Node father = null;
		public Node(int value) {
			super();
			this.value = value;
		}
	}
	
	static Node search(Node root,int value) {
		Queue<Node> queue = new LinkedList<Node>();
		queue.add(root);
		while(!queue.isEmpty()) {
			Node node = queue.remove();
			if(node.value==value) {
				return node;
			}else {
				for(Node son:node.sons) {
					queue.add(son);
				}
			}
		}
		return null;
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值