华为Oj题(01-10)

题目1:

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by huststl on 2018/3/11 22:16
 5  * 计算字符串最后一个单词的长度,单词以空格隔开。
 6  */
 7 public class demo01 {
 8     public static void main(String[] args) {
 9         Scanner scanner = new Scanner(System.in);
10 
11         //是否还有其他行,一次可以测试多行
12         while(scanner.hasNext()){
13             String input = scanner.nextLine();
14             System.out.println(findLastLength(input));
15         }
16         scanner.close();
17     }
18 
19     public static int findLastLength(String input) {
20         //最后一个字母的位置
21         int last = input.length()-1;
22         //找最后一个字母出现的位置
23         while (last>=0 && input.charAt(last)==' '){
24             last--;
25         }
26         //找到最后一个字母之前的第一个空白字符
27         int pos = last-1;
28         while (pos>=0&&input.charAt(pos)!=' '){
29             pos--;
30         }
31 
32         return last-pos;
33     }
34 }

题2

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by huststl on 2018/3/11 22:38
 5  * 写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,
 6  * 然后输出输入字符串中含有该字符的个数。不区分大小写。
 7  */
 8 public class demo02 {
 9     public static void main(String[] args) {
10         Scanner scanner = new Scanner(System.in);
11 
12         while(scanner.hasNext()){
13             String str = scanner.next();
14             String ch = scanner.next();
15             System.out.println(countCharNumber(str,ch));
16 
17         }
18     }
19 
20     private static int  countCharNumber(String str, String chStr) {
21         char ch = 0;
22         for(int i=0;i<chStr.length();i++){
23             if(chStr.charAt(i)!=' '){
24                 ch = chStr.charAt(i);
25                 break;
26             }
27         }
28 
29         ch = toUppercase(ch);
30         int count = 0;
31         for(int i=0;i<str.length();i++){
32             if(toUppercase(str.charAt(i))==ch){
33                 count++;
34             }
35         }
36         return count;
37 
38     }
39 
40     private static char toUppercase(char ch) {
41         if(ch>='a'&& ch<='z'){
42             return (char)('A'+(ch-'a'));
43         }
44         return ch;
45     }
46 
47 }

题3

 1 import java.util.Scanner;
 2 import java.util.Set;
 3 import java.util.TreeSet;
 4 
 5 /**
 6  * Created by huststl on 2018/3/12 8:35
 7  * 输入多行数字,进行去重和排序
 8  */
 9 public class demo03 {
10     public static void main(String[] args) {
11         Scanner scanner = new Scanner(System.in);
12 
13         while (scanner.hasNext()){
14             //读取输入的数字数
15             int num = scanner.nextInt();
16             //存放输入的数字
17             Set<Integer> set = new TreeSet<>();
18             while ((--num)>=0){
19                 set.add(scanner.nextInt());
20             }
21             System.out.println(setToString(set));
22         }
23 
24     }
25 
26     private static String setToString(Set<Integer> set)
27     {
28         StringBuilder builder = new StringBuilder(128);
29         for(Integer i:set){
30             builder.append(i).append("\n");
31         }
32         return builder.toString();
33     }
34 }

题4:

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by huststl on 2018/3/12 8:45
 5  * 连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
 6  •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
 7  */
 8 public class demo04 {
 9     public static void main(String[] args) {
10         Scanner sc= new Scanner(System.in);
11         /*StringBuilder builder = new StringBuilder(256);
12         while (scanner.hasNext()){
13             builder.setLength(0);
14             String input = scanner.nextLine();
15             stringSplit(builder,input);
16             input = scanner.nextLine();
17             stringSplit(builder,input);
18             System.out.println(builder);
19         }*/
20         while (sc.hasNext()){
21             String str = new String(sc.nextLine());
22             if(str.length()%8!=0){
23                 str = str +"00000000";
24 
25             }
26             while (str.length()>8){
27                 System.out.println(str.substring(0,8));
28                 str = str.substring(8);
29 
30             }
31         }
32 
33     }
34 
35     /*private static void stringSplit(StringBuilder builder, String str) {
36         if(str == null || str.length()<1){
37             return;
38         }
39         int pos = 0;
40         while((pos+8)<str.length()){
41             builder.append(str.substring(pos-8,pos)).append("\n");
42         }
43 
44         //如果str.length()<pos 说明最后字符不足8个或刚好8个
45         if(str.length()<=pos){
46             builder.append(str.substring(pos-8,str.length()));
47             for(int i=str.length();i<pos;i++){
48                 builder.append(0);
49             }
50             builder.append("\n");
51         }
52     }*/
53 }

题5:

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by huststl on 2018/3/12 9:21
 5  * 进制转换(输入16进制输出10进制)
 6  */
 7 public class demo05 {
 8     public static void main(String[] args) {
 9         Scanner sc = new Scanner(System.in);
10         while (sc.hasNext()){//多个测试用例用while(sc.hasNext()){}
11             String str = sc.next().substring(2);
12             System.out.println(Integer.parseInt(str,16));
13             /*System.out.println(hexToDec(str));*/
14         }
15     }
16 
17    /* private static int hexToDec(String str) {
18         final int BASE = 16;
19         int result = 0;
20 
21         for(int i=2;i<str.length();i++){
22             result = result * BASE + hexToNum(str.charAt(i));
23         }
24         return result;
25     }*/
26 
27     /*private static int hexToNum(char c) {
28 
29         if(c >='0' && c<='9'){
30             return c-'0';
31         }else if(c>='a'&& c<='z'){
32             return c -'a'+10;
33         }else {
34             return c-'A'+10;
35         }
36     }*/
37 }

