您的位置:首页 > 其它

华为机试题11-20

2017-03-12 18:47 369 查看

题目描述:句子逆序

将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”

所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符

接口说明

/**

* 反转句子

*

* @param sentence 原句子

* @return 反转后的句子

*/

public String reverse(String sentence);

输入描述:

将一个英文语句以单词为单位逆序排放。

输出描述:

得到逆序的句子

输入例子:

I am a boy

输出例子:

boy a am I

import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
String[] arrStr = str.split(" ");
for(int i =  arrStr.length - 1; i >= 0; i--){
System.out.print(arrStr[i]);
if(i != 0)
System.out.print(" ");
}
}
}
}


题目描述:字符个数统计

编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。

输入描述:

输入N个字符,字符在ACSII码范围内。

输出描述:

输出范围在(0~127)字符的个数。

输入例子:

abc

输出例子:

3

import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
Set<Integer> set = new LinkedHashSet<>();
int num = 0;
for(int i = 0; i < str.length(); i++){
int n = (int)str.charAt(i);
if(n >=0 && n <= 127)
set.add(n);
}
System.out.println(set.size());
}
}
}


Set 中的元素数是size()方法。

使用Set保证不重复。

题目描述:合并表记录【此题多注意一下】

数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。

输入描述:

先输入键值对的个数

然后输入成对的index和value值,以空格隔开

输出描述:

输出合并后的键值对(多行)

输入例子:4

0 1

0 2

1 2

3 4

输出例子:

0 3

1 2

3 4

import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
Map<Integer, Integer> map = new TreeMap<>();
int num = sc.nextInt();
for(int i = 0; i < num; i++){
int k = sc.nextInt();
int value = sc.nextInt();
if(map.containsKey(k)){
map.put(k, map.get(k) + value);
} else
map.put(k, value);
}
for(Integer key : map.keySet()){
System.out.println(key + " " + map.get(key));
}
}
}
}


题目描述:字串的连接最长路径查找

给定n个字符串,请对n个字符串按照字典序排列。

输入描述:

输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。

输出描述:

数据输出n行,输出结果为按照字典序排列的字符串。

输入例子:

9

cap

to

cat

card

two

too

up

boat

boot

输出例子:

boat

boot

cap

card

cat

to

too

two

up

import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
List<String> list = new ArrayList<>();
int n = Integer.valueOf(sc.nextLine());
for(int i = 0; i < n; i++){
list.add(sc.nextLine());
}
Collections.sort(list);
for(String i : list){
System.out.println(i);
}
}
}
}


import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
List<String> list = new ArrayList<>();
int n = sc.nextInt();
for(int i = 0; i < n; i++){
list.add(sc.next());
}
Collections.sort(list);
for(String i : list){
System.out.println(i);
}
}
}
}


import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
String[] str = new String
;
for(int i = 0; i < n; i++){
str[i] = sc.next();
}
Arrays.sort(str);
for(String i : str){
System.out.println(i);
}
}
}
}


题目描述:求int型正整数在内存中存储时1的个数

输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数。

输入描述:

输入一个整数(int类型)

输出描述:

这个数转换成2进制后,输出1的个数

输入例子:5

输出例子:

2

import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int num = 0;
int n = sc.nextInt();
while(n != 0){
if(n == 1){
num += 1;
break;
}
if(n % 2 == 1)
num++;
n /= 2;
}
System.out.println(num);
}
}
}


题目描述:字符逆序

将一个字符串str的内容颠倒过来,并输出。str的长度不超过100个字符。 如:输入“I am a student”,输出“tneduts a ma I”。

输入参数:

inputString:输入的字符串

返回值:

输出转换好的逆序字符串

输入描述:

输入一个字符串,可以有空格

输出描述:

输出逆序的字符串

输入例子:I am a student

输出例子:

tneduts a ma I

import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
StringBuilder sb = new StringBuilder(str);
System.out.println(sb.reverse());
}
}
}


题目描述:最小公倍数(两个数的乘积 除以 最大公约数)

正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。

输入描述:

输入两个正整数A和B。

输出描述:

输出A和B的最小公倍数。

输入例子:5

7

输出例子:

35

import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a * b / gys(a,b));
}
}
private static int gys(int m, int n){
while(n != 0){
int rem = m % n;
m = n;
n = rem;
}
return m;
}
}


题目描述:求解立方根:解决一切方根问题!!!!

•计算一个数字的立方根,不使用库函数

详细描述:

•接口说明

原型:

public static double getCubeRoot(double input)

输入:double 待求解参数

返回值:double 输入参数的立方根

输入描述:

待求解参数 double类型

输出描述:

输入参数的立方根 也是double类型

输入例子:

216

输出例子:

6.0

