第一题:
输入:一个整数n(n可能很大)
输出:例如 n = 789 ; f(n) = 7+8+9 = 24; f(n) = 2+4 = 6;此时f(n)只有1位,输出f(n)
样例输入:4546313123
样例输出:5
我的答案:
<span style="font-size:18px;">package 华为笔试;
import java.math.BigInteger;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
T1 t1 = new T1();
}
}
class T1{
String aString;
public T1(){
Scanner sca = new Scanner(System.in);
while(sca.hasNext()){
aString = sca.next();
while(aString.length()!=1){
aString = change(aString);
}
System.out.println((int)(aString.charAt(0)-'0'));
}
}
public String change(String tmp){
char[] cs = tmp.toCharArray();
BigInteger tInteger = new BigInteger("0");
for(int i=0;i<cs.length;i++){
int r = cs[i]-'0';
tInteger = tInteger.add(new BigInteger(r+""));
}
return tInteger.toString();
}
}</span>
package 华为笔试;
import java.math.BigInteger;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
T1 t1 = new T1();
}
}
class T1{
String aString;
public T1(){
Scanner sca = new Scanner(System.in);
while(sca.hasNext()){
aString = sca.next();
while(aString.length()!=1){
aString = change(aString);
}
System.out.println((int)(aString.charAt(0)-'0'));
}
}
public String change(String tmp){
char[] cs = tmp.toCharArray();
BigInteger tInteger = new BigInteger("0");
for(int i=0;i<cs.length;i++){
int r = cs[i]-'0';
tInteger = tInteger.add(new BigInteger(r+""));
}
return tInteger.toString();
}
}
第二题:
输入:一个字符串(如:abcdAbc) (另一个字符串(true|false),true表示第一个字符串大小写敏感,如果没有此字符串,默认为"true")
输出:第一个(abcdAbc 中只出现一次的字符)
样例输入1: abcdefABCDE true
样例输出1: a
样例输入2:abcdefABCDE
样例输出2:a
我的答案:
<span style="font-size:18px;">package 华为笔试;
import java.util.HashMap;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
T2 t2 = new T2();
}
}
class T2 {
String special;
String ignore = "";
String before = "";
public T2() {
Scanner sca = new Scanner(System.in);
while (sca.hasNext()) {
special = sca.nextLine();
if (!special.contains(" ")) {
ignore = "true";
} else {
ignore = special.substring(special.length() - 5, special.length());
special = special.replace(" true", "");
special = special.replace(" false", "");
}
HashMap<Character, Integer> jishu = new HashMap<Character, Integer>();
before = special;
if (ignore.equals("false")) {
special = special.toUpperCase();
}
char[] tmp = special.toCharArray();
for (int i = 0; i < tmp.length; i++) {
if (!jishu.containsKey(tmp[i])) {
jishu.put(tmp[i], 1);
} else {
jishu.put(tmp[i], 2);
}
}
for (int i = 0; i < tmp.length; i++) {
if (jishu.get(tmp[i]) == 1) {
System.out.println(before.charAt(i));//输出原字符转的
break;
}
}
}
}
}
</span>
package 华为笔试;
import java.util.HashMap;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
T2 t2 = new T2();
}
}
class T2 {
String special;
String ignore = "";
String before = "";
public T2() {
Scanner sca = new Scanner(System.in);
while (sca.hasNext()) {
special = sca.nextLine();
if (!special.contains(" ")) {
ignore = "true";
} else {
ignore = special.substring(special.length() - 5, special.length());
special = special.replace(" true", "");
special = special.replace(" false", "");
}
HashMap<Character, Integer> jishu = new HashMap<Character, Integer>();
before = special;
if (ignore.equals("false")) {
special = special.toUpperCase();
}
char[] tmp = special.toCharArray();
for (int i = 0; i < tmp.length; i++) {
if (!jishu.containsKey(tmp[i])) {
jishu.put(tmp[i], 1);
} else {
jishu.put(tmp[i], 2);
}
}
for (int i = 0; i < tmp.length; i++) {
if (jishu.get(tmp[i]) == 1) {
System.out.println(before.charAt(i));//输出原字符转的
break;
}
}
}
}
}
题目三:输入:两个7进制整数(可能很大)
输出:这两个7进制相乘结果的7进制表示
样例输入:14 2
样例输出:31
我的代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args) {
String s1;
BigInteger sss1=new BigInteger(0+"");
String s2;
BigInteger sss2=new BigInteger(0+"");
Scanner sca = new Scanner(System.in);
s1 = sca.next();
s2 = sca.next();
for(int i=0;i<s1.length();i++){
sss1 = sss1.multiply(new BigInteger(7+"")).add(new BigInteger(getNum(s1.charAt(i))+""));
}
for(int i=0;i<s2.length();i++){
sss2 = sss2.multiply(new BigInteger(7+"")).add(new BigInteger(getNum(s2.charAt(i))+""));
}
BigInteger mid = sss1.multiply(sss2);
System.out.print(mid.toString(7));
}
public static int getNum(char c){
if(c>='0'&&c<='9'){
return (int)c-'0';
}else{
return (int)(c-'a'+1)+9;
}
}
}
总结:
题目较简单,思路明了,但是细节较多(做完用了半个小时,找bug用了1个小时。。。),
需要非常非常非常非常注意,如果有bug找不出来,就反复读题,千万不要有题目错了的错觉0.0
技巧:
多练习以下类型题目,应该可以拿满分:
1.进制转换,包括n转10,10转n;
2.大数的处理(java:BigInteger类)
3.字符串的操作(java:String类)
4.搜索(dfs,bfs)
5.模拟
6.数组的灵活运用(包括集合,ArraList,HashMap等)
7.常用数据结构的熟练运用(Stack,Queue等)