C#程序确定数组中的任何两个整数之和是否等于给定的整数
以下是我们的数组-
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
};
//给定整数
int res = 8;
Console.WriteLine("Given Integer {0}: ", res);
Console.WriteLine("总和:");
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]);
}
}
}
}
}
}
}热门推荐
2 祝福语简短七夕
10 出院祝福语简短8字
11 撩妹生日祝福语简短
12 新婚抖音祝福语简短
13 送给同桌的祝福语简短
14 水晶婚夫妻祝福语简短
15 简短的高考祝福语精选
16 小朋友祝福语简短的话
17 女侠老师祝福语简短
18 古风婚礼祝福语唯美简短