// import java.text.DecimalFormat;
import java.util.*;

public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
double input = sc.nextDouble();
double cube = getCubeRoot(input);
if(input < 0)
cube = -1 * cube;
System.out.printf("%.1f", cube);
//            DecimalFormat df = new DecimalFormat("#0.0");
//            System.out.println(df.format(cube));
}
}
private static double getCubeRoot(double input) {
return Math.exp(Math.log(input) / 3);
}
}


题目描述 记负均正II

从输入任意个整型数,统计其中的负数个数并求所有非负数的平均值

输入描述:

输入任意个整数

输出描述:

输出负数个数以及所有非负数的平均值

输入例子:

-13

-4

-7

输出例子:

3

0.0

import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int count = 0;
int num = 0;
double avg = 0.0;
while(sc.hasNext()){
int n = sc.nextInt();
if(n >= 0){
avg += n;
num++;
}
else
count++;
}
avg /= num;
System.out.println(count);
System.out.printf("%.1f", avg);
}
}


题目描述:背包问题

王强今天很开心,公司发给N元的年终奖。王强决定把年终奖用于购物,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子:

主件附件电脑打印机,扫描仪书柜图书书桌台灯,文具工作椅无

如果要买归类为附件的物品,必须先买该附件所属的主件。每个主件可以有 0 个、 1 个或 2 个附件。附件不再有从属于自己的附件。王强想买的东西很多,为了不超出预算,他把每件物品规定了一个重要度,分为 5 等:用整数 1 ~ 5 表示,第 5 等最重要。他还从因特网上查到了每件物品的价格(都是 10 元的整数倍)。他希望在不超过 N 元(可以等于 N 元)的前提下,使每件物品的价格与重要度的乘积的总和最大。

设第 j 件物品的价格为 v[j] ,重要度为 w[j] ,共选中了 k 件物品,编号依次为 j 1 , j 2 ,……, j k ,则所求的总和为:

v[j 1 ]w[j 1 ]+v[j 2 ]*w[j 2 ]+ … +v[j k ]*w[j k ] 。(其中 为乘号)

请你帮助王强设计一个满足要求的购物单。

输入描述:

输入的第 1 行,为两个正整数,用一个空格隔开:N m

(其中 N ( <32000 )表示总钱数, m ( <60 )为希望购买物品的个数。)

从第 2 行到第 m+1 行,第 j 行给出了编号为 j-1 的物品的基本数据,每行有 3 个非负整数 v p q

(其中 v 表示该物品的价格( v<10000 ), p 表示该物品的重要度( 1 ~ 5 ), q 表示该物品是主件还是附件。如果 q=0 ,表示该物品为主件,如果 q>0 ,表示该物品为附件, q 是所属主件的编号)

输出描述:

输出文件只有一个正整数,为不超过总钱数的物品的价格与重要度乘积的总和的最大值( <200000 )。

输入例子:1000 5

800 2 0

400 5 1

300 5 1

400 3 0

500 2 0

输出例子:

2200

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNextInt()) {
int money = scan.nextInt(); // 总钱数
int goods = scan.nextInt(); // 希望购买物品的个数
int[] price = new int[goods];
int[] value = new int[goods];
int[] chief = new int[goods];
for (int i = 0; i < goods; i++) {
price[i] = scan.nextInt(); // 物品的价格
value[i] = price[i] * scan.nextInt(); // 物品的价值(价格*重要度)
chief[i] = scan.nextInt(); // 主件或附件
}
System.out.println(getMaxValue(money, goods, price, value, chief));
}
}

public static int getMaxValue(int m, int g, int[] p, int[] v, int[] c) {
int[][] dp = new int[g + 1][m + 1];
for (int i = 1; i <= g; i++) {
for (int j = 1; j <= m; j++) {
if (c[i - 1] == 0) { // 主件
if (p[i - 1] <= j) { // 当前购买金额小于总钱数
// dp[i][j]表示前i个物品放入容量(总钱数)为j的背包的最大价值
// dp[i-1][j]表示前i-1个物品放入容器(总钱数)为j的背包的最大价值
// dp[i-1][j-p[i-1]]表示前i-1个物品放入容器(总钱数)为j-p[i-1]的背包的最大价值
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - p[i - 1]] + v[i - 1]); // 用总金额j购买i件商品,可以获得最大价值
}
} else { // 附件(此时c表示所属主件编号),p[c[i-1]]表示所属主件的价格
if (p[i - 1] + p[c[i - 1]] <= j) {
dp[i][j] = Math.max(dp[i - 1][j],
dp[i - 1][j - p[c[i - 1]] - p[i - 1]] + v[c[i - 1]] + v[i - 1]);
}
}
}
}
return dp[g][m];
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: