华为机试题记录1

import java.math.BigInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
public class Main {

public static void main(String args[]){

//isSameTree();
//selectSeven();
//namePretty();
//treeBehind();
//nikechesi();
//recoverTree();
//biggestScore();
//songQuene1();
//printDiffChar();
//bigInt();
//gdc();
//zeroCount();
//picsort();
//delLeast();
//bottle();
//test();
//move();
//arraySort();
//money();
//separated();
//change();
//card();

//findMoney();
wordSort()	;
}

public static void isSameTree(){

Scanner s= new Scanner(System.in);
//判断个数
int num = Integer.valueOf(s.nextLine());
//第一个序列
String firstTreeStr = s.nextLine();
//得到第一个序列的两个子串，第一个子串全部小于串中第一个数字，第二个子串全部大于第一个数字，顺序和原串相同

String subFirstTreeStrSmall = devideTree( firstTreeStr,1);
String subFirstTreeStrBig = devideTree( firstTreeStr,2);

//		System.out.println(subFirstTreeStrSmall);
//		System.out.println(subFirstTreeStrBig);
String treeStr = s.nextLine();
String subTreeStrSmall = devideTree( treeStr,1);
String subTreeStrBig = devideTree( treeStr,2);

ArrayList res = new ArrayList();

while(!treeStr.equals("0")){

if(subTreeStrSmall.equals(subFirstTreeStrSmall)&&subTreeStrBig.equals(subFirstTreeStrBig))
//System.out.println("YES");

treeStr = s.nextLine();
subTreeStrSmall = devideTree( treeStr,1);
subTreeStrBig = devideTree( treeStr,2);
}

for(int i=0;i<num;i++){

System.out.println(res.get(i));

}

}

//分隔串函数，int i 代表，子串大于或小于第一个数字i小于，2大于
public static String  devideTree(String treeStr,int i){

String subStr = "";
int length = treeStr.length();

int firstStr = Integer.valueOf(treeStr.charAt(0)+"");
if(i==1){

for(int x = 1;x<length;x++){

int nextStr = Integer.valueOf(treeStr.charAt(x)+"");
if(nextStr<firstStr)
subStr+=nextStr;

}
}else if(i==2){
for(int x = 1;x<length;x++){

int nextStr = Integer.valueOf(treeStr.charAt(x)+"");
if(nextStr>firstStr)
subStr+=nextStr;

}
}

return subStr;

}

//挑7
public static void selectSeven(){

Scanner s = new Scanner(System.in);

int num = s.nextInt();

int count=0;

for(int i = 1;i<num;i++){

if(i%7==0) {

count++;
continue;

}

if(String.valueOf(i).contains("7")) {

count++;
continue;

}

}

System.out.println(count);

}

//名字的漂亮度

public static void namePretty(){

Scanner s = new Scanner(System.in);

int num = Integer.valueOf(s.nextLine());

int sum = 0 ;

HashMap<String ,Integer> hm = new HashMap<String ,Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
String resStr = "";
while(sum!=num){
hm.clear();
list.clear();
String name = s.nextLine();

for(int i = 0;i<name.length();i++){

String str = name.charAt(i)+"";
if(hm.containsKey(str))
hm.put(str, hm.get(str)+1);
else hm.put(str, 1);

}

//System.out.println(hm);

Collections.sort(list);

//System.out.println(list);

int digital = 27-list.size();
int result = 0;
for(int x:list){

result+= x*digital;
digital++;

}
resStr+=result+"\n";
sum++;
}

System.out.println(resStr);

}

//根据树的前序中序，输出后序

public static void treeBehind(){

Scanner n = new Scanner(System.in);

while(true){

String front = n.nextLine();
String middle = n.nextLine();

printTree(front,middle);
System.out.println();

}

}
public static String res="";
public static void printTree(String front,String middle){

int flength = front.length();
int mlength = middle.length();
//递归结束条件，当传入字符为空或者只有一个字符
if(flength==0) return;

else if(flength==1) {

//System.out.print(front);
res+=front;
return;

}

//在前序序列中找到树的根,为前序序列的第一个字符
String root = front.charAt(0)+"";
//在中序序列中找到根的位置，按照根的位置分隔前序和后序字符串，然后将分隔的左子串和右子串传入递归方法
int mindex = middle.indexOf(root);
if(mindex==-1)return;
String subMidLeft = middle.substring(0, mindex);
String subMidRight = middle.substring(mindex+1, mlength);

String subFroLeft = front.substring(1, mindex+1);
String subFroRight = front.substring(mindex+1, mlength);

//输出左子树
printTree(subFroLeft,subMidLeft);
//输出右子树
printTree(subFroRight,subMidRight);
//输出根
//System.out.print(root);
res+=root;
}

//尼科彻斯定理
public static void nikechesi(){

Scanner n = new Scanner(System.in);

int num = n.nextInt();

if(num<=1||num>=100){
System.out.println("-1");
return;
}
int i =0,j=0;
for(i=(num*(num-1)+1);j<num-1;i+=2,j++){

System.out.print(i+"+");

}

System.out.print(i);

}

public static void recoverTree(){

Scanner n = new Scanner(System.in);

while(true){
int num = n.nextInt();
String front = n.next();
String middle = n.next();

printTree(front,middle);
//System.out.println(res);
if(res.length()==num)
System.out.println(res);
else System.out.println("No");
res="";

}

}

//最高分是多少
public static void biggestScore(){

Scanner n = new Scanner(System.in);

//第一行输入
String[] firstLine = n.nextLine().split(" ");
//学生成绩总数 操作总数
int stuNum = Integer.valueOf(firstLine[0]);
int funNum = Integer.valueOf(firstLine[1]);
//学生成绩放到int数组中
String score = n.nextLine();

String scoreArray[] = score.split(" ");

String res = "";

for(int i = 0;i<funNum;i++){

String[] fun = n.nextLine().split(" ");

//获取操作数的第一，第二，第三个字符
String a = fun[0];
int b = Integer.valueOf(fun[1]);
int c = Integer.valueOf(fun[2]);

if(a.equals("Q")){

int big = Integer.valueOf(scoreArray[b]);

while(b>=stuNum) b--;//控制越界
for(int j = b;j<=c;j++){

if(Integer.valueOf(scoreArray[j])>big){

big = Integer.valueOf(scoreArray[j]);

}
}
//res+=big+"\n";

System.out.println(big);

}else if(a.equals("U")){

scoreArray[b]=String.valueOf(c);

}
}

//System.out.print(res);

}
//合唱队
public static void songQuene(){

Scanner n = new Scanner(System.in);

int num = Integer.valueOf(n.nextLine());
String high = n.nextLine();

String [] highArray = high.split(" ");

int a[] = new int[num];
int b[] = new int[num];

for(int i=0;i<num;i++){
a[1]=1;
for(int j = 0;j<i;j++){
if(Integer.valueOf(highArray[i])>Integer.valueOf(highArray[j]))
a[i]+=1;
}
}

for(int i=0;i<num;i++){
b[1]=1;
for(int j = i+1;j<num;j++){
if(Integer.valueOf(highArray[i])>Integer.valueOf(highArray[j]))
b[i]+=1;
}
}
int res=0;
for(int i=0;i<num;i++){
if(a[i]+b[i]>res)

res=a[i]+b[i];

}

System.out.println(res);
}

//合唱队
public static void songQuene1(){

Scanner n = new Scanner(System.in);

int num = Integer.valueOf(n.nextLine());
String high = n.nextLine();

String [] highArray = high.split(" ");

int a[] = new int[num];
int b[] = new int[num];

for(int i=0;i<num;i++){
a[i]=1;
for(int j = 0;j<i;j++){
if(Integer.valueOf(highArray[i])>Integer.valueOf(highArray[j])&&(a[i]<a[j]+1))
a[i]+=1;
}
}

for(int i=num-1;i>-1;i--){
b[i]=1;
for(int j = num-1;j>i;j--){
if(Integer.valueOf(highArray[i])>Integer.valueOf(highArray[j])&&(b[i]<b[j]+1))
b[i]+=1;
}
}
int res=0;
for(int i=0;i<num;i++){
if(a[i]+b[i]-1>res)

res=a[i]+b[i]-1;
System.out.println(a[i]+"   " +b[i]+"   " +highArray[i]);

}

System.out.println(num-res);
}

//将字符串中不同字符的个数打印出来
public static void printDiffChar() {

Scanner n = new Scanner(System.in);

String str = n.nextLine();

ArrayList list = new ArrayList();

for(int i=0;i<str.length();i++){

String s = str.charAt(i)+"";

}

System.out.println(list.size());

}

public static void bigInt(){
Scanner n = new Scanner(System.in);

String a= n.nextLine();

String b = n.nextLine();

BigInteger i = new BigInteger(a);
BigInteger j = new BigInteger(b);

System.out.println(x);

}

//打印非素数
public static void findNoPrime(){

Scanner n = new Scanner(System.in);

int num = n.nextInt();
String result="";

for(int i = 4;i<=num;i++){

for(int j=2;j*j<=i;j++){

if(i%j==0){

result+=i+" ";
break;

}

}

}

System.out.println(result);

}
//打印最大公约数
public static void gdc(){

Scanner n = new Scanner(System.in);
int a = Integer.parseInt(n.next());
int b=Integer.parseInt(n.next());

//					System.out.println(a);
//					System.out.println(b);

//控制让a>b
int  temp=0;
if(a<b){
temp=a;
a=b;
b=temp;
}
int c=0;
while(b!=0){

c=b;
b=a%b;
a=c;
}
if(a<0)a=-a;
System.out.println(a);

}

//二进制数中零的个数

public static void zeroCount(){

Scanner n = new Scanner(System.in);

int a = n.nextInt();
int flag=0;

int count=0;
int pos=0;
for(int i=0;i<32;i++){

flag=a&1;
if(flag==0) count++;
if(flag==1) pos=i;
a=a>>1;
}

//System.out.println("count="+count+"    pos"+pos);
int result=count-(32-(pos+1));
System.out.println(result);
}

//图片整理
public static void picsort(){

Scanner n = new Scanner(System.in);
String s = n.nextLine();

char[] c=s.toCharArray();

Arrays.sort(c);

for(int i=0;i<c.length;i++)

System.out.print(c[i]);

}

//删除字符串中出现次数最少的字符
public static void  delLeast(){

Scanner n = new Scanner(System.in);

String s = n.nextLine();

int []a = new int[26];
for(int i=0;i<s.length();i++){

int j=(s.charAt(i)-'a');//int j=s.charAt(i)-'a';完全是两回事儿

a[j]++;

}
int min=20;//这里不能是min=a[0]有可能a[0]=0
for(int i=0;i<26;i++){

if(a[i]<min&&a[i]!=0) min=a[i];//一定要加上a[i]!=0

}

//System.out.println(min);
for(int i=0;i<s.length();i++){

char c = s.charAt(i);

int x=(c-'a');
//System.out.println(x);
if(a[x]>min) System.out.print(c);
}

}

//汽水瓶
public static void bottle(){

Scanner sn = new Scanner(System.in);

int n=sn.nextInt();
while(n!=0){
int count=0;
int b=0;int c=0;
while(n>1){

if(n==2) {
count+=1;
break;
}
else{
b=n/3;
c=n%3;
count+=b;
n=b+c;
}
}

System.out.println(count);
n=sn.nextInt();
}

}

public static void test(){

Scanner n=new Scanner(System.in);
while(n.hasNext()){

int str = n.nextInt();

//System.out.println(str.replaceAll("a",""));
System.out.println(str);
}

}

//坐标移动
public static void move(){

Scanner n=new Scanner(System.in);

String str = n.nextLine();

String array[] = str.split(";");

int x=0;int y=0;

for(int i=0;i<array.length;i++){
int orderLength = array[i].length();

if(orderLength==3||orderLength==2){

//System.out.println(array[i]);

String firS = array[i].charAt(0)+"";

String secS="";
int sec1=48;
int sec2=48;
if(orderLength==3){
secS = array[i].substring(1,3);
sec1 = (secS.charAt(0)+"").hashCode();
sec2 = (secS.charAt(1)+"").hashCode();

}

else if(orderLength==2){
secS = array[i].substring(1,2);
sec1 = (secS.charAt(0)+"").hashCode();
}

//System.out.println(sec1+"   "+sec2);
//A10;S20;W10;D30;X;A1A;B10A11;;A10;
//if(firS.matches("A|D|W|S")&&sec1>=48&&sec1<=57&&sec2>=48&&sec2<=57){
if(sec1>=48&&sec1<=57&&sec2>=48&&sec2<=57&&firS.equals("A")||firS.equals("D")||firS.equals("S")||firS.equals("W")){
//System.out.println("-----------"+secS);
if(firS.equals("A")){
x-=Integer.parseInt(secS);
}else if(firS.equals("D")){
x+=Integer.parseInt(secS);
}else if(firS.equals("W")){
y+=Integer.parseInt(secS);
}else if(firS.equals("S")){
y-=Integer.parseInt(secS);
}
}
}

}
System.out.println(x+","+y);

}

//整数排序
public static void arraySort(){

Scanner n = new Scanner(System.in);
String str = n.nextLine();

String array[] = str.split(",");
//需要把字符型数组转成整形数组
int iarray[] = new int[array.length];

for(int x=0;x<array.length;x++){
iarray[x]=Integer.parseInt(array[x]);
}

Arrays.sort(iarray);

int i=0,j=1,a=0,b=0;
while(j<iarray.length){
a=iarray[i];
b=iarray[j];

int step=1;
while(b==a+step) {

j++;
step++;
if(j==iarray.length) break;
b=iarray[j];

}

if(j==i+1) {

System.out.print(iarray[i]+" ");

}
else{
System.out.print(iarray[i]+" "+iarray[j-1]+" ");
if(j==iarray.length-1){
System.out.print(iarray[j]+" ");
break;
}

}
i=j;
j=i+1;
}

}

//整数排序2
public static void arraySort2(){

Scanner n = new Scanner(System.in);
String str = n.nextLine();

String array[] = str.split(",");
//需要把字符型数组转成整形数组
int iarray[] = new int[array.length];

for(int x=0;x<array.length;x++){
iarray[x]=Integer.parseInt(array[x]);
}

Arrays.sort(iarray);
String result=iarray[0]+" ";
int i=1;
while(i<iarray.length-1){

if(!((iarray[i]==iarray[i-1]+1)&&(iarray[i+1]==iarray[i]+1)))

result+=iarray[i]+" ";
i++;

}
result+=iarray[i]+" ";
System.out.println(result);

}

//找零钱
public static void money(){

int n;
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int s;

Scanner sn = new Scanner(System.in);
n=sn.nextInt();
while(n!=0)
{

s=0;
if(n==0)
{
break;
}
if (n<1 || n>250)
{
continue;
}
for(a=0;a<=n/100;a++)
for(b=0;b<=n/50;b++)
for(c=0;c<=n/20;c++)
for(d=0;d<=n/10;d++)
for(e=0;e<=n/5;e++)
for(f=0;f<=n/2;f++)
for(g=0;g<=n;g++)
{
if( (n==a*100+b*50+c*20+d*10+e*5+f*2+g) && (a+b+c+d+e+f+g<=100) )
{
s++;
break;
}

}
System.out.println(s);
n=sn.nextInt();
}

}

//整数分隔
public static void separated(){

Scanner n = new Scanner(System.in);

while(true){

int num = n.nextInt();

if(num>=1&&num<=1000000)

System.out.println(ff(num)%1000000000);
else System.out.println(-1);
}
}
//这个方法栈溢出
public static int f(int n){

int result=0;
if(n==1) result=1;
else if(n%2==0) result=(f(n-1)+f(n/2))%1000000000;
else if(n%2==1) result=(f(n-1))%1000000000;

return result;
}

//利用数组存放所有结果
public static int ff(int n){

int result[]=new int[1000001];

for(int i=1;i<1000001;i++){
result[1]=1;
if(i%2==0) result[i]=(result[i-1]+result[i/2])%1000000000;
else if(i%2==1) result[i]=(result[i-1])%1000000000;

}

return result[n];
}

//字符转换
public static void change(){

Scanner n = new Scanner(System.in);

String str = n.nextLine();

for(int i=0;i<str.length();i++){

String s = str.charAt(i)+"";

int x = s.hashCode();

//System.out.println(x);
if(x>=65&&x<=85) s=String.valueOf((char)(x+37));
if(x>=86&&x<=90) s=String.valueOf((char)(x+11));

System.out.print(s);

}

}

//扑克牌大小
public static void card(){

Scanner n = new Scanner(System.in);

String str = n.nextLine();

int index = str.indexOf("-");

String aa[] = str.substring(0,index).split(" ");
String bb[] = str.substring(index+1,str.length()).split(" ");

int a[] = changeCard(aa);
int b[] = changeCard(bb);

int al = a.length;
int bl=b.length;

if(al==2){
if(a[0]==16||a[0]==17){
System.out.println(str(aa));
return;
}else if(bl==4){
System.out.println(str(bb));
return;
}else if(bl==2){

if(a[0]>b[0]) System.out.println(str(aa));
else if(a[0]<b[0]) System.out.println(str(bb));
return;

}else  {
System.out.println("ERROR");
return;
}
}
else if(al==4){
if(bl==2&&(a[0]==16||a[0]==17)){
//System.out.println("here");
//System.out.println(str(bb));
return;
}else if(al==4){
if(a[0]>b[0]) System.out.println(str(aa));
else if(a[0]<b[0]) System.out.println(str(bb));
return;
}else {
System.out.println(str(aa));
return;
}
}else if(al==bl){
//System.out.println("here");
System.out.println(a[0]+" "+b[0]);
if(a[0]>b[0]) System.out.println(str(aa));
else if(a[0]<b[0]) System.out.println(str(bb));
return;
}else{
System.out.println("ERROR");
return;
}

}

public static String str(String s[]){
String result="";
for(int i=0;i<s.length;i++){
result+=s[i]+" ";

}

return result;
}

public static int[] changeCard(String s[]){

int r[] = new int[s.length];

for(int i=0;i<s.length;i++){
if(s[i].equals("A")) r[i]=14;
else if(s[i].equals("2")) r[i]=15;
else if(s[i].equals("J")) r[i]=11;
else if(s[i].equals("Q")) r[i]=12;
else if(s[i].equals("K")) r[i]=13;
else if(s[i].equals("joker")) r[i]=16;
else if(s[i].equals("JOKER")) r[i]=17;
else r[i]=Integer.parseInt(s[i]);

}

return r;

}
static int a[] = {1,2,5,10,20,50,100};
public static void findMoney(){

Scanner sc = new Scanner(System.in);
int x= sc.nextInt();
while(x!=0){
int i=0;
for(;i<7&&a[i]<=x;i++);
i--;
//System.out.println(i);
System.out.println(getI(x,i));

x=sc.nextInt();

}

}

public static int getI(int x,int i){
if(x<=1||a[i]==1)  return 1;

else return getI(x-a[i],i)+getI(x,i-1);

}

//英文排序，其他不变
public static void wordSort()
{

Scanner n = new Scanner(System.in);

while(n.hasNext()){

String str = n.nextLine();
ArrayList<Character> list = new ArrayList();

char[] strArray = str.toCharArray();

for(int i=0;i<strArray.length;i++){

if((strArray[i]>='a'&&strArray[i]<='z')||(strArray[i]>='A'&&strArray[i]<='Z')){

strArray[i]='\0';
}

}

Collections.sort(list);
System.out.println(list);
int j=0;
String result="";
for(int i=0;i<strArray.length;i++){

if(strArray[i]=='\0'){

strArray[i]=list.get(j);
j++;

}
result+=strArray[i];
System.out.println(i);
}

System.out.println(result);

}

}

}


