
给定一个数组 arr[m],其中包含 m 个整数和 n(要添加到数组中的值),并给出 r 个查询,并给出一些开始和结束。对于每个查询,我们必须在数组中添加从开头到限制末尾的值 n。
示例
Input:
arr[] = {1, 2, 3, 4, 5}
query[] = { { 0, 3 }, { 1, 2 } }
n = 2
Output:
If we run above program then it will generate following output:
Query1: { 3, 4, 5, 6, 5 }
Query2: { 3, 6, 7, 6, 5 }
这个程序可以通过一种简单的方法来解决,其中 -
- 我们将迭代所有查询,从查询的起点开始遍历数组,直到存储在查询中的终点。
- 添加 n 的值并打印数组。
算法
START
STEP 1 : DECLARE A STRUCT range for start AND end LIMITS
STEP 2 : IN FUNCTION add_tomatrix(int arr[], struct range r[], int n, int size, int m)
int i, j, k;
LOOP FOR i = 0 AND i <h2>示例</h2><pre class="brush:php;toolbar:false;">#include <stdio.h>
struct range{
int start, end; //struct to give the range for the array elements
};
int add_tomatrix(int arr[], struct range r[], int n, int size, int m){
int i, j, k;
for ( i = 0; i ");
}
}
int main(int argc, char const *argv[]){
int arr[] ={3, 4, 8, 1, 10};
struct range r[] = {{0,2}, {1, 3}, {3, 4}};
int n = 2;
int size = sizeof(arr)/sizeof(arr[0]);
int m = sizeof(r)/sizeof(r[0]);
add_tomatrix(arr, r, n, size, m);
return 0;
}</stdio.h>
输出
如果我们运行上面的程序,那么它将生成以下输出 -
Query 1: 5 6 10 1 10 Query 2: 5 8 12 3 10 Query 3: 5 8 12 5 12











