1.三角形面積
- 如圖1所示。圖中的所有小方格面積都是1。
- 那么,圖中的三角形面積應該是多少呢?
- 請填寫三角形的面積。不要填寫任何多余內容或說明性文字。

答案 : 28
2.立方變自身
- 觀察下面的現象,某個數字的立方,按位累加仍然等於自身。
1^3 = 1
8^3 = 512 5+1+2=8
17^3 = 4913 4+9+1+3=17
- 請你計算包括1,8,17在內,符合這個性質的正整數一共有多少個?
考到一個知識點, 如何把一個整數拆分出每個位上的數. 見代碼
public class Main {
public static void main(String[] args) {
int res = 0;
int[] partNum = null;
int cubis = 0;
int sum = 0;
for(int i = 1; i <= 1000000; i++){//i從小嘗試到大, 發現結果也就那6個數.
sum = 0;
cubis = i * i * i;
partNum = getPartNum(cubis);
for(int j = 0; j < partNum.length; j++){
sum += partNum[j];
}
if(sum == i){
res++;
}
}
System.out.println(res);
}
public static int[] getPartNum(int num){
String numStr = String.valueOf(num);
int[] res = new int[numStr.length()];
int system = 1;//用於計算10的n次方
for(int i = 0; i < res.length; i++){
res[res.length - i - 1] = num / system % 10;
system *= 10;
}
return res;
}
}
答案 : 6
3.三羊獻瑞
- 觀察下面的加法算式:
祥 瑞 生 輝
+ 三 羊 獻 瑞
----------------------------
三 羊 生 瑞 氣
- 其中,相同的漢字代表相同的數字,不同的漢字代表不同的數字。
- 請你填寫“三羊獻瑞”所代表的4位數字(答案唯一),不要填寫任何多余內容。
這題有個小坑, "三"字不能為0, 要不然答案不是唯一的, 其實也挺好解釋的, 如果為0的話"三羊獻瑞"和"三羊生瑞氣"分別都不能算做一個4位數和一個5位數.
public class T3 {
public static void main(String[] args) {
for(int a = 0; a <= 9; a++){
for(int b = 0; b <= 9; b++){
if(b == a) continue;
for(int c = 0; c <= 9; c++){
if(c == a || c == b) continue;
for(int d = 0; d <= 9; d++){
if(d == a || d == b || d == c) continue;
for(int e = 0; e <= 9; e++){
if(e == a || e == b || e == c || e == d) continue;
for(int f = 0; f <= 9; f++){
if(f == a || f == b || f == c || f == d || f == e) continue;
for(int g = 0; g <= 9; g++){
if(g == a || g == b || g == c || g == d || g == e || g == f) continue;
for(int h = 0; h <= 9; h++){
if(h == a || h == b || h == c || h == d || h == e || h == f || h == g) continue;
int num1 = a * 1000 + b * 100 + c * 10 + d;
int num2 = e * 1000 + f * 100 + g * 10 + b;
int sum = e * 10000 + f * 1000 + c * 100 + b * 10 + h;
if(num1 + num2 == sum && e != 0){
System.out.println("三:" + 1 + " 羊:" + f + " 獻:" + g + " 瑞:" + 5);
}
}
}
}
}
}
}
}
}
}
}
答案 : 1085
4.循環節長度
- 兩個整數做除法,有時會產生循環小數,其循環部分稱為:循環節。
比如,11/13=6=>0.846153846153..... 其循環節為[846153] 共有6位。
下面的方法,可以求出循環節的長度。 - 請仔細閱讀代碼,並填寫划線部分缺少的代碼。
public static int f(int n, int m)
{
n = n % m;
Vector v = new Vector();
for(;;)
{
v.add(n);
n *= 10;
n = n % m;
if(n==0) return 0;
if(v.indexOf(n)>=0) _________________________________ ; //填空
}
}
用Vector來記錄小數點后出現的數字, 出現重復即說明開始有循環了. 但是可能沒有考慮到循環的開始不是直接從小數點后開始的?
答案 : v.size()
3.九數組分數
- 1,2,3...9 這九個數字組成一個分數,其值恰好為1/3,如何組法?
- 下面的程序實現了該功能,請填寫划線部分缺失的代碼。
public class A
{
public static void test(int[] x)
{
int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
if(a*3==b) System.out.println(a + " " + b);
}
public static void f(int[] x, int k)
{
if(k>=x.length){
test(x);
return;
}
for(int i=k; i<x.length; i++){
{int t=x[k]; x[k]=x[i]; x[i]=t;}
f(x,k+1);
_______________________________________ // 填空
}
}
public static void main(String[] args)
{
int[] x = {1,2,3,4,5,6,7,8,9};
f(x,0);
}
}
回溯. 全排列的標准寫法.
答案 : {int t=x[k]; x[k]=x[i]; x[i]=t;}
6.加法變乘法
- 我們都知道:1+2+3+ ... + 49 = 1225
- 現在要求你把其中兩個不相鄰的加號變成乘號,使得結果為2015
- 比如:
1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015
- 就是符合要求的答案。
- 請你尋找另外一個可能的答案,並把位置靠前的那個乘號左邊的數字提交(對於示例,就是提交10)。
- 注意:需要你提交的是一個整數,不要填寫任何多余的內容。
我用了兩層for循環模擬兩個*號的位置, 然后原地調整數組求結果
public class Main {
public static void main(String[] args) {
int[] arr = new int[49];
int num = 1;
for(int i = 0; i < 49; i++){//把1~49填入初始數組
arr[i] = num++;
}
for(int i = 0; i <= 45; i++){
for(int j = i + 2; j <= 47; j++){//i和j用於找到乘號的位置
int sum = 0;
for(int k = 0; k < arr.length; ){//遍歷拼接算式
if(k == i){
sum += arr[k] * arr[k + 1];
k += 2;
}else if(k == j){
sum += arr[k] * arr[k + 1];
k += 2;
}else{
sum += arr[k];
k++;
}
}
if(sum == 2015)
System.out.println(i + 1);
}
}
}
答案 : 16
7.牌型種數
- 小明被劫持到X賭城,被迫與其他3人玩牌。
- 一副撲克牌(去掉大小王牌,共52張),均勻發給4個人,每個人13張。
- 這時,小明腦子里突然冒出一個問題:
- 如果不考慮花色,只考慮點數,也不考慮自己得到的牌的先后順序,自己手里能拿到的初始牌型組合一共有多少種呢?
遞歸的策略是: 一共要取13張牌, 選滿13張結束. 每次選一種牌, 一種牌可以選0~4張. 記得回溯.
public class T7 {
public static int res = 0;//種類
public static void main(String[] args) {
process(1, 0);
System.out.println(res);
}
//n是第幾張牌(A~K), sum是當前的牌數.
public static void process(int n, int sum){
if(sum == 13){
res++;
return;
}
if(n < 14){
for(int i = 0; i < 5; i++){//每種牌可能拿0~4張
sum += i;
process(n + 1, sum);
sum -= i;
}
}
}
}
答案 : 3598180
8.飲料換購
- 樂羊羊飲料廠正在舉辦一次促銷優惠活動。樂羊羊C型飲料,憑3個瓶蓋可以再換一瓶C型飲料,並且可以一直循環下去,但不允許賒賬。
- 請你計算一下,如果小明不浪費瓶蓋,盡量地參加活動,那么,對於他初始買入的n瓶飲料,最后他一共能得到多少瓶飲料。
輸入:一個整數n,表示開始購買的飲料數量(0<n<10000)
輸出:一個整數,表示實際得到的飲料數
例如:
用戶輸入:
100
程序應該輸出:
149
用戶輸入:
101
程序應該輸出:
151
資源約定:
峰值內存消耗(含虛擬機) < 256M
CPU消耗 < 1000ms
請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。
每喝3瓶記錄一次結果
public class T8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = 0;
while(n >= 3){
res += 3;//每次喝3瓶
n -= 2;//喝3瓶攢回1瓶.
}
res += n;
System.out.println(res);
}
}
9.壘骰子
- 賭聖atm晚年迷戀上了壘骰子,就是把骰子一個壘在另一個上邊,不能歪歪扭扭,要壘成方柱體。
- 經過長期觀察,atm 發現了穩定骰子的奧秘:有些數字的面貼着會互相排斥!
- 我們先來規范一下骰子:1 的對面是 4,2 的對面是 5,3 的對面是 6。
- 假設有 m 組互斥現象,每組中的那兩個數字的面緊貼在一起,骰子就不能穩定的壘起來。
- atm想計算一下有多少種不同的可能的壘骰子方式。
- 兩種壘骰子方式相同,當且僅當這兩種方式中對應高度的骰子的對應數字的朝向都相同。
- 由於方案數可能過多,請輸出模 10^9 + 7 的結果。
- 不要小看了 atm 的骰子數量哦~
「輸入格式」
第一行兩個整數 n m
n表示骰子數目
接下來 m 行,每行兩個整數 a b ,表示 a 和 b 不能緊貼在一起。
「輸出格式」
一行一個數,表示答案模 10^9 + 7 的結果。
「樣例輸入」
2 1
1 2
「樣例輸出」
544
「數據范圍」
對於 30% 的數據:n <= 5
對於 60% 的數據:n <= 100
對於 100% 的數據:0 < n <= 10^9, m <= 36
資源約定:
峰值內存消耗(含虛擬機) < 256M
CPU消耗 < 2000ms
小弟無才, 目前這題只想到了暴力解. 在一骰子1上面堆一個骰子2的時候, 需要知道骰子1上表面是什么數字. 當一個骰子的地面確定后, 一共有4種擺放的方式, 堆在它上面的也有4種, 一共就是4*4種, 呈指數級增長. 有待更新...
public class Main {
public static int res = 0;
public static int n = 0;
public static HashMap<Integer, Integer> oppositeMap = new HashMap<Integer, Integer>();//定義骰子的規則.
public static void main(String[] args) {
oppositeMap.put(1, 4);
oppositeMap.put(2, 5);
oppositeMap.put(3, 6);
oppositeMap.put(4, 1);
oppositeMap.put(5, 2);
oppositeMap.put(6, 3);
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int m = sc.nextInt();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();//保存互斥的數字.
for(int i = 0; i < m; i++){
int num1 = sc.nextInt();
int num2 = sc.nextInt();
map.put(num1, num2);
map.put(num2, num1);
}
process(map, 1, 1);
System.out.println(res % 1000000007);
}
public static void process(HashMap<Integer, Integer> map, int num, int lastTop){
if(num == n + 1){
res += getPowerOfFour(n);
return;
}
if(num == 1){//第一層骰子以哪個數字為底面都行
for(int i = 1; i <= 6; i++){//以某個數字作為底面
process(map, num + 1, oppositeMap.get(i));
}
}else{
int unTouch = map.containsKey(lastTop) ? map.get(lastTop) : -1;//判斷是否有互斥的面.
if(unTouch > 0){
for(int i = 1; i <= 6; i++){
if(i != unTouch){
process(map, num + 1, oppositeMap.get(i));
}
}
}else{
for(int i = 1; i <= 6; i++){
process(map, num + 1, oppositeMap.get(i));
}
}
}
}
public static int getPowerOfFour(int n){
int res = 1;
for(int i = 0; i < n; i++){
res *= 4;
}
return res;
}
}
10.生命之樹
- 在X森林里,上帝創建了生命之樹。
- 他給每棵樹的每個節點(葉子也稱為一個節點)上,都標了一個整數,代表這個點的和諧值。
上帝要在這棵樹內選出一個非空節點集S,使得對於S中的任意兩個點a,b,都存在一個點列 {a, v1, v2, ..., vk, b} 使得這個點列中的每個點都是S里面的元素,且序列中相鄰兩個點間有一條邊相連。 - 在這個前提下,上帝要使得S中的點所對應的整數的和盡量大。
這個最大的和就是上帝給生命之樹的評分。 - 經過atm的努力,他已經知道了上帝給每棵樹上每個節點上的整數。但是由於 atm 不擅長計算,他不知道怎樣有效的求評分。他需要你為他寫一個程序來計算一棵樹的分數。
「輸入格式」
第一行一個整數 n 表示這棵樹有 n 個節點。
第二行 n 個整數,依次表示每個節點的評分。
接下來 n-1 行,每行 2 個整數 u, v,表示存在一條 u 到 v 的邊。由於這是一棵樹,所以是不存在環的。
「輸出格式」
輸出一行一個數,表示上帝給這棵樹的分數。
「樣例輸入」
5
1 -2 -3 4 5
4 2
3 1
1 2
2 5
「樣例輸出」
8
「數據范圍」
對於 30% 的數據,n <= 10
對於 100% 的數據,0 < n <= 10^5, 每個節點的評分的絕對值不超過 10^6 。
資源約定:
峰值內存消耗(含虛擬機) < 256M
CPU消耗 < 3000ms
思路
- 題目的意思是有一顆連通的樹, 任意連通的點都可以構成一個點集, 現在的問題是如何求得一個和最大的點集.
- 先根據樣例輸入畫圖, 畫成一棵樹的樣子以便分析.

