[HuaWei] 7.28笔试

第一题 服务器

输入

// 4

// 0,2,200,0,1

// 1,3,400,0,1

// 2,3,400,1,0

// 3,3,300,0,1

// 3 1 3 200 0 1

输出

//2 1 3

第一行输入服务器个数M

接下来M行为服务器参数,按顺序为编号,cpu数,内存大小, 架构, 是否支持NP卡

最后一行为选择要求, 按顺序为服务器数, 选择策略, cpu数,内存大小,架构, NP

(服务器cpu数内存应不小于要求, 架构NP必须一致)

其中架构0-8 为9则都可以

NP 0-1 为2则都可以

选择策略:

为1时 cpu优先, 优先最符合的cpu数,在选最符合的内存大小

为2时 内存优先, 优先最符合的内存大小,在选最符合的cpu数

cpu数内存大小一样时,按编号大小选

输出说明, 选两台, 分别为1,3


策略性筛选排序问题:因为题目复原不确定性,在排序中使用从小到大排列,与需求最相近的值。

/*
java.util.Comparator<T> 默认升序排列。从小到大
比较它的两个参数的顺序。当第一个参数小于、等于或大于第二个参数时,返回一个负整数、零或正整数。
*@param o1第一个要比较的对象。
*@param o2是要比较的第二个对象。
*@返回负整数、零或正整数,因为*第一个参数小于、等于或大于*第二个参数。
*@throws-NullPointerException(如果参数为null且此*比较器不允许使用null参数)*@throws-ClassCastException(如果参数的类型阻止此比较器对其*进行比较)。
*/

/*
原则
Comparator接口可以实现自定义排序,实现Comparator接口时,要重写compare方法:
	int compare(Object o1, Object o2) 返回一个基本类型的整型
	如果要按照升序排序,则o1 小于o2,返回-1(负数),相等返回0,o1大于o2返回1(正数)
	如果要按照降序排序,则o1 小于o2,返回1(正数),相等返回0,o1大于o2返回-1(负数)
*/
import java.util.*;

// 7.28题目一
public class Main1 {
	// 内部类:服务器及各种参数
	public class Sever{
		// 参数列表:编号、CPU数、内存大小、架构、是否支持NP卡
		private int severID;
		private int cpuNumber;
		private int memoryNumber;
		private int structure;
		private int isSupportNP;
		
		public Sever() {
		}
		
		public Sever(int severID, int cpuNumber, int memoryNumber, int structure, int isSupportNP) {
			this.severID = severID;
			this.cpuNumber = cpuNumber;
			this.memoryNumber = memoryNumber;
			this.structure = structure;
			this.isSupportNP = isSupportNP;
		}
		
