1.猜年龄
美国数学家维纳(N.Wiener)智力早熟,11岁就上了大学。他曾在1935~1936年应邀来中国清华大学讲学。
一次,他参加某个重要会议,年轻的脸孔引人注目。于是有人询问他的年龄,他回答说:
“我年龄的立方是个4位数。我年龄的4次方是个6位数。这10个数字正好包含了从0到9这10个数字,每个都恰好出现1次。”
请你推算一下,他当时到底有多年轻。
public class GuessYear {
/**
* 判断两个数组中的数字是否有相同的
* @param x
* @param y
* @return
*/
boolean isEqual(long[] x,long[] y){
boolean flag = false;
int len = x.length;
int len1 = y.length;
//判断两个数组间是否有相同的数字
for(int i = 0; i < len; i++){
for(int j = 0; j < len1; j++){
if(x[i] == y[j])
flag = true;
}
}
//判断第一个数组内是否有相同的数字
for(int i = 0; i < len; i++){
long tmp = x[i];
for(int j = i+1; j < len; j++){
if(tmp == x[j])
flag = true;
}
}
//判断第二个数组内是否有相同的数字
for(int i = 0; i < len1; i++){
long tmp = y[i];
for(int j = i+1; j < len1; j++){
if(tmp == y[j])
flag = true;
}
}
return flag;
}
/**
* 判断某数字是几位数
* @param tmp
* @return
*/
boolean isWhatBit(long num,int bit){
int count = 0;
while(num != 0){
num /= 10;
count++;
}
if( count == bit) return true;
else return false;
}
/**
* 将数字分离到数组中
* @param a
* @param x
*/
void splitNumber(long num,long []x){
int i = x.length - 1;
while(num != 0){
x[i--] = num % 10;
num /= 10;
}
}
/**
* 猜年龄
*/
void guess(){
long x[] = new long[4];
long y[] = new long[6];
long row,col;
row = col = 10;
for(long i = 1; i < row; i++){
for(long j = 0; j < col; j++){
long tmp = i*10+j;
long f = tmp*tmp*tmp;
long s = f*tmp;
if(!isWhatBit(f,4))
continue;
if(!isWhatBit(s,6))
continue;
splitNumber(f,x);
splitNumber(s,y);
if(!isEqual(x,y))
System.out.println(tmp);
}
}
}
}
答案:18
2.组素数
素数就是不能再进行等分的数。比如:2 3 5 7 11 等。9 = 3 * 3 说明它可以3等分,因而不是素数。
我们国家在1949年建国。如果只给你 1 9 4 9 这4个数字卡片,可以随意摆放它们的先后顺序(但卡片不能倒着摆放啊,我们不是在脑筋急转弯!),那么,你能组成多少个4 位的素数呢?
比如:1949,4919 都符合要求。
请你提交:能组成的4位素数的个数,不要罗列这些素数!!
public class FourWeiPrime {
boolean isPrime(int n){
boolean flag = true;
for(int i = 2; i*i <= n; i++){
if(n % i == 0)
flag = false;
}
return flag;
}
boolean isInclude1949(int n){
boolean flag = false;
int y[] = {0,0,0,0};
int count = 0;
while(n != 0){
int r = n%10;
if(r == 1){
y[0] = 1;
}else if(r == 4){
y[1] = 1;
}else if(r == 9){
if(y[2] != 1){
y[2] = 1;
}else if(y[3] != 1){
y[3] = 1;
}
}
n /= 10;
}
for(int i = 0; i < 4; i++){
if(y[i] == 1){
count++;
}
}
if(count == 4){
flag = true;
}
return flag;
}
void prime(){
int init = 1000;
int max = 9999;
int ln = 0;
int count = 0;
for(int i = init; i < max; i++){
if(isPrime(i)){
if(isInclude1949(i)){
System.out.print(""+i+",");
count++;
}
}
}
System.out.println("四位素数有:"+count+"个");
}
}
答案:6
转载请标明出处:http://blog.youkuaiyun.com/u012027907
3.马虎的算式
小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。有一次,老师出的题目是:36 x 495 = ?
他却给抄成了:396 x 45 = ?
但结果却很戏剧性,他的答案竟然是对的!!
因为 36 * 495 = 396 * 45 = 17820
类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54
假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)
能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?
请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。
满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。
public class Mahu {
void mahu(){
int MAX = 9;
int count = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
for(int a = 1; a <= MAX; a++){
for(int b = 1; b <= MAX; b++){
if(a == b)
continue;
for(int c = 1; c <= MAX; c++){
if(a == c || b == c)
continue;
for(int d = 1; d <= MAX; d++){
if(a == d || b == d || c == d)
continue;
for(int e = 1; e <= MAX; e++){
if(a == e || b == e || c == e || d == e)
continue;
num1 = a*10+b;
num2 = c*100+d*10+e;
num3 = a*100+d*10+b;
num4 = c*10+e;
// System.out.println(a+","+b+","+c+","+d+","+e);
if(num1*num2 == num3*num4){
count++;
System.out.println(""+a+b+"*"+c+d+e);
}
}
}
}
}
}
System.out.println("总数:"+count);
}
}
答案:142
4.第39级台阶
小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级!站在台阶前,他突然又想着一个问题:
如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?
请你利用计算机的优势,帮助小明寻找答案。
要求提交的是一个整数。
public class Plant39 {
void plant(){
int MAX = 39;
int m,n;
int sum = 0;
int count = 0;
for(m = 1; m <= MAX; m++){
for(n = 1; n <= MAX; n++){
int tmp = m + n*2;
sum = m + n;
if(tmp == 39 && sum % 2 == 0){
count++;
System.out.println(m+","+n+"");
}
}
}
System.out.println("共有"+count+"种");
}
}
答案:10
5.有理数类
有理数就是可以表示为两个整数的比值的数字。一般情况下,我们用近似的小数表示。但有些时候,不允许出现误差,必须用两个整数来表示一个有理数。这时,我们可以建立一个“有理数类”,下面的代码初步实现了这个目标。为了简明,它只提供了加法和乘法运算。
class Rational
{
private long ra;
private long rb;
private long gcd(long a, long b){
if(b==0) return a;
return gcd(b,a%b);
}
public Rational(long a, long b){
ra = a;
rb = b;
long k = gcd(ra,rb);
if(k>1){ //需要约分
ra /= k;
rb /= k;
}
}
// 加法
public Rational add(Rational x){
return _____________________; //填空位置
}
// 乘法
public Rational mul(Rational x){
return new Rational(ra*x.ra, rb*x.rb);
}
public String toString(){
if(rb==1) return "" + ra;
return ra + "/" + rb;
}
}
使用该类的示例:
Rational a = new Rational(1,3);
Rational b = new Rational(1,6);
Rational c = a.add(b);
System.out.println(a + "+" + b + "=" + c);
答案:new Rational(ra*x.rb+rb*x.ra,rb*x.rb)