c# - 检查具有给定总和的数组中的对
问题描述
我正在用c#解决这个问题,
编写一个程序,在给定的整数数组中找到一个给定总和 S(如果存在)的序列。
这是它应该如何工作的
输入:{4, 3, 1, 4, 2, 5, 4}, S = 11 输出:“第一个序列:{4, 2, 5}, 4 + 2 + 5 = 11”,“第二个序列:{2 , 5, 4}, 2 + 5 + 4 = 11"
但是我得到的输出是正确的总和,我也尝试了其他数字
直接从控制台输出:11 11
我的代码:
using System.Collections.Generic;
using System.Linq;
class EntryPoint
{
static void Main(string[] args)
{
int[] arr = { 4, 3, 1, 4, 2, 5, 4 };
int S = 11;
int p = 0;
int[] sumSeq = { };
for (int i = 0; i < arr.Length; i++)
{
p = 0;
for (int j = 0; j < arr.Length; j++)
{
if (arr[j]!=arr[i])
{
p += arr[j] + arr[i];
if (p == S)
{
Console.WriteLine(p);
break;
}
}
}
}
}
}
解决方案
推荐阅读
- javascript - 如何实现长滚动行为
- video - 自动视频创建 API
- node.js - 即使电子邮件未送达也能成功响应
- android - 如何使用 DevicePolicyManger API 中的 installCaCert 方法?
- r - 轴标签大于ggplot2中的绘图输出
- database - 非规范化多对多关系Hbase
- protractor - 无法单击量角器中的元素
- ruby - 如何在 Ruby 中检查一个完美的正方形?
- angular - Angular 5 AngularFire Firebase 登录闪烁
- sql - SQL TOP 10 已售产品,按 itemno 分组,按客户分组