		public int getseverID() {
			return severID;
		}
		public int getcpuNumber() {
			return cpuNumber;
		}
		public int getmemoryNumber() {
			return memoryNumber;
		}
		public int getstructure() {
			return structure;
		}
		public int getisSupportNP() {
			return isSupportNP;
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		// 获取输入
		int M = sc.nextInt(); // 服务器个数
		sc.nextLine();//将换行符读掉
		
		// 根据每个服务器的配置参数赋值
		List<Sever> severs = new ArrayList<Main1.Sever>();
		for(int i = 0; i < M; i ++) {
			
			String str = sc.nextLine();
			String[] str_Sever = str.split(",");
			
	        for (int j = 0; j < str_Sever.length; j++) {
	            System.out.println(str_Sever[j]);
	        }
	        
	        // 这里注意内部类的使用方式
	        Main1 ma1 = new Main1();
	        Sever tempSever = ma1.new Sever(Integer.parseInt(str_Sever[0]),Integer.parseInt(str_Sever[1]),Integer.parseInt(str_Sever[2]),Integer.parseInt(str_Sever[3]),Integer.parseInt(str_Sever[4]));
	        
	        severs.add(tempSever);
		}
		
		// 输入选择要求,按顺序为 服务器数,选择策略,CPU数,内存大小,架构,NP卡
		int N, strategy, cpuCount, memSize, cpuArch, supportNP;
		N = sc.nextInt();
		strategy = sc.nextInt();
		cpuCount = sc.nextInt();
		memSize = sc.nextInt();
		cpuArch = sc.nextInt();
		supportNP = sc.nextInt();
		
		// 建立结果数组
		List<Sever> result = new ArrayList<Main1.Sever>();
		// 对原始数组进行条件筛选,找结果
		// 1、服务器cpu数、内存应不小于要求, 架构NP必须一致 —— 硬性要求
		for(int i = 0; i < M; i ++) {
			if(severs.get(i).cpuNumber >= cpuCount && severs.get(i).memoryNumber >= memSize) {
				if(severs.get(i).structure != 9 && severs.get(i).structure != cpuArch) {
					continue;
				}
				if(severs.get(i).isSupportNP != 2 && severs.get(i).isSupportNP != supportNP) {
					continue;
				}
				result.add(severs.get(i));
			}
			
			/*
			if(severs.size() == N) {
				break;
			}*/
		}
		
		// 满足硬性要求后,根据策略进行选择
		if(strategy == 1) {
			Collections.sort(result, new Comparator<Sever>() {

				@Override
				public int compare(Main1.Sever o1, Main1.Sever o2) {
					// TODO Auto-generated method stub
					if(o1.cpuNumber > o2.cpuNumber) {
						return 1;
					}else if(o1.cpuNumber < o2.cpuNumber) {
						return -1;
					}else {
						if(o1.memoryNumber > o2.memoryNumber) {
							return 1;
						}else if(o1.memoryNumber < o2.memoryNumber) {
							return -1;
						}else {
							if(o1.severID > o2.severID) {
								return 1;
							}else {
								return -1;
							}
						}
					}
				}
			});
		}else {
			Collections.sort(result, new Comparator<Sever>() {

				@Override
				public int compare(Main1.Sever o1, Main1.Sever o2) {
					// TODO Auto-generated method stub
					
					if(o1.memoryNumber > o2.memoryNumber) {
						return 1;
					}else if(o1.memoryNumber < o2.memoryNumber) {
						return -1;
					}else {
						if(o1.cpuNumber > o2.cpuNumber) {
							return 1;
						}else if(o1.cpuNumber < o2.cpuNumber) {
							return -1;
						}else {
							if(o1.severID > o2.severID) {
								return 1;
							}else {
								return -1;
							}
						}
					}
				}
			});
		}
		
		// 输出要求的大小
		if(result.size() <= N) {
			System.out.println(result.size());
			for(int i = 0; i < result.size(); i ++) {
				System.out.println(result.get(i).severID);
			}
		}else {
			System.out.println(N);
			for(int i = 0; i < N; i ++) {
				System.out.println(result.get(i).severID);
			}
		}
	}
	
}

/*
4
0,2,200,0,1
1,3,400,0,1
2,3,400,1,0
3,3,300,0,1
3 1 3 200 0 1
 */

/*
7
0,2,200,0,1
1,3,400,0,1
2,3,400,1,0
3,3,300,0,1
4,3,200,0,1
5,2,500,1,1
6,3,300,0,1
3 1 3 200 0 1
 */

第二题 大侠吃药

// 5

// 2

// 4

// 2 1

// 3 1

// 4 2

// 1 5

// 4

输入

第一行为要吃几幅药

第二行一天最多吃几幅

第三行输入接下来几组依赖关系

2 1表示吃了2才能吃1

3 1吃了3才能吃1

有依赖关系的不能一天吃

输出

最快4天吃完


第三题 去西藏

// 3 4

// 1 0 0 0

// 0 0 0 0

// 0 0 2 -1

// 2

输入

二维矩阵 其中1表示起点,2表示终点,0可走,-1不可走

求从1开始经过所有0到2的路径数,不走重复节点

输出

有两条

import java.util.*;

// 去西藏 矩阵中的路径
public class Main3 {
	
	public static int allRoad = 0;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(); // 行数
		int m = sc.nextInt(); // 列数
		
		int[][] paths = new int[n][m]; // 每个地点的具体信息
		
		for(int i = 0; i < n; i ++) {
			for(int j = 0; j < m; j ++) {
				paths[i][j] = sc.nextInt();
			}
		}
		for(int i = 0; i < n; i ++) {
			for(int j = 0; j < m; j ++) {
				System.out.print(paths[i][j] + " ");
			}
			System.out.println();
		}
		
		// 二维矩阵,1表示起点,2表示终点,0可走,-1不可走
		// 求从 1开始经过所有0 到达2 的路径数
		int allZero = 1;
		int hasMetZero = 0;
		int sx = 0,sy = 0; // 起点坐标
		
		for(int i = 0; i < n; i ++) {
			for(int j = 0; j < m; j ++) {
				if(paths[i][j] == 0) {
					allZero ++;
				}
				// 查找起点
				if(paths[i][j] == 1) {
					sx = i;
					sy = j;
					paths[i][j] = 0;
				}
			}
		}
		
		FindRoads(paths, allZero, hasMetZero, sx, sy);

		System.out.println(allRoad);
	}

