- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在编写一个程序,用于查找总和最接近 0 的最大长度子数组。其实我想解决这个问题:http://opc.iarcs.org.in/index.php/problems/DEVIOUS ,但是当我提交我的代码时,它只对 50% 的测试用例给出了正确答案。我的代码运行良好,甚至为我自己的测试用例给出了正确的答案,所以我需要一个测试用例来证明我的代码/算法是错误的,这样我才能找到我的代码中的错误。
这是我的算法,
- Create an array 'temp' equal to the size of the input array, initialize all the elements of temp such that, temp[i] = sum of all elements up to input[i]
- Sort the array temp, find 'i' in temp such that temp[i-1] - temp[i] is closest to 0
这是我解决上面链接问题的代码。
#include
#include
#include
#include
sử dụng không gian tên std;
struct element{
int index;
int value;
};
bool compare(element a, element b){
return a.value < b.value;
}
int chính(){
số nguyên n;
cin >> n;
int array [n];
element elements[n];
int sum = 0;
for(int i = 0 ;i < n; i ++){
cin >> array[i];
sum = sum + array[i];
elements[i].index = i;
elements[i].value = sum;
}
sort(elements,elements+n,compare);
int diff = INT_MAX;
int i1 = 0,i2 = 0;
for(int i = 1;i < n;i ++){
int temp = elements[i].value - elements[i-1].value;
if(temp < diff){
diff = temp;
i1 = elements[i].index;
i2 = elements[i-1].index;
}
else if(temp == diff){
if(abs(elements[i].index - elements[i-1].index) > abs(i1-i2)){
i1 = elements[i].index;
i2 = elements[i-1].index;
}
}
}
diff = 0;
int s,e;
if(i1 >= i2){
e = i1, s = i2+1;
}khác{
s = i1+1, e = i2;
}
for(int i = s;i <= e ; i++){
diff = diff+array[i];
}
cout << diff << endl;
cout << s+1 << " " << e+1;
trả về 0;
}
编辑:我在我的代码中发现了一个错误并对其进行了编辑,它现在为 80% 的输入给出了正确答案
câu trả lời hay nhất
In other words, if the profits/losses at the stations are p1, p2, ..., pN the minister would like to handover a sequence i, i+1, ..., j such that the absolute value of pi + pi+1 + ... + pj is minimized. If there is more than one sequence with this minimum absolute value then he would like to hand over the longest one
我认为你的算法有点错误:
temp[i] = sum of all elements up to input[i] Sort the array temp
序列的开始位置很可能不是数组的开始位置 - 它可以是一个子序列,不要忘记。
-1 | -2 | 10 | 5 |-10 | 6
| | | x | x | x
edit2:删除了错误的解释,现在我看了代码 -
if(temp < diff){
diff = temp;
i1 = elements[i].index;
i2 = elements[i-1].index;
}
您确实偏爱 -ve 个数字(损失)。可能是因为它略小于 -ve 或 +ve(但 abs 值较小),所以未命中数更接近零?
关于c++ - 我需要一个测试用例来证明我的算法/代码是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13229699/
(这不是关于定理证明,而是关于实践中的测试,例如 quickCheck) 让f一些通用函数 f :: RESTRICTIONS => GENERICS 具有一些“理想的”属性(即不是 hack,是不可
给定数组 arr 和索引数组 ind,以下算法就地重新排列 arr 以满足给定的索引: function swap(arr, i, k) { var temp = arr[i]; arr[i]
我有兴趣创建一个具有运行时间和空间限制的简单数组问题。看来我找到了解决问题的方法。请阅读以下java代码中问题的初始描述注释: /* * Problem: Given two integer ar
我是 isabelle 的新手,并试图证明以下简单的不等式: lemma ineq: "(a::real) > 0 ⟹ a 0 ⟹ b 0" proof have "1/a + 1/b >
是否有任何理论说缓存应该比文件系统更快? 我认为,由于文件系统也使用缓存,因此没有科学证据表明当文件系统的概念有些松散时,我们应该将内容从文件系统移动到诸如 memcache 之类的缓存中——比如下载
我正在做一个证明,我的一个子目标看起来有点像这样: Goal forall (a b : bool) (p: Prop) (H1: p -> a = b) (H2: p), neg
我有定义的归纳类型: Inductive InL (A:Type) (y:A) : list A -> Prop := | InHead : forall xs:list A, InL y (co
我知道 CRC 是一个线性函数,这意味着 CRC(x xor y) = CRC(x) xor CRC(y),但我不知道如何证明 CRC 的这个属性。 有谁有想法吗? 非常感谢! 最佳答案 这通常不是真
我是 Coq 的初学者。 虽然计算机为我验证了证明令人满意,但众所周知,满足 Coq 的证明对人类来说难以阅读。这是一个简单的例子,假设您没有看到任何评论: Theorem add_comm : fo
我试图了解是什么决定了类型参数是否必须是标称的。 虽然 GADT 和类型家族在某种意义上看起来不同,但它们不是“简单容器”,因为它们的实例定义可以“查看”它们的参数,但简单类型是否可以明显需要名义参数
我想使用 function 关键字定义来证明函数定义的正确性。以下是自然数的通常归纳定义上的加法函数的定义: theory FunctionDefinition imports Main begin
我定义了一个 Sygma-Type,如下所示: { R : nat -> nat -> bool | Reflexive R } 我有两个元素 r1 r2 : { R : nat -> nat ->
我有以下数据: new_pairs x y Freq start.latittude start.longitude start.station end.la
出于教育目的,我一直试图通过使用各种语言扩展和单例类型,在 Haskell 中重建《Type-Driven Development with Idris》(即 RemoveElem.idr )一书中的
我定义了一个 Sygma-Type,如下所示: { R : nat -> nat -> bool | Reflexive R } 我有两个元素 r1 r2 : { R : nat -> nat ->
我正在使用Ax DevTools,并且试图弄清楚如何使用相同的构建信息标记多个扫描。现在,我的测试运行如下: class MyTestCase : XCTestCase { func myTest
我正在尝试证明一个函数的正确性,该函数检查数组是否按递增/递减顺序排序或未排序。行为是返回 -1,如果按降序排序,1,如果按升序排序,大小为 1,或包含相同的值,0,如果没有已排序或为空。运行:Fra
我试图证明 Z3(Microsoft 的 SMT 求解器)中的一个归纳事实。我知道 Z3 通常不提供此功能,如 Z3 guide 中所述。 (第 8 节:数据类型),但是当我们限制要证明事实的域时,这
问题已编辑: 如代码中所述,HashSet 和 HashMap 是快速失败的(但这不是保证): void goHashSet() { Set set = new HashSet();
我试图使导航栏中的链接延伸到导航栏的全长。我环顾四周,发现了一些有用的信息,但无法使其正常工作 HTML: To
Tôi là một lập trình viên xuất sắc, rất giỏi!