Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Fall 2014 -- CSC 2700 Section 01

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

week04/m/Main.java

public class Main
{
	public static int[] answer = new int[1000000];
	public static void main(String args[])
	{
		// The solution code:
		for(int i=1; i<=1000000; i++)
			answer[i-1] = i;
		for(int i=1,d=digitize(i); i<answer.length && d<answer.length; i++,
		d=digitize(i))
		{
			answer[d-1] = 0;
		}
		printAnswer();
	}

	public static int digitize(int n)
	{
		int danswer = n;
		String nstring = Integer.toString(n);
		int[] digits = new int[nstring.length()];
		for(int i =0; i<digits.length; i++)
		{
			digits[i] = Integer.parseInt(Character.toString(nstring.charAt(i)));
			danswer+=digits[i];
		}
		return danswer;
	}

	public static void printAnswer()
	{
		for(int i=0; i<answer.length; i++)
		{
			if(answer[i] !=0)
			System.out.println(answer[i]);
		}
	}
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014