2017年
1.编写一个reverseadd函数,实现两个数倒置后再求和的功能,比如输入123,456就是求321+654,输出975。注意:输入100,200输出3,(自动去除开头的0),超过范围输出-1
思路:接收两个数字,如果为负数,先打印负号,再转为正数进行处理。倒序就利用取余。如果余数为零,则跳过该次循环,不为零,则存到字符串。注意:调整语句要放到continue语句前,否则会死循环。
package com.huawei.test2017.test1;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个数字");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.println( reverseadd(num1,num2) );
}
public static int reverseadd(int num1,int num2) {
if ((num1<1)||(num1>700000)||(num2<1)||(num2>700000)) {
return -1;
}
String res1 = "";
String res2 = "";
while ( num1 != 0 ){
int r1 = num1 % 10;
num1 = num1 / 10;
if ( r1==0 ) {
continue;
}else {
res1 += r1;
}
}
while ( num2 != 0 ){
int r2 = num2 % 10;
num2 = num2 / 10;
if ( r2==0 ) {
continue;
}else {
res2 += r2;
}
}
int reserveresult1 = Integer.parseInt(res1);
int reserveresult2 = Integer.parseInt(res2);
int sum = reserveresult1 + reserveresult2;
return sum;
}
}
2.一个立方体骰子平放在桌面上,有一面正对着读者,称为前面。我们将六个面分别称为左、右、前、后、上、下,每个面对应的数字分别为1、2、3、4、5、6。我们定义以下操作:向前滚动称为F,向后滚动称为B,向左滚动称为L,向右滚动称为R,上下面不变顺时针旋转称为C,上下面不变逆时针旋转称为U。在原始状态情况下,输入对应的操作码,输出最终的骰子状态(按照每个面的顺序左右前后上下)。
package com.huawei.test2017.test1;
import java.util.Scanner;
public class test2 {
private static int state[]= {
1,2,3,4,5,6}; //定义初始状态数组
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一串字符串,L代表向左翻转,R代表向右翻转,F代表向前翻转,B代表向后翻转,A代表逆时针翻转,C代表顺时针翻转");
while(sc.hasNext()) { //如果有下一个事件则返回true
String s = sc.next(); //定义字符串接收操作码
char c[] = s.toCharArray(); //将字符串转换成一个新的字符数组
move(c);
for(int i=0; i<6 ;i++) {
System.out.print( state[i] );
}
}
}
public static void left() { //向左旋转
int temp1 = state[0];
int temp2 = state[1];
state[0] = state[4];
state[1] = state[5];
state[5] = temp1;
state[4] = temp2;
}
public static void front() { //向前旋转
int temp1 = state[2];
int temp2 = state[3];
state[2] = state[4];
state[3] = state[5];
state[5] = temp1;
state[4] = temp2;
}
public static void clockwise() { //顺时针旋转
int temp1 = state[<