<Algorithm>_solved/<BOJ>_C
[백준 9713] Sum of Odd Sequence :: C언어
meow00
2019. 11. 27. 00:09
문제
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.
* odd integer ; 홀수 정수
평소 영어에 자신이 없어서 영어로 된 문제에 지레 겁을 먹었다. 근데 천천히 읽어보니까 어려운 문제가 아니어서 금방 풀 수 있었다. ^_^♥♥
T에 대한 제한이 없어서 배열에 동적할당을 사용해서 출력을 했다.
학교에서 배운 이래로 직접 적용해 본 건 처음인데 성공적으로 코드가 짜여졌다ㅎㅂㅎ!!
앞으로도 잘 부탁해ㅜㅅㅜ ♥♥
#include <stdio.h> #include <stdlib.h> int main() { int N, T; scanf_s("%d", &T); int *arr; arr = (int *)malloc(sizeof(int)*T); for (int i = 0; i < T; i++) { int sum = 0; scanf("%d", &N); for (int j = 0; j < N; N--) if (N % 2 == 1) sum += N; arr[i] = sum; } for (int i = 0; i < T; i++) { printf("%d\n", arr[i]); } free(arr); return 0; }