2016年华为校园招聘算法题目

匿名网友 匿名网友 发布于: 2015-10-23 00:00:00
阅读 201 收藏 0 点赞 0 评论 0

1. 将数字进行翻转,如输入1234,输入4321,需考虑是负数的情况
public class FanZhuan {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean isNegative = false;
int i = scanner.nextInt();
scanner.close();
if (i <0) {//判断是否是负数 i = -i; isNegative = true; } String string = ""; while(i!=0){ string += i%10;//每次取余数,拼接上 i = i/10; } int a = Integer.parseInt(string);//在转换成int型 System.out.println(isNegative ? -a : a); } } 2. 输入一个字符串,输出重复的字符 public class RepeatOut { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String string = scanner.next(); scanner.close(); char a [ ] = string.toCharArray(); int b [ ] = new int[26*2]; for (int i = 0; i < a.length; i++) { if ( b[a[i]-65] == 1) { System.out.print(a[i]); b[a[i]-65]++; }else if(b[a[i]-65] == 0){ b[a[i]-65]++; } } } } 3. 将输入的字符串中的数字进行排序,例如,输入29shf23 ,输出2239 public class SortNum { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String string = scanner.nextLine(); ArrayList arrayList = new ArrayList();
scanner.close();
for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i);//依次取出每个字符 if (Character.isDigit(ch)) {//判断是否是数字 arrayList.add(string.charAt(i) + "");//用ArrayList总感觉大材小用了,好浪费呀. } } Collections.sort(arrayList);//排序 for (int i = 0; i < arrayList.size(); i++) { System.out.print( arrayList.get(i) ); } } } 4. 字符串中挑数字并排序, 输入: abzxcd12efgdhi543fexy,输出: 12345 public class TiaoNumAndSort { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // System.out.println("请输入字符串:"); String string = scanner.nextLine(); scanner.close(); String s = ""; for (int i = 0; i < string.length(); i++) {//先把数组取出来并拼接到成一个字符串 char c = string.charAt(i); if( !(c >=’A’ && c<='z') ){ s +=c; } } char b[] = s.toCharArray();//将数字字符串转成数组 char tmp; for (int i = 0; i < b.length; i++) { for (int j = i+1; j < b.length; j++) { if(b[i]>b[j]){
tmp = b[i];
b[i] = b[j];
b[j] = tmp;
}
}
}

for (int i = 0; i < b.length; i++) { System.out.print(b[i]); } } } 5. Lily上课时使用字母数字图片教小朋友们学习英语单词,每次都需要把这些图片按照大小(ASCII码值从小到大)排列收好。请大家给Lily帮忙输入: Lily使用的图片包括"A"到"Z"、"a"到"z"、"0"到"9"。输入字母或数字个数不超过1024。输出 Lily的所有图片按照从小到大的顺序输出样例输入 Ihave1nose2hands10fingers 样例输出 0112Iaadeeefghhinnnorsssv public class ZhengLi { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); scanner.close(); char m[] = s.toCharArray(); int a[] = new int[10]; int b[] = new int[36*2]; for (int i = 0; i < m.length; i++) { if( ('0' <= m[i]) && (m[i] <= '9') ){ a[m[i]-48]++; }else { b[m[i]-65]++; } } for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i]; j++) { System.out.print( (char)(i+48) ); } } for (int i = 0; i < b.length; i++) { for (int j = 0; j < b[i]; j++) { System.out.print( (char)(i+65) ); } } } } 6. 输出一个字符串最后一个单词的长度。 public class ZiFuChuan { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); scanner.close(); String a[] = s.split(" "); String b = a[a.length-1]; System.out.println(b.length()); } }

评论列表
文章目录