프로그래밍 문제/c# 문제
c# 문제 55. 배열에서 두 요소의 합이 특정 값이 되는 모든 요소 쌍 찾기
지나팩
2023. 12. 15. 16:54
주어진 배열의 두 요소의 합이 입력 받은 값이 되는 모든 요소 쌍 찾는 코드를 작성하세요.
int[] array = { 1, 2, 3,4,5,6,7,8,9,-1,-2,-3,-4,-5};
출력 예)
코드는 아래에~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
using System;
class Program
{
static void Main()
{
int[] array = { 1, 2, 3,4,5,6,7,8,9,-1,-2,-3,-4,-5};
int target = 0;
Console.WriteLine("타겟 값을 입력하세요: ");
target = int.Parse(Console.ReadLine());
Console.WriteLine($"배열 내 요소의 합이 {target}이 되는 쌍:");
for (int i = 0; i < array.Length; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] + array[j] == target)
{
Console.WriteLine($"({array[i]}, {array[j]})");
}
}
}
}
}
|
cs |