英文:
How to reduce compilation time and which cases will this code fail
问题
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int k = sc.nextInt();
int v[] = new int[n];
for (int j = 0; j < n; j++) {
v[j] = sc.nextInt();
}
for (int m = 1; m <= k; m++) {
int temp = v[n - 1];
for (int p = 1; p < n; p++) {
v[n - p] = v[n - (p + 1)];
}
v[0] = temp;
}
for (int a : v) {
System.out.print(a + " ");
}
}
英文:
Problem-<br />
Monk loves to preform different operations on arrays, and so being the principal of Hackerearth School, he assigned a task to his new student Mishki. Mishki will be provided with an integer array A of size N and an integer K , where she needs to rotate the array in the right direction by K steps and then print the resultant array. As she is new to the school, please help her to complete the task.<br />
Input:<br />
The first line will consists of one integer T denoting the number of test cases.
For each test case:
- The first line consists of two integers N and K, N being the number of elements in the array and K denotes the number of steps of rotation.<br />
- The next line consists of N space separated integers , denoting the elements of the array A.<br />
It is failing a few test cases, I dont know which and also how to reduce time complexity.<br />
Here is my code I did all research I could do from my end. Also please give easy solutions as I am a beginner.
<br />
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int k = sc.nextInt();
int v[] = new int[n];
for (int j = 0; j < n; j++) {
v[j] = sc.nextInt();
}
for (int m = 1; m <= k; m++) {
int temp = v[n - 1];
for (int p = 1; p < n; p++) {
v[n - p] = v[n - (p + 1)];
}
v[0] = temp;
}
for (int a : v) {
System.out.print(a + " ");
}
}
答案1
得分: -1
我并不确定我理解了代码,但根据我所看到的内容,你发布的代码在我看来是有效的。
我重新进行了重构,以便更易于理解 - 更好的命名,许多部分被提取到方法中,去除了扫描器的需求,因此我可以针对一个名为“rotate”的方法运行一系列自己的测试用例,以检查输入与预期输出之间的关系。
import java.util.Arrays;
public class Application {
public void run(int testCases, int arraySize, int rotations, String[] arrayElements) {
for (int i = 0; i < testCases; i++) {
getRotatedArray(arraySize, rotations, arrayElements);
}
}
public int[] getRotatedArray(int arraySize, int rotationXTimes, String[] arrayElements) {
int[] a = new int[arraySize];
populateIntArray(arrayElements, a);
rotateArray(rotationXTimes, a);
Arrays.toString(a);
return a;
}
private void rotateArray(int rotations, int[] array) {
for (int m = 1; m <= rotations; m++) {
// 获取数组的最后一个元素...
int length = array.length;
int temp = array[length - 1];
// 将元素向右移动1个位置
for (int p = 1; p < length; p++) {
array[length - p] = array[length - (p + 1)];
}
// 第一个元素变为最后一个元素。
array[0] = temp;
}
}
private int[] populateIntArray(String[] arrayElements, int[] array) {
for (int j = 0; j < arrayElements.length; j++) {
array[j] = Integer.parseInt(arrayElements[j]);
}
return array;
}
}
一些测试示例:
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class ApplicationTest {
private Application app;
@Before
public void setUp() {
app = new Application();
}
// ... 其他测试用例被省略 ...
@Test
public void test13() {
int[] result = app.getRotatedArray(4 , 4, new String[]{"0","1","2","3"});
assertEquals(4, result.length);
assertArrayEquals(new int[]{0,1,2,3}, result);
}
}
所有测试都通过。
如果你阅读这些测试,你应该能够看到我提供了一些输入,执行特定的方法(执行一些过程),并根据我期望的结果检查输出。
也就是说,我正在测试getRotatedArray
方法是否按照期望工作。而且它确实有效。
当你说它出现错误并且你不知道出错的地方时,我建议添加一些System.out.println
语句,这样你可以验证应用程序是否按照你认为的方式运行。
英文:
I'm not certain I understood the code but what you posted works as far as I can tell.
I refactored it all to make it easier to digest - better naming, lots of parts extracted to methods, removed the need for a scanner so I could run a bunch of my own test cases against the a "rotate" method to check inputs vs expected outputs.
import java.util.Arrays;
public class Application {
public void run(int testCases, int arraySize, int rotations, String[] arrayElements) {
for (int i = 0; i < testCases; i++) {
getRotatedArray(arraySize, rotations, arrayElements);
}
}
public int[] getRotatedArray(int arraySize, int rotationXTimes, String[] arrayElements) {
int[] a = new int[arraySize];
populateIntArray(arrayElements, a);
rotateArray(rotationXTimes, a);
Arrays.toString(a);
return a;
}
private void rotateArray(int rotations, int[] array) {
for (int m = 1; m <= rotations; m++) {
// get last item of array...
int length = array.length;
int temp = array[length - 1];
// shift elements 1 position right
for (int p = 1; p < length; p++) {
array[length - p] = array[length - (p + 1)];
}
// first element becomes last.
array[0] = temp;
}
}
private int[] populateIntArray(String[] arrayElements, int[] array) {
for (int j = 0; j < arrayElements.length; j++) {
array[j] = Integer.parseInt(arrayElements[j]);
}
return array;
}
}
Some tests:
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class ApplicationTest {
private Application app;
@Before
public void setUp() {
app = new Application();
}
@Test
public void test1() {
int[] result = app.getRotatedArray(0 , 0, new String[]{});
assertEquals(0, result.length);
}
@Test
public void test2() {
int[] result = app.getRotatedArray(1 , 0, new String[]{"0"});
assertEquals(1, result.length);
assertArrayEquals(new int[]{0}, result);
}
@Test
public void test3() {
int[] result = app.getRotatedArray(1 , 1, new String[]{"0"});
assertEquals(1, result.length);
assertArrayEquals(new int[]{0}, result);
}
@Test
public void test4() {
int[] result = app.getRotatedArray(1 , 2, new String[]{"0"});
assertEquals(1, result.length);
assertArrayEquals(new int[]{0}, result);
}
@Test
public void test5() {
int[] result = app.getRotatedArray(2 , 0, new String[]{"0","1"});
assertEquals(2, result.length);
assertArrayEquals(new int[]{0,1}, result);
}
@Test
public void test6() {
int[] result = app.getRotatedArray(2 , 1, new String[]{"0","1"});
assertEquals(2, result.length);
assertArrayEquals(new int[]{1,0}, result);
}
@Test
public void test7() {
int[] result = app.getRotatedArray(2 , 2, new String[]{"0","1"});
assertEquals(2, result.length);
assertArrayEquals(new int[]{0,1}, result);
}
@Test
public void test8() {
int[] result = app.getRotatedArray(3 , 0, new String[]{"0","1","2"});
assertEquals(3, result.length);
assertArrayEquals(new int[]{0,1,2}, result);
}
@Test
public void test9() {
int[] result = app.getRotatedArray(3 , 1, new String[]{"0","1","2"});
assertEquals(3, result.length);
assertArrayEquals(new int[]{2,0,1}, result);
}
@Test
public void test10() {
int[] result = app.getRotatedArray(3 , 2, new String[]{"0","1","2"});
assertEquals(3, result.length);
assertArrayEquals(new int[]{1,2,0}, result);
}
@Test
public void test11() {
int[] result = app.getRotatedArray(3 , 3, new String[]{"0","1","2"});
assertEquals(3, result.length);
assertArrayEquals(new int[]{0,1,2}, result);
}
@Test
public void test12() {
int[] result = app.getRotatedArray(4 , 0, new String[]{"0","1","2","3"});
assertEquals(4, result.length);
assertArrayEquals(new int[]{0,1,2,3}, result);
}
@Test
public void test13() {
int[] result = app.getRotatedArray(4 , 4, new String[]{"0","1","2","3"});
assertEquals(4, result.length);
assertArrayEquals(new int[]{0,1,2,3}, result);
}
}
All tests passed.
If you read the tests you should be able to see I'm providing some input, exercising a particular method (performing some process), and checking the output against what I expect it to be.
i.e. I'm testing the getRotatedArray
works according to expectations. And it does.
When you say it breaks and you don't know where, I'd suggest adding some System.out.println
statements so you can verify the application is doing what you think it's doing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论