	private static void FindRoads(int[][] paths, int allZero, int hasMetZero, int sx, int sy) {
		// TODO Auto-generated method stub
		// 结束条件
		if(sx < 0 || sx > paths.length - 1 || sy < 0 || sy > paths[0].length - 1) {
			return;
		}
		// 当 当前位置为起点或不可走,退出
		if(paths[sx][sy] == -1 || paths[sx][sy] == 1) {
			return;
		}
		if(paths[sx][sy] == 2 && hasMetZero != allZero) {
			return;
		}
		if(paths[sx][sy] == 2 && hasMetZero == allZero) {
			allRoad ++;
			return;
		}
		
		
		// 以最初的起点开始出发
		paths[sx][sy] = 1;
		hasMetZero ++; // 已经碰到一个0
		
		System.out.println();
		for(int i = 0; i < paths.length; i ++) {
			for(int j = 0; j < paths[0].length; j ++) {
				System.out.print(paths[i][j] + " ");
			}
			System.out.println();
		}
		
		FindRoads(paths, allZero, hasMetZero, sx - 1, sy);
		FindRoads(paths, allZero, hasMetZero, sx + 1, sy);
		FindRoads(paths, allZero, hasMetZero, sx, sy - 1);
		FindRoads(paths, allZero, hasMetZero, sx, sy + 1);
		
		// 注意要对修改的数据归位
		paths[sx][sy] = 0;
	}
	
}


/*
3 4
1 0 0 0
0 0 0 0
0 0 2 -1
 */

//============================================================================
// Name        : HuaWeiMain283.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
// #include <typeinfo>
#include <limits.h>
#include <string.h>
#include <iomanip>

using namespace std;

void FindRoad(vector<vector<int> > vvMap, int &allZero ,int &allRoad , int haveZero, int x, int y)
{
    if(x < 0 || x > vvMap.size()-1 || y < 0 || y > vvMap[0].size()-1)
        return;

    if(vvMap[x][y] == -1 || vvMap[x][y] == 1)
        return;

    if(vvMap[x][y] == 2 && haveZero != allZero){
    	return;
    }
    if(vvMap[x][y] == 2 && haveZero == allZero)
    {
        allRoad++;
        return;
    }

    vvMap[x][y] = 1;
    haveZero++;
    FindRoad(vvMap, allZero, allRoad, haveZero, x-1, y);
    FindRoad(vvMap, allZero, allRoad, haveZero, x+1, y);
    FindRoad(vvMap, allZero, allRoad, haveZero, x, y-1);
    FindRoad(vvMap, allZero, allRoad, haveZero, x, y+1);
    //vvMap[x][y] = 1;
}

int main()
{
    int M, N;
    cin >> M >> N;
    vector< vector<int> > vvMap = vector< vector<int> >(M, vector<int>(N,0) );
    int allZero = 1;
    int allRoad = 0;
    int haveZero = 0;
    int x = 0;
    int y = 0;

    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
            int temp = 0;
            cin >> temp;
            if(temp == 0)
                allZero++;
            vvMap[i][j] = temp;
            if(temp == 1)
            {
                x = i;
                y = j;
                vvMap[i][j] = 0;
            }
        }
    }

    FindRoad(vvMap, allZero, allRoad, haveZero, x, y);

    cout << allRoad << endl;

    return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int graph[41][41];
int book[41][41];
int res,sx,sy,ex,ey;
int nex[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void dfs(int x,int y,int temp_line_num,int m ,int n ,int line_num){
    if (x==ex and y==ey){
        if (temp_line_num == line_num){
            res+=1;
        }
        return;
    }
    book[x][y] = 1;
    int newx,newy;
    for (int i = 0; i < 4; ++i) {
        newx= nex[i][0]+x;
        newy = nex[i][1]+y;
        if (0<= newx && newx<m && 0<=newy && newy<n &&book[newx][newy] == 0 && (graph[newx][newy]==1 || graph[newx][newy]==0 || graph[newx][newy]==2)){
            dfs(newx,newy,temp_line_num+1,m,n,line_num);
        }
    }
    book[x][y] =0;
    return;
}
int main() {
    int t;
    int a,b,n,m,line_num=1;
    cin>>m>>n;

    for (int i = 0; i <m ; ++i) {
        for (int j = 0; j <n ; ++j) {
            cin>>graph[i][j];
            if (graph[i][j]==0){
                line_num+=1;
            }
            else if (graph[i][j]==1){
                sx= i;
                sy = j;
            } else if (graph[i][j]==2){
                ex= i;
                ey = j;
            }
        }
    }

    dfs(sx,sy,0,m,n,line_num);
    cout<<res<<endl;

//    std::cout << "Hello, World!" << std::endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值