- 畫樹的時候選取根結點是任意的.
- 我們以一棵樹分析如何求最大和點集, 假設對結點2進行分析.
- 以結點2為源頭, 向子結點方向擴充子集的范圍, 最開始子集里只有結點2, 也就是-2.
- 遍歷到左孩子4, 如果以該節點為源的子集的值大於0, 那么子集就加上這個結點, 因為這樣會使子集的和增大. 於是子集的和變成
-2 + 4 = 2
. - 然后繼續遍歷孩子結點, 把結點5也加入進來, 最終子集的和變成
2 + 5 = 7
. - 這樣便確認了以2為起點的點集的最大和.
- 通過這一邏輯, 可以推出遞歸函數.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class T10 {
public static long res = 0;
public static int n;
public static long[] arr;
public static List<Integer>[] rel;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
arr = new long[n + 1];
rel = new ArrayList[n + 1];
initRel();
for(int i = 1; i < n + 1; i++){
arr[i] = sc.nextLong();
}
for(int i = 1; i < n; i++){
int a = sc.nextInt();
int b = sc.nextInt();
rel[a].add(b);
rel[b].add(a);
}
process(1, 0);
System.out.println(res);
}
public static void process(int cur, int father){
for(int i = 0; i < rel[cur].size(); i++){
int child = rel[cur].get(i);
if(child == father) continue;
process(child, cur);
if(arr[child] > 0){
arr[cur] += arr[child];
}
}
res = Math.max(res, arr[cur]);
}
public static void initRel(){
for(int i = 0; i <= n; i++){
rel[i] = new ArrayList<Integer>();
}
}
}