6. 代码填空(满分9分)
下列代码把16进制表示的串转换为3进制表示的串。试完善之。
例如:x=“5”
则返回:“12”
又例如:x=”F”
则返回:“120”
private static int getRealValue(char x)
{
if(x>='0' && x<='9') return x-'0';
if(x>='a' && x<='f') return x-'a'+10;
if(x>='A' && x<='F') return x-'A'+10;
return 0;
}
public static String jin_zhi_16_3(String x)
{
int n = 0; // 累加真值
for(int i=0; i<x.length(); i++)
{
n = _________ +getRealValue(x.charAt(i)); // 填空
}
String t = "";
for(;;)
{
if(n==0) break;
t = (n % 3) + t;
_____________; // 填空
}
return t;
}
-------------------------------------------------------------------------------------------------------
ps:十进制转换为3进制的规则:首先对该数对3求余求得最低位,然后将该数对3求整,即/3,移除最低位(相当于111/10求得高两位的情况)
十六进制转换为10进制关键是正的for循环,因为字符串位数正序第一位为0,则i从这里开始循环递增,则求基数的次方的次方为字符串长度-i+1,即将字符串的标号顺序颠倒
package java2011Yangti;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class M6 {
/**
* author hanhexin
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string = br.readLine();//输入为10进制的即可
int x = Integer.valueOf(string);
System.out.println("---- 用来验证 ------");
String hex = Integer.toHexString(x).toUpperCase();
System.out.println(hex);
String third = Integer.toString(x, 3);
System.out.println(third);
System.out.println("---- 函数 ------");
System.out.println(jin_zhi_16_3(hex));
}
private static int getRealValue(char x) {
if (x >= '0' && x <= '9')
return x - '0';
if (x >= 'a' && x <= 'f')
return x - 'a' + 10;
if (x >= 'A' && x <= 'F')
return x - 'A' + 10;
return 0;
}
public static String jin_zhi_16_3(String x) {
int n = 0; // 累加真值
for (int i = 0; i < x.length(); i++) {
n = n + (int) Math.pow(16, x.length() - 1 - i)* getRealValue(x.charAt(i)); // 填空
}
//System.out.println(n);
String t = "";
for (;;) {
if (n == 0)
break;
t = (n % 3) + t;
n = n / 3; // 填空
}
return t;
}
}
8. 代码设计(满分11分)
考虑方程式:a^3 + b^3 = c^3 + d^3
其中:“^”表示乘方。a、b、c、d是互不相同的小于30的正整数。
这个方程有很多解。比如:
a = 1,b=12,c=9,d=10 就是一个解。因为:1的立方加12的立方等于1729,而9的立方加10的立方也等于1729。
当然,a=12,b=1,c=9,d=10 显然也是解。
如果不计abcd交换次序的情况,这算同一个解。
你的任务是:找到所有小于30的不同的正整数解。把a bc d按从小到大排列,用逗号分隔,每个解占用1行。比如,刚才的解输出为:
1,9,10,12
不同解间的顺序可以不考虑。
--------------------------------------------------------------------------------------------------------------------
ps:这个题遍历就可以,将不重复的情况存入一个链表中,最后输出 , 我想肯定有更好的方法,只是我暂时没有想到
package java2011Yangti;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class M8 {
/**
* @author hanhexin
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
int a, b, c, d;
for (a = 0; a < 30; a++) {
for (b = 0; b < 30; b++) {
if (a == b) {
continue;
}
for (c = 0; c < 30; c++) {
if (c == a || b == c) {
continue;
}
for (d = 0; d < 30; d++) {
if (d == c || d == b || d == a) {
continue;
}
if (Math.pow(a, 3) + Math.pow(b, 3) == Math.pow(c, 3)
+ Math.pow(d, 3)) {
int[] arr = { a, b, c, d };
Arrays.sort(arr);
int n = 0;
for (String string : list) {
if (!string.endsWith("" + arr[0] + "," + arr[1]
+ "," + arr[2] + "," + arr[3])) {
n++;
}
}
if (n == list.size()) {
list.add("" + arr[0] + "," + arr[1] + ","
+ arr[2] + "," + arr[3]);
}
}
}
}
}
}
for (String string : list) {
System.out.println(string);
}
}
}
PS: 今天突然想到一个类 HashSet 哈希集合类,只能存不同的元素,存进去然后通过Iterator类输出
package java2011Yangti;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
public class M8 {
/**
* @author hanhexin
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<String> set = new HashSet<String>();
int a, b, c, d;
for (a = 0; a < 30; a++) {
for (b = 0; b < 30; b++) {
if (a == b) {
continue;
}
for (c = 0; c < 30; c++) {
if (c == a || b == c) {
continue;
}
for (d = 0; d < 30; d++) {
if (d == c || d == b || d == a) {
continue;
}
if (Math.pow(a, 3) + Math.pow(b, 3) == Math.pow(c, 3)
+ Math.pow(d, 3)) {
int[] arr = { a, b, c, d };
Arrays.sort(arr);
String str = "" + arr[0] + "," + arr[1]+ "," + arr[2] + "," + arr[3];
set.add(str);
}
}
}
}
}
Iterator<String> its=set.iterator();
while (its.hasNext()) {
System.out.println(its.next());
}
}
}
PS 方法3这个好 保证了abcd不同,同时如果不重复,必须a与d组合与b与c组合才能形成相等
package java2011Yangti;
public class M8 {
/**
* @author hanhexin
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a, b, c, d;
for (a = 0; a < 27; a++) {
for (b = a + 1; b < 28; b++) {
for (c = b + 1; c < 29; c++) {
for (d = c + 1; d < 30; d++) {
if (Math.pow(a, 3) + Math.pow(d, 3) == Math.pow(b, 3)
+ Math.pow(c, 3)) {
System.out.println(a + "," + b + "," + c + "," + d);
}
}
}
}
}
}
}