-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.c
53 lines (48 loc) · 1.45 KB
/
operations.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#define OPERATION_1 1
#define OPERATION_2 2
#define OPERATION_3 3
void operate(int , int);
int main(){
int num_count, ope_count;
scanf("%d %d", &num_count, &ope_count);
operate(num_count, ope_count);
return 0;
}
void operate(int num_count, int ope_count){
long long * nums = (long long *) malloc(num_count * sizeof(long long));
for(int count = 0; count < num_count; count++){
scanf(" %lld", (nums + count));
}
int operation;
for(int count = 0; count < ope_count; count++){
scanf(" %d", &operation);
long long input;
int left, right;
switch(operation){
case OPERATION_1:
for(int c = 0; c < num_count; c++){
*(nums + c) *= -1;
}
break;
case OPERATION_2:
scanf(" %lld", &input);
for(int c = 0; c < num_count; c++){
*(nums + c) += input;
}
break;
case OPERATION_3:
scanf(" %d %d", &left, &right);
long long result = 0;
int current = left;
while(current <= right){
result += *(nums + current - 1);
current++;
}
printf("%lld\n", result);
break;
}
}
free(nums);
}