题6:

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by huststl on 2018/3/12 9:34
 5  * 质子因子
 6  * :输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )
 7  */
 8 public class demo06 {
 9     public static void main(String[] args) {
10 
11         Scanner sc = new Scanner(System.in);
12 
13         while (sc.hasNext()){
14             long num = sc.nextLong();
15             System.out.println(findPrime(num));
16         }
17 
18     }
19 
20     private static String findPrime(long num) {
21 
22         StringBuilder builder = new StringBuilder(128);
23 
24         long i = 2;
25 
26         while (i<=num){
27             //每次的i一定是质数才会满足
28             while (num%i==0){
29                 builder.append(i).append(' ');
30                 num /=i;
31             }
32             i++;
33         }
34         return builder.toString();
35     }
36 }

题7:

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by huststl on 2018/3/12 9:46
 5  * 取近似值
 6  * 写出一个程序,接受一个浮点数值,输出该数值的近似整数值。
 7  * 如果小数点后数值大于等于5,向上取整;小于5,则向下取整。
 8  */
 9 public class demo007 {
10     public static void main(String[] args) {
11         Scanner sc = new Scanner(System.in);
12 
13         while (sc.hasNext()){
14             float num = sc.nextFloat();
15             System.out.println(floatToNearInt(num));
16         }
17     }
18 
19     private static int floatToNearInt(float num) {
20         //取整的计算方式
21         return (int) ((num*10+5)/10);
22     }
23 }

题8:

 1 import java.util.Scanner;
 2 import java.util.SortedMap;
 3 import java.util.TreeMap;
 4 
 5 /**
 6  * Created by huststl on 2018/3/12 9:52
 7  * 数据表记录包含表索引和数值,请对表索引相同的记录进行合并,
 8  * 即将相同索引的数值进行求和运算,输出按照key值升序进行输出。
 9  */
10 public class demo008 {
11     public static void main(String[] args) {
12         Scanner scan =new Scanner(System.in);
13 
14         while (scan.hasNext()){
15             int pares = Integer.parseInt(scan.nextLine());
16             SortedMap<Integer,Integer> map = new TreeMap<>();
17             for(int i=0;i<pares;i++){
18                 String[] nums = scan.nextLine().split("\\s+");
19                 addPare(map,nums);
20             }
21             System.out.println(mapToString(map));
22         }
23     }
24 
25     private static String mapToString(SortedMap<?, ?> map) {
26         StringBuilder builder = new StringBuilder();
27 
28         for(SortedMap.Entry<?,?> e:map.entrySet()){
29             builder.append(e.getKey()).append(" ").append(e.getValue()
30             ).append("\n");
31         }
32 
33         return builder.toString();
34     }
35 
36 
37     private static void addPare(SortedMap<Integer, Integer> map, String[] nums) {
38 
39         int key = Integer.parseInt(nums[0]);
40         int val = Integer.parseInt(nums[1]);
41 
42         if(map.containsKey(key)){
43             map.put(key,map.get(key)+val);
44         }else {
45             map.put(key,val);
46         }
47     }
48 }

题9:

 1 import java.util.HashSet;
 2 import java.util.LinkedHashSet;
 3 import java.util.Scanner;
 4 
 5 /**
 6  * Created by huststl on 2018/3/12 10:10
 7  * 提取不重复的整数
 8  * 输入一个int型整数,
 9  * 按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
10  */
11 public class demo009 {
12     public static void main(String[] args) {
13         Scanner scanner = new Scanner(System.in);
14 
15         while (scanner.hasNext()){
16             /*int num = scanner.nextInt();
17             System.out.println(extractNumber(num+""));*/
18             String str = scanner.nextLine();
19 
20             int len = str.length();
21 
22             int[] arr1 = new int[10];
23 
24             for(int i=len-1;i>=0;i--){
25                 if(arr1[str.charAt(i)-48]==0){
26                     System.out.println(str.charAt(i)-48);
27                     //字符与整数之间差48
28                     arr1[str.charAt(i)-48]++;
29                 }
30             }
31 
32         }
33 
34     }
35 
36     /*private static String extractNumber(String s) {
37 
38         StringBuilder builder = new StringBuilder();
39 
40         HashSet<Character> set = new LinkedHashSet<>();
41         for(int i=s.length()-1;i>=0;i++){
42             set.add(s.charAt(i));
43         }
44         for(Character c:set){
45             builder.append(c);
46         }
47 
48         //如果第一个字符是0
49         if(builder.charAt(0)=='0'){
50             return builder.substring(1,builder.length());
51         }
52 
53         return builder.toString();
54     }*/
55 }

题10:

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by huststl on 2018/3/12 10:30
 5  * 010--字符个数统计
 6  * 编写一个函数,计算字符串中含有的不同字符的个数。
 7  * 字符在ACSII码范围内(0~127)。不在范围内的不作统计。
 8  */
 9 public class demo10 {
10     public static void main(String[] args) {
11         Scanner scanner = new Scanner(System.in);
12         while (scanner.hasNext()){
13             int[] arr = new int[128];
14             String str = scanner.nextLine();
15             countChar(arr,str);
16             System.out.println(countCharNum(arr));
17         }
18     }
19 
20     private static int countCharNum(int[] arr) {
21         int result = 0;
22         for(int i:arr){
23             if(i!=0){
24                 result++;
25             }
26         }
27         return result;
28     }
29 
30     private static void countChar(int[] arr, String str) {
31         for(int i=0;i<str.length();i++){
32             char c = str.charAt(i);
33             if(c<=127){
34                 arr[c]++;
35             }
36         }
37     }
38 }

 

posted @ 2018-03-12 10:57  huster-stl  阅读(627)  评论(0编辑  收藏  举报