다음은 우리의 배열입니다 -
int[] arr = new int[] {
7,
4,
6,
2
}; 다른 두 정수의 합과 같아야 하는 주어진 정수가 -
라고 가정해 보겠습니다.int res = 8;
합을 구하고 평등을 구하려면.
for (int i = 0; i < arr.Length; i++) {
for (int j = 0; j < arr.Length; j++) {
if (i != j) {
int sum = arr[i] + arr[j];
if (sum == res) {
Console.WriteLine(arr[i]);
}
}
}
} 예
using System;
using System.Collections.Generic;
namespace Demo {
public class Program {
public static void Main(string[] args) {
int[] arr = new int[] {
7,
4,
6,
2
};
// given integer
int res = 8;
Console.WriteLine("Given Integer {0}: ", res);
Console.WriteLine("Sum of:");
for (int i = 0; i < arr.Length; i++) {
for (int j = 0; j < arr.Length; j++) {
if (i != j) {
int sum = arr[i] + arr[j];
if (sum == res) {
Console.WriteLine(arr[i]);
}
}
}
}
}
}
}