This is a challenge I have solved, but I get a score of zero when I submit the code. It seems to produce the correct output when I run it on my local machine. I have solved it with C and Java. In case of java the server is responding with a NZEC error the reason of which is also unknown.
Input: The first line contains a number T
which is the number of test cases. The next T
lines each contain 3 integers X
, Y
and N
separated by a single space.
Output: For each test case print the N
th number of the sequence.
Constraints:
1) 1 <= T <= 10^6
2) 0 < N <= 50
3) 0 < X < 50
4) 0 <= Y < 3
Sample Input- Sample Output-
1 34
3 2 7
Here's my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int array[10000], x, y, z, j;
long long int testcase;
scanf("%lld", &testcase);
for (long long int v = 0; v < testcase; v++) {
scanf("%d %d %d", &x, &y, &z);
*array = (int)(calloc ((z + 100), sizeof(int *)));
for (int i = 0; i < z + 100; i++) {
array[i] = 0;
}
for (j = 0; j < x; j++) {
array[j] = y;
}
for (int l = j; l < z; l++) {
array[l] = array[l - 1] + array[l - 2] + array[l - 3];
}
printf("%d ", array[(z - 1)]);
}
return 0;
}
Here is my code written in java--
public class TestClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long testCases = Long.parseLong(sc.nextLine());
StringBuilder sb = new StringBuilder();
for (long i = 0; i < testCases; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
int array[] = new int[z+1];
long sum = 0;
int j;
for (j = 0; j < x; j++) {
array[j] = y;
}
for (int l = j; l < z; l++) //add for loop in case of greater j values
{
array[l] = array[l - 1] + array[l - 2] + array[l - 3];
}
System.out.println (array[(z-1)] + " ");
}
}
}
Aucun commentaire:
Enregistrer un commentaire