您的位置:首页 > 编程语言 > Java开发

华为机试准备--练习(JAVA实现)

2013-08-30 00:50 381 查看
题1---- 选秀节目打分,分为专家评委和大众评委,score[] 数组里面存储每个评委打的分数,judge_type[] 里存储与 score[] 数组对应的评委类别,judge_type[i] == 1,表示专家评委,judge_type[i] == 2,表示大众评委,n表示评委总数。打分规则如下:专家评委和大众评委的分数先分别取一个平均分(平均分取整),然后,总分 = 专家评委平均分  * 0.6 + 大众评委 * 0.4,总分取整。如果没有大众评委,则 总分 = 专家评委平均分,总分取整。函数最终返回选手得分。
    函数接口   int cal_score(int score[], int judge_type[], int n) 

 

public class HuaWeiTest4{
public static void main(String[] args){
int[] score = {80, 70, 85, 88, 90};
int[] judge_type = {1,1,2,2,2};
HuaWeiTest4 test = new HuaWeiTest4();
int last_score = test.cal_score(score, judge_type,5);
System.out.println("最终得分是:"+last_score);
}
public int cal_score(int score[], int judge_type[], int n){
int totalExpert = 0;
int totalPublic = 0;
int numExpert = 0;
int numPublic = 0;
int result = 0;
for(int i=0; i<n; i++){
if(judge_type[i] == 1){
totalExpert += score[i];
numExpert++;
}
if(judge_type[i] == 2){
totalPublic += score[i];
numPublic++;
}
}
if(0 == numPublic)
result = totalExpert  / numExpert;
else
result = (int)((totalExpert * 1.0 / numExpert)*0.6 + (totalPublic*1.0/numPublic)*0.4);
return result;
}
}

编写一个算法,过滤掉数组中的非法字符,最终只剩下正式字符。
      示例:输入数组:“!¥@&HuaWei*&%123”

              调用函数后的输出结果,数组:“HuaWei123”。

函数声明:

public static void getFormatString(String s)

public class Test2{
public static void main(String[] args){
Test2 test = new Test2();
String str ="!¥@&HuaWei*&%123";
String res = test.formatString(str);
System.out.println("过滤后的字符串是:" + res);
}
public String formatString(String str){
int StrLength = str.length();
String result ="";
for(int i=0; i<StrLength; i++){
if((str.charAt(i)>='A'&&str.charAt(i)<='Z') ||
(str.charAt(i)>= 'a'&&str.charAt(i)<='z') ||
(str.charAt(i)>='0'&&str.charAt(i)<='9'))
result += str.charAt(i);
}
return result;
}
}


 

判断一个数是否回文数,如果1221,232, 3;
【输入】:一个整型数iNumber
【输出】:0: iNumber不是回文数
          1:iNumber是回文数

import java.util.Scanner;

public class Test1{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要判断的字符串:");
String str = sc.nextLine();
Test1 test = new Test1();
test.isHuiWen(str);
}
public void isHuiWen(String str){
int StrLength = str.length();
int count = 0;
boolean isYes = true;
while(isYes){
if(str.charAt(count+0) != str.charAt(StrLength -count-1))
isYes = false;
count++;
if(count >= StrLength/2){
System.out.println(str+"是回文字符串");
isYes = false;
}
}
if(count < StrLength / 2)
System.out.println(str+"不是回文字符串");
}
}


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