Turn on thread page Beta

"Coding challenge" problem... watch

Announcements
    • Thread Starter
    Offline

    21
    ReputationRep:
    The task is to write a program such that shifts a sequence of numbers until the maximum possible number of numbers whose positions match their values is obtained. The input is structured such that the first line contains the number of iterations, each of which contains two lines; first line contains the length of the sequence, second line contains the sequence itself. As I'm coding in Java, it also tells me not to use API code except java.util.Scanner.
    Code:
    public class Main
    {
    	public static void main(String[] args)
    	{
    		java.util.Scanner scanner = new java.util.Scanner(System.in);
    		int t = scanner.nextInt();
    		int test = (t <= 1000) ? t : 0;
    		String output = "";
    		for (int i = 0; i < test; i++)
    		{
    			int n = scanner.nextInt();
    			int[] firstLine = (n <= 100) ? new int[n] : new int[0];
    			for (int j = 0; j < firstLine.length; j++)
    				firstLine[j] = scanner.nextInt();
    			for (int j = 0; j < firstLine.length; j++)
    			{
    				int posCorrect = posTest(firstLine);
    				int[] newFirstLine = advance(firstLine);
    				if (posCorrect <= posTest(newFirstLine))
    					firstLine = newFirstLine;
    			}
    			for (int j = 0; j < firstLine.length; j++)
    				output += firstLine[j] + " ";
    			output += "\n";
    		}
    		System.out.println(output);
    	}
    	public static int posTest(int[] firstLine)
    	{
    		int count = 0;
    		for (int i = 0; i < firstLine.length; i++)
    		{
    			if (firstLine[i] == i+1)
    				count++;
    		}
    		return count;
    	}
    	public static int[] advance(int[] firstLine)
    	{
    		int[] newFirstLine = new int[firstLine.length];
    		int first = firstLine[0];
    		for (int i = 1; i < firstLine.length; i++)
    			newFirstLine[i-1] = firstLine[i];			
    		newFirstLine[firstLine.length - 1] = first;
    		return newFirstLine;
    	}
    }
    And it scores only 2 out of 10?! :lolwut:
    Help please... (maybe the problem is something to do with System.out and System.in)
    Offline

    19
    ReputationRep:
    I am no Java expert but is this any use?

    http://www.coderanch.com/t/405551/ja...Scanner-issues

    There are tonnes of pages on all coding problems under the sun. Google the error and I am sure you will have an answer very soon.

    Good luck!
 
 
 
Reply
Submit reply
Turn on thread page Beta
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: October 27, 2015
Poll
Do you think parents should charge rent?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.