卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章20321本站已运行347

C# 程序确定数组中的任意两个整数之和是否为给定整数

<?xml encoding="utf-8" ?>

C# 程序确定数组中的任意两个整数之和是否为给定整数

The following is our array −

int[] arr = new int[] {
   7,
   4,
   6,
   2
};

Let’s say the given intger that should be equal to sum of two other integers is −

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]);
         }
      }
   }
}

Example

的中文翻译为:

示例

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]);
                  }
               }
            }
         }
      }
   }
}
卓越飞翔博客
上一篇: php有哪些高效文本数据库
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