软通动力华为java机考题库_华为机考笔试刷题-java-1
題庫來源
計算字符個數(shù)
寫出一個程序,接受一個由字母和數(shù)字組成的字符串,和一個字符,然后輸出輸入字符串中含有該字符的個數(shù)。不區(qū)分大小寫。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
char[] text = sc.nextLine().toLowerCase().toCharArray();
char targetChar = sc.nextLine().toLowerCase().toCharArray()[0];
int count=0;
for (Character t : text) {
if (t==targetChar) {
count=count+1;
}
}
System.out.println(count);
}
}
明明的隨機數(shù)
他先用計算機生成了N個1到1000之間的隨機整數(shù)(N≤1000),對于其中重復(fù)的數(shù)字,只保留一個,把其余相同的數(shù)去掉,不同的數(shù)對應(yīng)著不同的學(xué)生的學(xué)號。
再把這些數(shù)從小到大排序,按照排好的順序去找同學(xué)做調(diào)查。
TreeSet與HashSet
HashSet不能保證元素的排列列順序,TreeSet是SortedSet接?口的唯一實現(xiàn)類,可以確保集合
元素處于排序狀態(tài)
HashSet底層?用的是哈希表,TreeSet采?用的數(shù)據(jù)結(jié)構(gòu)是紅黑樹(紅黑樹是一種特定類型的二叉樹)
HashSet中元素可以是null,但只能有一個,TreeSet不不允許放入null
一般使用HashSet,如果需要排序的功能時,才使?用TreeSet(性能原因)
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
TreeSet set=new TreeSet(new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
int n=sc.nextInt();
if(n>0){
for(int i=0;i
set.add(sc.nextInt());
}
}
for(Integer i:set){
System.out.println(i);
}
}
}
}
字符串?dāng)?shù)組
?連續(xù)輸入字符串,請按長度為8拆分每個字符串后輸出到新的字符串?dāng)?shù)組;
?長度不是8整數(shù)倍的字符串請在后面補數(shù)字0,空字符串不處理。
這題的重點在于要將長度不是8的整數(shù)倍字符串補齊后循環(huán)輸出,而不是輸出同時補齊(太麻煩并且)。
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String line = sc.nextLine();
int lineLen = line.length();
if (lineLen % 8 == 0) {
for (int i = 0; i < lineLen; i = i + 8) {
System.out.println(line.substring(i, i + 8));
}
} else {
int lenMod = 8 - lineLen % 8;
for (int i = 0; i < lenMod; i++) {
line = line + 0;
}
if (lineLen < 8) {
System.out.println(line);
} else {
for (int j = 0; j < lineLen; j = j + 8) {
System.out.println(line.substring(j, j + 8));
}
}
}
}
}
}
進制轉(zhuǎn)換
寫出一個程序,接受一個十六進制的數(shù),輸出該數(shù)值的十進制表示。(多組同時輸入 )
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(Long.parseLong(line.substring(2), 16));
}
}
}
輸出質(zhì)因子
功能:輸入一個正整數(shù),按照從小到大的順序輸出它的所有質(zhì)因子(重復(fù)的也要列舉)(如180的質(zhì)因子為2 2 3 3 5 )
最后一個數(shù)后面也要有空格
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
long number = 0;
while(scanner.hasNextLong())
{
number = scanner.nextLong();
isPrimerFactors(number);
}
}
private static void isPrimerFactors(long num)
{
long number = num;
while(number != 1)
{
for(int i = 2; i <= number ;i++)
{
if(number % i == 0)
{
number /= i;
System.out.print(i + " ");
break;
}
}
}
}
}
取近似值
java中的三種取整函數(shù):floor,ceil,round
寫出一個程序,接受一個正浮點數(shù)值,輸出該數(shù)值的近似整數(shù)值。如果小數(shù)點后數(shù)值大于等于5,向上取整;小于5,則向下取整。
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
float num=sc.nextFloat();
System.out.println(Math.round(num));
}
}
合并表記錄(TreeMap)
數(shù)據(jù)表記錄包含表索引和數(shù)值(int范圍的整數(shù)),請對表索引相同的記錄進行合并,
即將相同索引的數(shù)值進行求和運算,輸出按照key值升序進行輸出。
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num=sc.nextInt();
TreeMap map=new TreeMap<>();
while(num>0){
int key=sc.nextInt();
int value=sc.nextInt();
if(!map.containsKey(key)){
map.put(key,value);
}else{
map.put(key,map.get(key)+value);
}
num--;
}
Iterator iterator=map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry en = (Map.Entry)iterator.next();
Integer key=en.getKey();
Integer value=en.getValue();
System.out.println(key+" "+value);
}
}
}
提取不重復(fù)的整數(shù)
輸入一個int型整數(shù),按照從右向左的閱讀順序,返回一個不含重復(fù)數(shù)字的新的整數(shù)。
① HashSet的輸出順序是不確定的,但是它的速度最快;
② TreeSet輸出順序是升序排列的,相當(dāng)于C++中的set,個人比較喜歡這種;
③ LinkedHashSet輸出順序是確定的,就是插入時的順序。
import java.util.*;
/**
* @Author hwj
* @Date 2020/8/15 8:37
* @Desc:
**/
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] number=sc.nextLine().split("");
int numLen=number.length;
LinkedHashSet set=new LinkedHashSet<>();
for(int i=numLen-1;i>=0;i--){
if(!set.contains(Integer.parseInt(number[i]))) {
set.add(Integer.parseInt(number[i]));
}
}
Iterator iterator= set.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next());
}
}
}
句子逆序
將一個英文語句以單詞為單位逆序排放。
例如“I am a boy”,逆序排放后為“boy a am I”
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String sentence = sc.nextLine();
String strReverse=reverse(sentence);
System.out.println(strReverse);
}
public static String reverse(String sentence){
String[] str=sentence.split(" ");
int strLen=str.length;
String str2=str[strLen-1];
for(int i=strLen-2;i>=0;i--){
str2=str2+" "+str[i];
}
return str2;
}
字串的連接最長路徑查找
給定n個字符串,請對n個字符串按照字典序排列。
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int num=n;
TreeSet set=new TreeSet<>();
while(num>=0){
set.add(sc.nextLine());
num--;
}
Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
字串的連接最長路徑查找
在線編程適用: BufferedReader
集合排序 Collections.sort()
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* @Author hwj
* @Date 2020/8/15 8:37
* @Desc:
**/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num=Integer.parseInt(br.readLine());
ArrayList arr=new ArrayList<>();
while(num>0){
num--;
arr.add(br.readLine());
}
Collections.sort(arr);
for(String s:arr){
System.out.println(s);
}
}
}
總結(jié)
以上是生活随笔為你收集整理的软通动力华为java机考题库_华为机考笔试刷题-java-1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA(2021)最全常用快捷键《必须
- 下一篇: 写得好的html网页,优化网站排名-使用