给定条件的数组总和

问题是打印具有以下条件的数组的总和:

如果数组有 6 和 7 的连续顺序,则忽略 6 和 7 之间的数字并考虑其他数字来计算总和。

样本输出:

输出:22 表示 [10,3,6,1,2,7,9] [即 10+3+9]

输出:19 代表 [7,1,2,3,6]

输出:12:10 for [1,6,4,7,9, 1]

我的代码:

int sum=0;

for (int i=0; i<n.length-1; i++) {

    if (n[i]==6 && n[i+1]==7) {

         continue;

     }

     else{

         sum+=n[i];

     }

}

System.out.println(sum);

我没有得到预期的输出。我知道我的逻辑并不完美,但我不知道该怎么做。因此,任何帮助或指导都会非常有帮助。提前致谢。


尚方宝剑之说
浏览 102回答 4
4回答

动漫人物

可以通过查找 7 的嵌套循环来完成,尽管这会变得很棘手,具体取决于我们应该如何处理嵌套的 6 和 7。在下面的示例 6、6、7 中,中间的 6 被忽略:int[] n = {1,6,3,7,6,6,7,1};int sum = 0;outer:for (int i = 0; i < n.length; i++) {&nbsp; &nbsp; if (n[i] == 6) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = i + 1; j < n.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n[j] == 7) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue outer;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; sum += n[i];}System.out.println(sum); // 2

达令说

public static void main(String[] args) {&nbsp; &nbsp; //int arr[]= {10,3,6,1,2,7,9};&nbsp; &nbsp; //int arr[]= {7,1,2,3,6};&nbsp; &nbsp; int arr[]= {1,6,4,7,9};&nbsp; &nbsp; int n=arr.length; //the length of the array&nbsp; &nbsp; int sum1=0,sum2=0,sum3=0;&nbsp; &nbsp; boolean flag=false;&nbsp; &nbsp; for(int i=0;i<=n-1;i++) {&nbsp; &nbsp; &nbsp; &nbsp; for(int j=n-1;j>=1;j--) {&nbsp; &nbsp; &nbsp; &nbsp; if(arr[i]==6 && arr[j]==7 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag= true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;if(flag) {&nbsp; &nbsp; for(int i=0;i<=n-1;i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(arr[i]==6) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum1+=arr[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for(int j=n-1;j>=1;j--) {&nbsp; &nbsp; &nbsp; &nbsp; if(arr[j]==7) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum2+=arr[j];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; int result=sum1+sum2;&nbsp; &nbsp; System.out.println(result);}else {&nbsp; &nbsp; for(int k=0;k<n;k++) {&nbsp; &nbsp; &nbsp; &nbsp; sum3+=arr[k];&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(sum3);}}

哈士奇WWW

&nbsp;int[] n = {10,3,6,1,2,7,9};&nbsp;int sum = 0,d=0;&nbsp; &nbsp;&nbsp;&nbsp;for(int i=0;i<n.length-1;i++){&nbsp; &nbsp; &nbsp;if(n[i]==6){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int j=n.length-1;j>=0;j--){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(n[j]==7)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d=d+n[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;sum=sum+n[i];&nbsp; }&nbsp;System.out.println(d+sum);&nbsp;&nbsp;}

守着星空守着你

&nbsp; &nbsp; int sum=0,i;&nbsp; &nbsp; boolean add = true;&nbsp; &nbsp; int[] array = new int[]{1,6,4,7,9};&nbsp; &nbsp; for(i=0; i<array.length;i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(array[i] != 6 && add==true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum+array[i];&nbsp; &nbsp; &nbsp; &nbsp; else if(array[i] == 6){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((i+1) == array.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum+array[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(array[i] == 7)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add = true;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(sum);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java