백준 3052 나머지
c++문제풀이
문제
두 자연수 A와 B가 있을 때, A%B는 A를 B로 나눈 나머지 이다. 예를 들어, 7, 14, 27, 38을 3으로 나눈 나머지는 1, 2, 0, 2이다. 수 10개를 입력받은 뒤, 이를 42로 나눈 나머지를 구한다. 그 다음 서로 다른 값이 몇 개 있는지 출력하는 프로그램을 작성하시오.
입력
첫째 줄부터 열번째 줄 까지 숫자가 한 줄에 하나씩 주어진다. 이 숫자는 1,000보다 작거나 같고, 음이 아닌 정수이다.
출력
첫째 줄에, 42로 나누었을 때, 서로 다른 나머지가 몇 개 있는지 출력한다.
예제 입력
39 40 41 42 43 44 82 83 84 85
예제 출력
6
배열의 크기가 명시적으로 지정되어있고 일부 배열요소가 초기화된경우, 초기화되지않은 index에 대한 모든 value는 0 으로 세팅된다.배열 초기화. If an explicit array size is specified, but a shorter initialization list is specified, the unspecified elements are set to zero.
int array_1[5] = { 1,2,3 }; //{1,2,3,0,0}
int array_2[5]; //garbage value
int array_3[5] = {0}; //{0,0,0,0,0}
- zero initialization
#include <stdio.h>
int i=10, a[45], t, cnt;
main()
{
while(i--)
{
scanf("%d", &t);
a[t%42]=1;
}
for(i=0; i<45; i++)
cnt+=a[i];
printf("%d", cnt);
}
- my original code
#include <stdio.h>
int main()
{
int arr[10];
for (int i = 0; i<10; i++)
scanf("%d", &arr[i]);
int remainder[10];
for (int i = 0; i<10; i++)
remainder[i] = arr[i]%42;
int cnt = 0;
for (int i = 0; i < 9; i++)
{
for (int j = i + 1; j < 10; j++)
{
if(remainder[i] == remainder[j])
{
cnt++;
break; //this is mandatory
}
}
}
printf("%d", 10 - cnt);
return 0;
}