目录
前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~
1 三角形面积
三角形面积如【图1】所示。图中的所有小方格面积都是1。那么,图中的三角形面积应该是多少呢?请填写三角形的面积。不要填写任何多余内容或说明性文字。28简单的数学平面几何问题:大正方形面积-三个三角形面积 = 最终结果
2 立方变自身
立方变自身观察下面的现象,某个数字的立方,按位累加仍然等于自身。1^3 = 1 8^3 = 512 5+1+2=817^3 = 4913 4+9+1+3=17...请你计算包括1,8,17在内,符合这个性质的正整数一共有多少个?请填写该数字,不要填写任何多余的内容或说明性的文字。6
public class Main { //暴力枚举,轻易可知,当n > 100时,一定没有符合题意正整数,原因:100^3共7位置,7*9<100,依次类推 public static void main(String[] args) { int count = 0; for(long i = 1;i <= 100000;i++) { //此处使用较大数据检测猜测 long temp = i * i * i; long temp1 = 0; while(temp > 0) { temp1 += (temp % 10); temp = temp / 10; } if(i == temp1) { System.out.println("i = "+i+", i^3 = "+(i*i*i)); count++; } } System.out.println(count); }}
3 三羊献瑞
三羊献瑞观察下面的加法算式: 祥 瑞 生 辉 + 三 羊 献 瑞------------------- 三 羊 生 瑞 气(如果有对齐问题,可以参看【图1.jpg】)其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。1085
import java.util.Arrays;public class Main { public static boolean judge(int[] A) { int[] tempA = new int[A.length]; for(int i = 0;i < A.length;i++) tempA[i] = A[i]; Arrays.sort(tempA); //对tempA元素进行从小到大排序 for(int i = 1;i < tempA.length;i++) { if(tempA[i - 1] == tempA[i]) return false; } return true; } public static boolean judge1(int[] A, int[] B) { int[] temp = new int[A.length + B.length - 1]; int i = 0; for(;i < A.length;i++) temp[i] = A[i]; for(;i < temp.length;i++) temp[i] = B[i - A.length]; Arrays.sort(temp); for(i = 1;i < temp.length;i++) { if(temp[i - 1] == temp[i]) return false; } return true; } public static void main(String[] args) { for(int i = 1000;i <= 9999;i++) { int[] A = new int[4]; A[0] = i / 1000; A[1] = i / 100 % 10; A[2] = i / 10 % 10; A[3] = i % 10; if(judge(A) == false) continue; for(int j = 1000;j <= 9999;j++) { int[] B = new int[4]; B[0] = j / 1000; B[1] = j / 100 % 10; B[2] = j / 10 % 10; B[3] = j % 10; if(judge(B) == false) continue; if(B[3] != A[1]) continue; if(judge1(A, B) == false) continue; int temp = i + j; if(temp < 9999 || temp > 99999) continue; int[] C = new int[5]; C[0] = temp / 10000; C[1] = temp / 1000 % 10; C[2] = temp / 100 % 10; C[3] = temp / 10 % 10; C[4] = temp % 10; if(C[0] == B[0] && C[1] == B[1] && C[2] == A[2] && C[3] == A[1]) { if(C[4] != A[0] && C[4] != A[1] && C[4] != A[2] && C[4] != A[3]) { if(C[4] != B[0] && C[4] != B[1] && C[4] != B[2] && C[4] != B[3]) System.out.println("i = "+i+", j = "+j+", temp = "+temp); } } } } }}
4 九数组分数
九数组分数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
5 饮料换购
饮料换购乐羊羊饮料厂正在举办一次促销优惠活动。乐羊羊C型饮料,凭3个瓶盖可以再换一瓶C型饮料,并且可以一直循环下去,但不允许赊账。请你计算一下,如果小明不浪费瓶盖,尽量地参加活动,那么,对于他初始买入的n瓶饮料,最后他一共能得到多少瓶饮料。输入:一个整数n,表示开始购买的饮料数量(0<10000)输出:一个整数,表示实际得到的饮料数例如:用户输入:100程序应该输出:149用户输入:101程序应该输出:151资源约定:峰值内存消耗(含虚拟机) < 256MCPU消耗 < 1000ms请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。注意:主类的名字必须是:Main,否则按无效代码处理。
import java.util.Scanner;public class Main { public static void printResult(int n) { int sum = n; while(n > 2) { sum += n / 3; n = n / 3 + n % 3; } System.out.println(sum); return; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); printResult(n); }}
6 生命之树
生命之树在X森林里,上帝创建了生命之树。他给每棵树的每个节点(叶子也称为一个节点)上,都标了一个整数,代表这个点的和谐值。上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点a,b,都存在一个点列 {a, v1, v2, ..., vk, b} 使得这个点列中的每个点都是S里面的元素,且序列中相邻两个点间有一条边相连。在这个前提下,上帝要使得S中的点所对应的整数的和尽量大。这个最大的和就是上帝给生命之树的评分。经过atm的努力,他已经知道了上帝给每棵树上每个节点上的整数。但是由于 atm 不擅长计算,他不知道怎样有效的求评分。他需要你为他写一个程序来计算一棵树的分数。「输入格式」第一行一个整数 n 表示这棵树有 n 个节点。第二行 n 个整数,依次表示每个节点的评分。接下来 n-1 行,每行 2 个整数 u, v,表示存在一条 u 到 v 的边。由于这是一棵树,所以是不存在环的。「输出格式」输出一行一个数,表示上帝给这棵树的分数。「样例输入」51 -2 -3 4 54 23 11 22 5「样例输出」8「数据范围」对于 30% 的数据,n <= 10对于 100% 的数据,0 < n <= 10^5, 每个节点的评分的绝对值不超过 10^6 。资源约定:峰值内存消耗(含虚拟机) < 256MCPU消耗 < 3000ms请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。注意:主类的名字必须是:Main,否则按无效代码处理。对于此题,首先重点在于题意的理解:上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点a,b,都存在一个点列 {a, v1, v2, ..., vk, b} 使得这个点列中的每个点都是S里面的元素,且序列中相邻两个点间有一条边相连。 其中,是对于S中任意两点a,b,存在(看清楚,是存在)一个点列,满足题意要求。对于实例数据,选择点集S = {1,2,4,5},可知1,2直接相连,2,4和2,5都是直接相连,对于4,5存在点列{4,2,5}满足题意。所以,此题要求我们在给定树的情况下,寻找一颗子树,其所有顶点的和谐值和最大。
import java.util.ArrayList;import java.util.Scanner; public class Main{ public static int[] nodeValue; //存放各个顶点的和谐值 public static ArrayList[] edge; //存放各个顶点包含邻接边 public static int[] value; //用于存放某个顶点为根节点情况下,对于和谐值的和 public static int max = 0; //用于记录最终输出结果最大值 public static void dfs(int node, int father) { value[node] = nodeValue[node];//节点node为根节点,刚开始和谐值和为节点本身和谐值 for(int i = 0;i < edge[node].size();i++) { int son = edge[node].get(i); //节点node的孩子节点 if(son == father) //如果遍历到son为根节点时,son的孩子发现为node时,排除 continue; dfs(son, node); //DFS搜索 if(value[son] > 0) value[node] += value[son]; //回溯计算父母连带子节点的和谐值和 max = Math.max(max, value[node]); } } @SuppressWarnings("unchecked") public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); nodeValue = new int[n + 1]; for(int i = 1;i <= n;i++) { int a = in.nextInt(); nodeValue[i] = a; } edge = new ArrayList[n + 1]; value = new int[n + 1]; for(int i = 1;i <= n;i++) edge[i] = new ArrayList (); for(int i = 1;i <= n - 1;i++) { int a = in.nextInt(); int b = in.nextInt(); edge[a].add(b); edge[b].add(a); } dfs(1, -1);//从顶点1开始DFS搜索,设置顶点1的父母节点为-1,即设置顶点1为树的根节点 System.out.println(max); }}