二、生日蜡烛
某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。
现在算起来,他一共吹熄了236根蜡烛。
请问,他从多少岁开始过生日party的?
请填写他开始过生日party的年龄数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
【答案】:26
public class 生日蜡烛 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<=100;i++){
for(int j=i;j<=100;j++){
if((i+j)*(j-i+1)/2==236) System.out.print(i);
}
}
}
}
三、凑算式
这个算式中A ~ I代表1~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?
注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。
public class 凑算式 {
public static int[]a={1,2,3,4,5,6,7,8,9};
public static int ans=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
f(0);
System.out.print(ans);
}
public static void f(int k){
if(k==9){
int x=a[0];
int y=a[1];
int z=a[2];
int l=a[3]*100+a[4]*10+a[5];
int m=a[6]*100+a[7]*10+a[8];
if(x*z*m+y*m+z*l==10*z*m)ans++;
}
for(int i=k;i<9;i++){
int temp=a[k];
a[k]=a[i];
a[i]=temp;
f(k+1);
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
}
}
from itertools import permutations
res = 0
nums = [1,2,3,4,5,6,7,8,9]
for i in permutations(nums,9):
if i[0]+i[1]/i[2]+int(str(i[3])+str(i[4])+str(i[5]))/int(str(i[6])+str(i[7])+str(i[8])) == 10:
res+=1
print(res)
六、方格填数
如下的10个格子
填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)
一共有多少种可能的填数方案?
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
public class 方格填数 {
public static int[]a={0,1,2,3,4,5,6,7,8,9};
public static int ans=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
f(0);
System.out.print(ans);
}
public static void f(int k){
if(k==10){
if(Math.abs(a[0]-a[1])!=1 && Math.abs(a[0]-a[3])!=1 && Math.abs(a[0]-a[4])!=1 && Math.abs(a[0]-a[5])!=1 &&
Math.abs(a[1]-a[2])!=1 && Math.abs(a[1]-a[4])!=1 && Math.abs(a[1]-a[5])!=1 && Math.abs(a[1]-a[6])!=1 &&
Math.abs(a[2]-a[5])!=1 && Math.abs(a[2]-a[6])!=1 &&
Math.abs(a[3]-a[4])!=1 && Math.abs(a[3]-a[7])!=1 &&Math.abs(a[3]-a[8])!=1 &&
Math.abs(a[4]-a[5])!=1&& Math.abs(a[4]-a[7])!=1 && Math.abs(a[4]-a[8])!=1 && Math.abs(a[4]-a[9])!=1 &&
Math.abs(a[5]-a[6])!=1 && Math.abs(a[5]-a[8])!=1 &&Math.abs(a[5]-a[9])!=1 &&
Math.abs(a[6]-a[9])!=1 &&
Math.abs(a[7]-a[8])!=1 &&
Math.abs(a[8]-a[9])!=1
)ans++;
}
for(int i=k;i<10;i++){
int temp=a[k];
a[k]=a[i];
a[i]=temp;
f(k+1);
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
}
}
from itertools import permutations
res = 0
nums = [0,1,2,3,4,5,6,7,8,9]
for i in permutations(nums,10):
if i[0]+1 != i[1] and i[0]+1 != i[3] and i[0]+1 != i[4] and i[0]+1 != i[5]:
if i[1]+1 != i[0] and i[1]+1 != i[2] and i[1]+1 != i[4] and i[1]+1 != i[5] and i[1]+1 != i[6]:
if i[2]+1 != i[1] and i[2]+1 != i[5] and i[2]+1 != i[6]:
if i[3]+1 != i[0] and i[3]+1 != i[4] and i[3]+1 != i[7] and i[3]+1 != i[8]:
if i[4]+1 != i[0] and i[4]+1 != i[1] and i[4]+1 != i[3] and i[4]+1 != i[5] and i[4]+1 != i[7] and i[4]+1 != i[8] and i[4]+1 != i[9]:
if i[5]+1 != i[0] and i[5]+1 != i[1] and i[5]+1 != i[2] and i[5]+1 != i[4] and i[5]+1 != i[6] and i[5]+1 != i[8] and i[5]+1 != i[9]:
if i[6]+1 != i[1] and i[6]+1 != i[2] and i[6]+1 != i[5] and i[6]+1 != i[9]:
if i[7]+1 != i[3] and i[7]+1 != i[4] and i[7]+1 != i[8]:
if i[8]+1 != i[3] and i[8]+1 != i[4] and i[8]+1 != i[5] and i[8]+1 != i[7] and i[8]+1 != i[9]:
if i[9]+1 != i[4] and i[9]+1 != i[5] and i[9]+1 != i[6] and i[9]+1 != i[8]:
res+=1
print(res)