[c] 백준 9713번_Sum of Odd Sequence
출처: www.acmicpc.net/problem/9713
9713번: Sum of Odd Sequence
First line of the input contains T, the number of test cases. Each test case contains a single integer N. N is between 1 and 100.
www.acmicpc.net
Sum of Odd Sequence 성공출처다국어분류
문제
Given an odd integer N, calculate the sum of all the odd integers between 1 and N inclusive.
입력
First line of the input contains T, the number of test cases. Each test case contains a single integer N. N is between 1 and 100.
출력
For each test case output the value 1+3+….+N.
예제 입력
10 1 3 5 7 9 11 13 15 17 19
예제 출력
1 4 9 16 25 36 49 64 81 100
처음에 예제를 보고 첫 입력 값을 보고 헷갈렸다. 예제에서 10은 두 번째부터 입력하는 홀수의 개수라고 볼 수 있다.
입력에서 N개를 입력할지 정하고, 그 뒤로 홀수 값을 입력해주면 출력에서는 N개의 숫자를 계속해서 차례로 더해준다.
1
2
|
int T;
int n = 0, sum[100] = { 0 }, a[100] = { 0 };
|
cs |
먼저 처음에 입력하는 개수를 의미하는 값을 T로, 총합은 n, 그리고 합을 나타내는 배열 sum[100]과 T개만큼 나타낼 배열을 a[100]을 먼저 선언해준다.
1
2
|
scanf("%d", &T);
for (int i = 1; i <= T; i++) scanf("%d", &a[i]);
|
최종 코드는 다음과 같다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <stdio.h>
int main(void) {
int T;
int n = 0, sum[100] = { 0 }, a[100] = { 0 };
scanf("%d", &T);
for (int i = 1; i <= T; i++) scanf("%d", &a[i]);
for (int i = 1; i<=T; i++) {
for (int j = 1; j <= a[i]; j++) {
if (j % 2 != 0) sum[n] += j;
}
printf("%d\n", sum[n]);
n++;
}
}
|
cs |