• 本文已收录于以下专栏：

2016年华为机试题

• smallfish_love
• 2016年08月24日 10:55
• 2049

牛客华为机试题刷题笔记（一）

• zhangxiao93
• 2017年06月26日 21:03
• 922

2015华为上机练习《朋友圈转发消息》

• fanjiaxia
• 2014年09月10日 19:50
• 376

华为机试第一题[2016年8月18日上午]计算重复字符

• wdasdaw
• 2016年08月18日 17:32
• 526

华为OJ机试训练（一）

• linfeng24
• 2014年06月23日 22:01
• 2539

2015届华为校园招聘机试题

• Hackbuteer1
• 2014年09月13日 18:25
• 56188

2017华为机试题--任务调度

• Jocelyn92
• 2017年04月10日 09:21
• 636

2014届华为校园招聘机试题

2014年华为校园招聘机试题 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序，若字符串中出现多个相同的字符，将非首次出现的字符过滤掉 通过键盘输入一串小写字母(a~z)组成的...
• Hackbuteer1
• 2013年09月05日 14:14
• 42905

华为笔试题 明明的随机数

• lezong2011
• 2016年04月08日 10:20
• 300

[华为机试练习题]44.24点游戏算法

• SunnyYoona
• 2015年07月03日 22:27
• 2021

举报原因： 您举报文章：华为机试题记录1 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)