小小+霸霸+王王=小霸王用SQL列举出结果(下面是javaEye上的网友写的)create table n_table (n int)insert n_table values(1);//把1到9插入 select a.n, b.n, c.n
from n_table a, n_table b, n_table c
where 11 * (a.n + b.n + c.n) = a.n * 100 + b.n * 10 + c.n 相当java代码里面 三个 for 循环,或者 Y+10Z=89X,然后把符合条件的 输出。也有网友给出了 java代码。(这对我们程序员不算问题吧,这里就不列出)个人想了下,单纯从这道题目来看,可不可以先分析下?从题目可以得知 小霸王 的取值范围会在 100-297之间。我们不用循环判断,我们从 小霸王 往反向拆分然后在拆分里面 加 符合上面条件的判断,这样循环应该减少很多次吧。个人没学过数据结构,只能这样写写。嘿嘿。package test;public class TQTest { /**
* @param args
*/
public static void main(String[] args)
{
int maxValue = 297;
int minVaule = 100;
int temp1[] = null;
for(int i = minVaule;i<=maxValue;i++)
{
temp1 = returnChangeValue(i);
if(temp1!=null && temp1.length==3)
{
if(temp1[0]!=0 && temp1[1]!=0 && temp1[2]!=0)
{
System.out.println(temp1[0]+" "+temp1[1]+" "+temp1[2]);
}
}
}
}
private static int[] returnChangeValue(int temp)
{
int x,y,z;
int value = temp;
x = temp/100;
temp = temp%100;
y = temp/10;
temp = temp%10;
z = temp;
if(x+x*10+y*10+y+z*10+z == value)
{
return new int[]{x,y,z};
}else
{
return new int[]{0,0,0};
}
}
}感觉还是蛮局限的,没有提炼出通用的办法,纯粹为解决问题而写,数据结构白痴啊。
抛砖(估计砖的级别都没到,嘿嘿)引玉,等待高手,是否能从数据结构方面优化下
from n_table a, n_table b, n_table c
where 11 * (a.n + b.n + c.n) = a.n * 100 + b.n * 10 + c.n 相当java代码里面 三个 for 循环,或者 Y+10Z=89X,然后把符合条件的 输出。也有网友给出了 java代码。(这对我们程序员不算问题吧,这里就不列出)个人想了下,单纯从这道题目来看,可不可以先分析下?从题目可以得知 小霸王 的取值范围会在 100-297之间。我们不用循环判断,我们从 小霸王 往反向拆分然后在拆分里面 加 符合上面条件的判断,这样循环应该减少很多次吧。个人没学过数据结构,只能这样写写。嘿嘿。package test;public class TQTest { /**
* @param args
*/
public static void main(String[] args)
{
int maxValue = 297;
int minVaule = 100;
int temp1[] = null;
for(int i = minVaule;i<=maxValue;i++)
{
temp1 = returnChangeValue(i);
if(temp1!=null && temp1.length==3)
{
if(temp1[0]!=0 && temp1[1]!=0 && temp1[2]!=0)
{
System.out.println(temp1[0]+" "+temp1[1]+" "+temp1[2]);
}
}
}
}
private static int[] returnChangeValue(int temp)
{
int x,y,z;
int value = temp;
x = temp/100;
temp = temp%100;
y = temp/10;
temp = temp%10;
z = temp;
if(x+x*10+y*10+y+z*10+z == value)
{
return new int[]{x,y,z};
}else
{
return new int[]{0,0,0};
}
}
}感觉还是蛮局限的,没有提炼出通用的办法,纯粹为解决问题而写,数据结构白痴啊。
抛砖(估计砖的级别都没到,嘿嘿)引玉,等待高手,是否能从数据结构方面优化下
不过,遍了一个简单的代码。
public static String fun(String arg1,String arg2,String arg3){
if(arg1==null || arg2==null || arg3==null)return null;
if(arg1.length()==2 && arg2.length()==2 && arg3.length()==2){
if(arg1.charAt(0)==arg1.charAt(1) && arg2.charAt(0)==arg2.charAt(1) && arg3.charAt(0)==arg3.charAt(1)){
StringBuilder sb = new StringBuilder(3);
sb.append(arg1.charAt(0)).append(arg2.charAt(0)).append(arg3.charAt(0));
return sb.toString();
}
}
return null;
}
public static void main(String[] args){
int maxValue = 300;
int minVaule = 121;
int ge,shi,bai;
for(int i = minVaule;i <=maxValue;i+=11){
ge=i%10;
shi=i/10%10;
bai=i/100;
if(11*(ge+shi+bai)==i){
System.out.println(bai +" "+shi +" " +ge+" ");
}
}
}
}
F:\java>java TQTest
1 9 8
* 首先分析结果范围在100-297之间
* 再分析可以知道第一位和第二位只能是11+99或22+88的组合(由于第三位要个各位相同,因此前两位数字和应该为10),只需检测第3位
* @param args
*/
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
if (i*10+i+11+99 == 190+i) {
System.out.println(1+" "+9+" "+i);
}
if(i*10+i+22+88 == 280+i){
System.out.println(2+" "+8+" "+i);
}
} }}只有1个答案? 1 9 8
+yy
+zz
-------
xyz 得出 x+y=0 ,如果不等于0,个位数就不是z了,对不!
在看百位数,有百位树,证明十位树 进位了,而且只能进1
得出 x=1 z=9 ,带入 y=8
xx
yy
zz
+----
xyz
从个位看:x+y+z=10+z,所以x+y=10
从十位看x+y+z+1=10*x+y, 所以z+1=9*x
由z+1=9*x得,因为x如果超过超过1,z就成了两位数了,所以,x只能是1,同时z=8.
再由,x+y=10得:y=9.一经分析,不用编程了,x=1,y=9,z=8.
传说是的,我没有去 Tencent面试过,传说是某网友去tencent面试时出的一道SQL题
{
public static void main(String[] args)
{
int a, b, c;
for(a = 1; a < 3; a++)
{
b = (a * 89) % 10;
c = (89 * a - b) / 10;
if(c < 10)
System.out.println("a = " + a + "\nb = " + b + "\nc = " + c);
}
}
}
aa
bb
+cc
---
abc首先认为这个题目的规则包含a、b、c都不为0,否则就不是3个2位数的和等于一个3位数了因此有 0 < a、b、c <= 9. 下面N为任意整数看个位 (a+b+c)%10 = c, 所以 a+b = 10N. 考虑 0< a+b < 18, 得 a+b = 10再看十位 (a+b+c+1)%10 = b,所以 a+c = 10N - 1,考虑 0< a+c < 18,得 a+c = 9;十位到百位的进位是1,因此a=1.带入,得 b=9, c=8
这样可以得到
1,都是十进制数 a,b,c三个数范围(1-9),2 abc这个数 >100, < 300分析 abc:
a: 其实是个进位位,范围(1~2)
c: 其实是余数 a + b + c -(a * 10)(进位) = c
=> b = 9a
b: 三个数相加 再加个进位(a) => a + b + c + a = b + a * 10 => c = 8A 所以c = 8A:1,b:9,c:8
最小三位数是100;
三个二位数相加最大 99 * 3 = 297;
“小”取值要么是1,要么是2;
三位数最后一位是“王”,所以“小”+“霸”= 10;
“霸”要么是9,要么是8,且“小小”+“霸霸”= 110;
结果:小霸王 = 198。代码:
int findXiaoBaWang()
{
return 198;
}完成。
declare @b int
declare @c int declare @i int select @i = 110while @i < 297begin select @a = @i/100 select @b = @i%100/10 select @c =@i % 10 if 11*(@a+@b+@c) = @i and @a*100 + @b*10 +@c = @i
begin
return @i
end
else
set @i = @i + 11
end
return 0
end