给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1≤i≤j≤K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。
本题旨在测试各种不同的算法在各种数据情况下的表现。各组测试数据特点如下:
输入第1行给出正整数K (≤100000);第2行给出K个整数,其间以空格分隔。
在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。
6
-2 11 -4 13 -5 -2
20
实现方法:
方法一:在线处理
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int k = in.nextInt(); int sum = 0; int maxsum = 0; int[] indata = new int[k]; for(int i=0;i方法二:分而治之for(int i=0;i if(sum<0){ sum = 0 ; } if(sum>maxsum){ maxsum = sum; } } System.out.println(maxsum); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int k = in.nextInt(); int sum = 0; int maxsum = 0; int[] indata = new int[k]; for(int i=0;i/* //方案一 for(int i=0;i sum += indata[i]; if(sum<0){ sum = 0 ; } if(sum>maxsum){ maxsum = sum; } } */ maxsum = MaxSubseq(indata,0,k-1); System.out.println(maxsum); } public static int Max3(int a,int b,int c){ return a>b ? a>c ? a:c : b>c? b:c; } public static int MaxSubseq(int indata[],int left,int right){ int maxLeftSum,maxRightSum; int maxLeftBoardSum,maxRightBoardSum; int boardSum; int center; if(left==right){ if (indata[left]>0){ return indata[left]; } else { return 0; } } center = (left + right) / 2; maxLeftSum = MaxSubseq(indata,left,center); maxRightSum = MaxSubseq(indata,center+1,right); boardSum = 0; maxRightBoardSum = 0; for(int i=center+1;i<=right;i++){ boardSum += indata[i]; if (boardSum > maxRightBoardSum){ maxRightBoardSum = boardSum; } } boardSum = 0; maxLeftBoardSum = 0; for(int i=center;i>=left;i--){ boardSum += indata[i]; if(boardSum > maxLeftBoardSum){ maxLeftBoardSum = boardSum; } } return Max3(maxLeftSum,maxRightSum,maxLeftBoardSum+maxRightBoardSum); } }
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
10
-10 1 2 3 4 -5 -23 3 7 -21
10 1 4
设计函数分别求两个一元多项式的乘积与和。
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0
。
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[][] poly1= SavePoly(in); int[][] poly2= SavePoly(in); //PrintPoly(poly1); //PrintPoly(poly2); int[][] polyMulty= MultyPoly(poly1,poly2); PrintPoly(polyMulty); System.out.println(); int[][] polyAdd= AddPoly(poly1,poly2); PrintPoly(polyAdd); } public static void PrintPoly(int[][] poly){ for(int i=0;i0].length;i++){ if(poly[0][i]==0){ if(i==0) System.out.printf("%d %d",0,0); break; } if (i==0) System.out.printf("%d %d",poly[0][i],poly[1][i]); else System.out.printf(" %d %d",poly[0][i],poly[1][i]); } } public static int[][] SavePoly(Scanner in){ int m = in.nextInt(); if (m==0){ int[][] poly = new int[2][1]; return poly; } int[][] poly = new int[2][m]; for(int i =0;i 0][i]=in.nextInt();//多项式系数 poly[1][i]=in.nextInt();//多项式指数 } return poly; } public static int[][] MultyPoly(int[][] poly1,int[][] poly2){ int m1 = poly1[0].length; int m2 = poly2[0].length; int i1 = 0; int i2 = 0; int[][] polyMulty = new int[2][m1*m2]; int[][] polyTemp = new int[2][m2]; for(int i=0;i for(int j=0;j 0][j]=poly2[0][j]*poly1[0][i]; polyTemp[1][j]=poly2[1][j]+poly1[1][i]; } polyMulty = AddPoly(polyMulty,polyTemp); } return polyMulty; } public static int[][] AddPoly(int[][] poly1,int[][] poly2){ int m1 = poly1[0].length; int m2 = poly2[0].length; int i1=0; int i2=0; int order1=0; int order2=0; int last = 0; int[][] polyAdd = new int[2][m1+m2]; while((i1 if(i1 1][i1]; else order1 = -9999; if(i2 1][i2]; else order2 = -9999; if(order1 == order2){ if(poly1[0][i1]+poly2[0][i2]!=0) { polyAdd[0][last] = poly1[0][i1] + poly2[0][i2]; polyAdd[1][last] = poly1[1][i1]; i1++; i2++; last++; } else{ i1++; i2++; } } if(order1>order2){ polyAdd[0][last]=poly1[0][i1]; polyAdd[1][last]=poly1[1][i1]; i1++; if(polyAdd[0][last]!=0) { last++; } } if(order1 0][last]=poly2[0][i2]; polyAdd[1][last]=poly2[1][i2]; i2++; if(polyAdd[0][last]!=0) { last++; } } } return polyAdd; } }
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
图1
图2
输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。
如果两棵树是同构的,输出“Yes”,否则输出“No”。
8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -
Yes
8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4
No
import java.util.Scanner; class TreeNode { String val; int left; int right; TreeNode(String x,int y,int z){this.val = x;this.left = y;this.right = z;} } class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num1 = in.nextInt(); TreeNode[] tree1 = new TreeNode[num1]; int root1 = buildTree(in,tree1,num1); int num2 = in.nextInt(); TreeNode[] tree2 = new TreeNode[num2]; int root2 = buildTree(in,tree2,num2); if(isomorphism(tree1,root1,tree2,root2)){ System.out.println("Yes"); } else { System.out.println("No"); } } public static boolean isomorphism(TreeNode[] tree1,int root1,TreeNode[] tree2,int root2) { if (root1 == -1 && root2 == -1) { return true; } if ((root1 == -1 && root2 != -1) || (root1 != -1 && root2 ==-1)) { return false; } if (!tree1[root1].val.equals(tree2[root2].val)) { return false; } if ((tree1[root1].left == -1) && (tree2[root2].left == -1)) { return isomorphism(tree1, tree1[root1].right, tree2, tree2[root2].right); } if ((tree1[root1].left != -1) && (tree2[root2].left != -1) &&(tree1[tree1[root1].left].val.equals(tree2[tree2[root2].left].val))) { return isomorphism(tree1,tree1[root1].right,tree2,tree2[root2].right) && isomorphism(tree1,tree1[root1].left,tree2,tree2[root2].left); } else{ return isomorphism(tree1,tree1[root1].left,tree2,tree2[root2].right) && isomorphism(tree1,tree1[root1].right,tree2,tree2[root2].left); } } public static int buildTree(Scanner in,TreeNode[] res,int num){ int[] flag = new int[num]; String val; int left,right; for(int i =0;istr2int(in.next(),flag); right = str2int(in.next(),flag); res[i] = new TreeNode(val,left,right); } for(int i = 0;i if(flag[i]==0) return i; } return -1; } public static int str2int(String x,int[] flag){ if(x.equals("-")){ return -1; } int i = Integer.parseInt(x); flag[i] = 1; return i; } }