【问题标题】:Dividing the array and subtracting array elements from each block: C program将数组相除并从每个块中减去数组元素:C程序
【发布时间】:2017-08-19 17:10:36
【问题描述】:

我有一个包含 100 个数字的数组,例如 [1,2,3,4,5,6....98,99,100]。我想将它分成 25 组,每组包含 4 个元素,然后用一个块的每个元素减去其他块的元素。例如:如果 25 个块中有 3 个被标记为 A、B、C,并且包含以下元素:

A [1,2,3,4], 
B[5,6,7,8] &
C[9,10,11,12]

那么减法是这样进行的:

(A-B, A-C),
(B-A, B-C) & 
(C-A, C-B) 

1-5,1-6,1-7,1-8,1-9,1-10,1-11,1-12; then 
2-5,2-6,2-6,2-8,2-9,2-10,2-11,2-12; then 
3-5,3-6,3-7,3-8,3-9,3-10,3-11,3-12; then 
4-5,4-6,4-7,4-8,4-9,4-10,4-11,4-12; 

那么 5-1,5-2,5-3,5-4,5-9,5-10,5-11,5-12; 和明智的一样..

谁能帮助我为此编写 C 程序。 我编写的代码是部分的,并没有完全完成上述任务。代码是:

#include <stdio.h>
#include <conio.h>

void main()
{
  int a[100]={1,2,3,4.....,98,99,100};
  int i=0, j=0;
  int x[100], y[100];
  // considering only 12 numbers for the sake of simplicity
  for (i=0;i<12;i++)
  {
    for(j=0;j<8;j++)
    {
      x[j] = a[i] - a[r+4];
    }
    y[i] = x[i];
  }
}

【问题讨论】:

  • 告诉我们你到目前为止做了什么。
  • 我们不为人们写程序'没有自己的尝试
  • 需求/算法还是不清楚:(
  • 使用代码标签,良好的缩进帮助使您的代码可读。
  • 你好 A.B.,我们不在这里写完整的代码。请选择一种语言,尝试自己解决问题,并在遇到困难时返回显示您的代码。

标签: c


【解决方案1】:
#include <stdio.h>

int main(void) {

    int N, i;
    scanf("%d", &N);

    int numArray[N]; // Define an array of four integers

    // Get inputs for the array elements
    for (i=0;i<N; i++) {
        scanf("%d", &numArray[i]);
    }

    int sum = 0;
    // Write here the logic to add these integers:
    for (i=0;i<N;i++) sum += numArray[i];



    printf("%d\n",sum);  // Print the sum

    return 0;
}

【讨论】:

  • 您好,您回答了一个应该关闭的问题,因为它与本网站无关。这可能会导致投反对票并鼓励用户提出离题的问题。请改为标记离题问题。
猜你喜欢
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
相关资源
最近更新 更多