第一个和最后一个备用元素的总和 [英] Sum of first and last alternate elements

查看:102
本文介绍了第一个和最后一个备用元素的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个大小为N的整数数组,打印第一个和最后一个数字的总和,第二个第一个和第二个数字等等。



注意:N是总是连。



边界条件:

1< = N< = 9999



输入格式:

第一行包含数组的大小N

第二行包含由空格分隔的N个整数。



输出格式:

第一行包含指定的输出。



示例输入/输出1 :

输入:

6

2 9 1 5 3 2



输出:

4 12 6



示例输入/输出2:

输入:

10

97 94 66 99 17 78 70 44 67 86



输出:

183 161 110 169 95



我的尝试:



  #include   < span class =code-keyword><   stdio.h  >  
#include < stdlib.h >

int main()
{
int i,j,n,a [ 9999 ],b [ 9999 ];
scanf( %d,& n);
for (i = 0 ; i< n; i ++)
{
scanf( %d,& a [i]);
}
for (i = 0 ; i< n; i ++)
for (j = n- 1 ; j> = 0 ; j--)
{
b [i] = a [i] + a [j];
}
for (i = 0 ; i< n; i ++)
printf( %d,b [i]);


}

解决方案

你不需要两个嵌套for循环,而是一个循环有两个变量。

  int  i,j; 
for (i = 0 ,j = N - 1 ; i< = j; ++ i, - j)
b [i] = a [i] + a [j];


引用:

我在输出中得到addtionaly 3个零



因为您还需要更正打印部件。

  (i =  0 ; i< n  / 2 ; i ++)
printf( %d,b [i]);





[更新]

一点点分析表明 a 一旦 b 被计算,就永远不会重复使用=>你不需要 b

  #include   <   stdio.h  >  
#include < stdlib.h >

int main()
{
int i,j ,n,a [ 9999 ] ,b [ 9999 ] 的;
scanf( %d,& n);
for (i = 0 ; i< n; i ++)
{
scanf( %d,& a [i]);
}
for (i = 0 ,j = n- 1 ; i< j; i ++,j--)
{
b [i] = a [i] + a [ j];
a [i] = a [i] + a [j] ;
}
for (i = 0 ; i< n / 2; i ++ )
printf( %d,b [i] );
printf( %d,< b> A [I]);
}



内存占用空间刚刚除以2,可以更进一步。


#include< ; stdio.h>

#include< stdlib.h>



int main()

{

int i,n,a [9999],b [9999];

scanf(%d,& n);

for(i = 0; i< n; i ++)

{

scanf(%d,& a [i]);

}

for(i = 0; i< n / 2; i ++)

{

int j = in-1;

b [i] = a [i] + a [j];

}

for(i = 0; i< n / 2 ; i ++)

printf(%d,b [i]);

返回0;

}


Given an integer array of size N, print the sum of first and last number, second first and second last number and so on.

Note: N is always even.

Boundary Condition:
1<= N <= 9999

Input Format:
The first line contains the size of the array N
The second line contains N integers separated by space.

Output Format:
The first line contains the output as specified.

Example Input/Output 1:
Input:
6
2 9 1 5 3 2

Output:
4 12 6

Example Input/Output 2:
Input:
10
97 94 66 99 17 78 70 44 67 86

Output:
183 161 110 169 95

What I have tried:

#include<stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,n,a[9999],b[9999];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    for(j=n-1;j>=0;j--)
    {
    b[i]=a[i]+a[j];
    }
    for(i=0;i<n;i++)
    printf("%d ",b[i]);


}

解决方案

You do not need two nested for loops, but rather a single loop with two variables.

int i, j;
for (i = 0, j = N - 1; i <= j; ++i, --j)
   b[i] = a[i] + a[j];


Quote:

i am getting addtionaly 3 zeros in the output


Because you need to correct the printing part too.

for(i=0;i<n/2;i++)
    printf("%d ",b[i]);



[Update]
A little analyze show that a is never reused once b is calculated => you don't need b

#include<stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,n,a[9999],b[9999];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0,j=n-1;i<j;i++,j--)
    {
        b[i]=a[i]+a[j];
        a[i]=a[i]+a[j];
    }
    for(i=0;i<n/2;i++)
        printf("%d ",b[i]);
        printf("%d ",a[i]);
}


the memory footprint have just been divided by 2 and it is possible to go further.


#include<stdio.h>
#include <stdlib.h>

int main()
{
int i,n,a[9999],b[9999];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n/2;i++)
{
int j=i-n-1;
b[i]=a[i]+a[j];
}
for(i=0;i<n/2;i++)
printf("%d ",b[i]);
return 0;
}


这篇关于第一个和最后一个备用元素的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