1.设计一个A类,实现对一维数组的指定范围内的元素的相加运算
源代码:
1 package s001; 2 import java.util.Scanner; 3 public class A { 4 5 6 public static int count(int[]arr,int start,int end,int sum){ 7 8 9 for(int i=start;i<=end;i++) 10 sum+=arr[i]; 11 return sum; 12 } 13 public static void main(String[] args) { 14 15 int s=0; 16 17 int []a=new int[]{-2574,-4010,8243,771,2447,-5197,2556,8044,3314,3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37,-1520,164,2055,-5936,5912,1717,5988,4781,5757,892,-4394,8034,2213,-1080,-2080,5364,106,2657,566,3940,-5116,4583,1806,6555,2621,-7197,528,1626,18,1049,6243,3198,4397,-1325,9087,936,-6291,662,-178,135,-3473,-2385,-165,1713,-7949,-4234,1138,2212,104,6968,-3632,3801,1137,-1296,-1215,4272,6223,-5922,-7723,7044,-2938,-8180,1356,1159,-4022,-3713,1158,-8715,-4081,-2541,-2555,-2284,461,940,6604,-3631,3802,-2037,-4354,-1213,767}; 18 19 Scanner sc=new Scanner(System.in); 20 System.out.println("请依次输入m、n:"); 21 int m= sc.nextInt(); 22 int n=sc.nextInt(); 23 System.out.println("m为:" +m); 24 System.out.println("n为:" +n); 25 26 A.count(a,m,n,s ); 27 28 System.out.println("sum="+A.count(a, m, n, s)); 29 } 30 }
A测试:
验证:
2.设计一个B类,实现对二维数组内指定范围内元素的相加运算
1 package s001; 2 import java.util.Scanner; 3 4 public class B { 5 6 public static int count(int[][]arr,int start1,int start2,int end1,int end2,int sum){ 7 8 for(int i=start1;i<=end1;i++) 9 { 10 for(int j=start2;j<=end2;j++) 11 { 12 sum+=arr[i][j]; 13 } 14 } 15 return sum; 16 17 } 18 19 public static void main(String[] args) { 20 21 int s=0; 22 int[][] b=new int[][]{{-2574,-4010,8243,771,2447,-5197,2556,8044,3314,3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37,-1520,164,2055,-5936,5912,1717,5988,4781,5757,892,-4394,8034,2213,-1080,-2080,5364,106,2657,566,3940,-5116,4583,1806,6555,2621,-7197,528,1626,18,1049,6243,3198,4397,-1325,9087,936,-6291,662,-178,135,-3473,-2385,-165,1713,-7949,-4234,1138,2212,104,6968,-3632,3801,1137,-1296,-1215,4272,6223,-5922,-7723,7044,-2938,-8180,1356,1159,-4022,-3713,1158,-8715,-4081,-2541,-2555,-2284,461,940,6604,-3631,3802,-2037,-4354,-1213,767},{2740,-4182,-5632,-2966,-1953,567,-8570,1046,2211,1572,-2503,-1899,3183,-6187,3330,3492,-464,-2104,316,8136,470,50,466,-1424,5809,2131,6418,-3018,6002,-8379,1433,1144,2124,1624,-602,-5518,5872,870,-5175,-3961,-427,-6284,2850,481,6175,141,-766,-1897,-748,-4248,366,4823,3003,1778,3256,2182,2253,5076,5540,-2650,2451,-1875,5482,-6881,-329,-969,-8032,-2093,612,1524,-5492,5758,-7401,-5039,3241,6338,3581,4321,-1072,4942,2131,210,-7045,-7514,7450,-1142,-2666,-4485,-639,2121,-5298,-3805,-1686,-2520,-1680,2321,-4617,-1961,2076,7309}}; 23 24 Scanner sc = new Scanner(System.in); 25 System.out.println("请输入m1,n1,m2,n2:"); 26 27 int m1 = sc.nextInt(); 28 int n1 = sc.nextInt(); 29 30 System.out.println("m1=" +m1); 31 System.out.println("n1=" +n1); 32 33 int m2 = sc.nextInt(); 34 int n2= sc.nextInt(); 35 36 System.out.println("m2=" +m2); 37 System.out.println("n2=" +n2); 38 39 B.count(b,m1,n1,m2,n2,s ); 40 System.out.println("sum="+B.count(b,m1,n1,m2,n2,s )); 41 } 42 43 }
B测试:
验证:
3.设计一个C类,实现对三维数组的指定范围内的元素相加运算
1 package s001; 2 import java.util.Scanner; 3 4 public class C { 5 6 public static int count(int[][][]arr,int start1,int end1,int start2,int end2,int start3,int end3,int sum){ 7 8 for(int i=start1;i<=end1;i++) 9 { 10 for(int j=start2;j<=end2;j++) 11 { 12 for(int k=start3;k<=end3;k++) 13 { 14 sum+=arr[i][j][k]; 15 16 } 17 18 } 19 } 20 return sum; 21 22 } 23 public static void main(String[] args) { 24 25 int s=0; 26 int[][][] c = new int[][][]{ 27 {{-2574,-4010,8243,771,2447,-5197,2556,8044,3314,3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37,-1520,164,2055,-5936,5912,1717,5988,4781,5757,892,-4394,8034,2213,-1080,-2080,5364,106,2657,566,3940,-5116,4583,1806,6555,2621,-7197,528,1626,18,1049,6243,3198,4397,-1325,9087,936,-6291,662,-178,135,-3473,-2385,-165,1713,-7949,-4234,1138,2212,104,6968,-3632,3801,1137,-1296,-1215,4272,6223,-5922,-7723,7044,-2938,-8180,1356,1159,-4022,-3713,1158,-8715,-4081,-2541,-2555,-2284,461,940,6604,-3631,3802,-2037,-4354,-1213,767}}, 28 {{2740,-4182,-5632,-2966,-1953,567,-8570,1046,2211,1572,-2503,-1899,3183,-6187,3330,3492,-464,-2104,316,8136,470,50,466,-1424,5809,2131,6418,-3018,6002,-8379,1433,1144,2124,1624,-602,-5518,5872,870,-5175,-3961,-427,-6284,2850,481,6175,141,-766,-1897,-748,-4248,366,4823,3003,1778,3256,2182,2253,5076,5540,-2650,2451,-1875,5482,-6881,-329,-969,-8032,-2093,612,1524,-5492,5758,-7401,-5039,3241,6338,3581,4321,-1072,4942,2131,210,-7045,-7514,7450,-1142,-2666,-4485,-639,2121,-5298,-3805,-1686,-2520,-1680,2321,-4617,-1961,2076,7309}}, 29 {{302,-7944,-6551,6648,-884,-6332,1188,-248,3902,-2171,-3782,-3510,6203,-636,-4605,4497,3187,-6278,1198,-7963,7647,-3022,-8267,7995,-1998,-4171,730,-8117,-1706,7601,-2454,-6161,-1810,243,-2841,-5377,2985,2328,177,5622,3596,3057,2830,-1151,6566,2254,6972,-3060,4063,367,84,3156,3880,-1530,5272,1385,-565,-1736,1267,-3863,-229,2072,2423,-7449,9555,2844,-1278,-3613,-919,4153,-4232,4032,-5732,199,4792,2498,-3344,-4193,2650,-579,3416,-3575,1822,-6936,-2661,-1873,414,2314,6844,7613,-7844,6680,5974,2208,-3597,6302,4396,-141,-1864,1939}} 30 31 }; 32 Scanner sc= new Scanner(System.in) ; 33 System.out.println("请输入m1,n1,m2,n2,m3,n3:"); 34 int m1 = sc.nextInt(); 35 int n1 = sc.nextInt(); 36 System.out.println("m1=" +m1); 37 System.out.println("n1=" +n1); 38 39 int m2 = sc.nextInt(); 40 int n2= sc.nextInt(); 41 System.out.println("m2=" +m2); 42 System.out.println("n2=" +n2); 43 44 int m3 = sc.nextInt(); 45 int n3= sc.nextInt(); 46 System.out.println("m3=" +m3); 47 System.out.println("n3=" +n3); 48 49 C.count(c,m1,n1,m2,n2,m3,n3,s ); 50 System.out.println("sum="+C.count(c,m1,n1,m2,n2,m3,n3,s )); 51 } 52 53 54 }
C测试:
验证:
小结:
最开始做这个题目的时候,没有理解老师的意思,完全跑题了。后来看了班上同学和老师们的聊天记录,才知道自己想偏了。可是还是不知道从何下手,后来参考了一下林杰同学做的,有一点点思路了却还是不怎么明确。后来看到了宝玉老师和鞠老师说的,有了一点点启发。迫于作业截止时间快到了,就先把做的东西发上来,再慢慢改进吧,看到这么繁琐的代码我也是醉了。希望老师可以指导一下
时间: 2024-10-12 13:41:06