[백준 14909] 양수 개수 세기 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
주어진 정수들 중 양수의 개수를 세는 문제입니다.
접근법
입력 라인을 공백으로 분리하여 각 숫자를 확인합니다.
0보다 큰 경우만 카운트하여 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
using System;
class Program {
static void Main() {
var tokens = Console.ReadLine()!.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var cnt = 0;
foreach (var s in tokens)
if (int.Parse(s) > 0) cnt++;
Console.WriteLine(cnt);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x, cnt = 0;
while (cin >> x)
if (x > 0) cnt++;
cout << cnt << "\n";
return 0;
}