无聊尝试些面试题
任意进制(jz)的一个数据(num)转为十进制
/**
*
* @Title: fy_1
* @Description: 任意进制转10进制
* @author shany
* @date 2018年6月2日 下午6:13:45
* @version V1.0
*/
public static int fy_1(int num,int jz){
//num为要转的int型数据,jz是要转的几进制
int len=String.valueOf(num).length();
//把把元数据反转一下
num=Integer.parseInt(new StringBuilder(String.valueOf(num)).reverse().toString());
//出第一个外,其他循环
int result=num%10;
num/=10;
//循环操作
for(int i=1;i<len;i++){
result=result*jz+num%10;
num/=10;
}
return result;
}
算法实现数字颠倒,不调用系统函数,中间不能转为字符串
public class XunFei {
private static int num_len;
private static int[] num;
// 获取字符长度
public void how_len(int temp) {
temp /= 10;
num_len++;
if (temp > 0) {
how_len(temp);
} else {
num = new int[num_len];
}
}
// 输出结果
public int num_result(int temp) {
// 先初始化基础属性
how_len(temp);
num[num_len - 1] = temp % 10;
for (int i = 1; i < num_len; i++) {
temp /= 10;
num[num_len - i - 1] = temp % 10;
}
temp = num[num_len - 1];
for (int i = 1; i < num_len; i++) {
temp *= 10;
temp = temp + num[num_len - i - 1];
}
return temp;
}
public static void main(String[] args) {
int num_1 = 123456789;
XunFei xf = new XunFei();
System.out.println(xf.num_result(num_1));
}
}
张王李三家各有三个小孩。一天,三家的九个孩子在一起比赛短跑,规定不分年龄大小,跑第一得9分,跑第2得8分,依此类推。比赛结果各家的总分相同,且这些孩子没有同时到达终点的,也没有一家的两个或三个孩子获得相连的名次。已知获第一名的是李家的孩子,获得第二的是王家的孩子。问获得最后一名的是谁家的孩子?
public class T1044 {
public static void main(String[] args) {
int a[] = new int[9];
a[0] = 1;
a[1] = 2;
a[2] = 3;
int x = 0, y = 1, z = 2;
for (int i = 3; i < a.length; i++) { // &&(i+1)%3!=6-a[i-1]-1
if (i != 4 && i != 7) {
if (a[i - 1] != 1 && i % 3 != 0 && i % 3 != x % 3) {// &&(i+1)%3!=6-a[i-1]-1
a[i] = 1;
x = i;
if (i == a.length - 1)
System.out.println("L");
} else if (a[i - 1] != 2 && i % 3 != 1 && i % 3 != y % 3) { // &&(i+1)%3!=6-a[i-1]-2
a[i] = 2;
y = i;
if (i == a.length - 1)
System.out.println("W");
} else if (a[i - 1] != 3 && i % 3 != 2 && i % 3 != z % 3) { // &&(i+1)%3!=6-a[i-1]-3
a[i] = 3;
z = i;
if (i == a.length - 1)
System.out.println("Z");
}
} else {
if (a[i - 1] != 1 && i % 3 != 0 && i % 3 != x % 3
&& (i + 1) % 3 + 1 != 6 - a[i - 1] - 1) {//
a[i] = 1;
x = i;
} else if (a[i - 1] != 2 && i % 3 != 1 && i % 3 != y % 3
&& (i + 1) % 3 + 1 != 6 - a[i - 1] - 2) { //
a[i] = 2;
y = i;
} else if (a[i - 1] != 3 && i % 3 != 2 && i % 3 != z % 3
&& (i + 1) % 3 + 1 != 6 - a[i - 1] - 3) { //
a[i] = 3;
z = i;
}
}
// System.out.println(a[i]);
}
}
}
为什么不能根据返回值类型区分重载?
答:如果重置函数中,只有返回值类型不同,那么可以使用object类型数据,来接收所有函数名相同的函数的返回值,这样程序就无法判断接收那个函数传来的数据;同样如果对函数接收的数据进行强制类型转换,系统也无法判断,是对那个函数传来的数据进行接收和转换,所以不能根据返回值类型来区分重载。
以上均为原创,没事慢慢改。。。