【发布时间】:2017-03-22 09:26:21
【问题描述】:
嘿,我只是想解决关于hackerrank 的挑战,但在某些测试用例中,代码会超时,我不知道为什么。 This is the challenge.
这是我的代码:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n, k, q;
scanf("%d %d %d",&n,&k,&q);
int qs[q];
int a[n];
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
for(int i = 0; i < q; i++){
scanf("%d",&qs[i]);
}
int lastNbr = a[n-1];
for(int i = 0; i < k; i++){
lastNbr = a[n-1];
for(int j = n - 1; j > -1; j--){
a[j] = (j-1 >= 0) ? a[j-1] : lastNbr;
}
}
for(int i = 0; i < q; i++){
printf("%d\n", a[qs[i]]);
}
return 0;
}
【问题讨论】:
-
好吧,正如你所写:你正在努力解决它。如果我们这样做,那将是不公平的...... - 您有具体的问题吗?
-
秘密是你不需要旋转数组;
%运算符是你的朋友。 -
@Olaf 我的意思是当我提交代码时,大多数测试用例都在工作,但有些超时。如果您能给我提示超时发生在我的代码的哪一部分,那就太好了。 :)
-
代码执行没有在规定的时间内完成,这是挑战的一部分。如果即使在测试用例上也超时,您需要认真重新考虑算法。
-
如果你不能解决,那就是社论的原因。