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]);
}
}
}
}
}
}
}热门推荐
1 甄嬛传祝福语简短
10 新娘对伴娘祝福语简短
11 白羊生日祝福语 简短独特
12 猪年幽默祝福语简短
13 中秋祝福语给父亲简短
14 新人结婚父母祝福语简短
15 孩子满月随礼简短祝福语
16 60岁长辈祝福语简短
17 送花祝福语简短十一朵
18 求助短信生日祝福语简短