Skip to content

Instantly share code, notes, and snippets.

@hcn1519
Last active November 24, 2016 07:07
Show Gist options
  • Save hcn1519/78c9268d6478c3b16e4b253a2e8bc8ca to your computer and use it in GitHub Desktop.
Save hcn1519/78c9268d6478c3b16e4b253a2e8bc8ca to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
class FileReading {
File file;
Scanner scin;
FileReading(){}
FileReading(String path){
this.file = new File(path);
}
public Condition[] makeCondition(){
Condition[] conditions = null;
int count=0;
if(file.exists()){
try{
scin = new Scanner(file);
String a = scin.nextLine();
conditions = new Condition[Integer.parseInt(a)];
for(int i=0; i< conditions.length; i++)
conditions[i] = new Condition();
int dataSet[]=new int[2];
while(scin.hasNext()){
String temp = scin.nextLine();
String temp2[] = temp.split("\\b");
int n=0;
for(int i=0; i< temp2.length; i++){
if(temp2[i].matches(".*\\w.*")){
dataSet[n] = Integer.parseInt(temp2[i]);
n++;
}
}
conditions[count].startNum = dataSet[0];
conditions[count].power = dataSet[1];
count++;
}
} catch(IOException e){}
} else{
System.out.println("input.txt not exist!!");
}
return conditions;
}
}
class Condition{
int startNum;
int power;
public void print(){
System.out.print(startNum + ", ");
System.out.print(power);
System.out.println("");
}
}
class Node {
int data;
Node link;
Node(){}
Node(int data){
this.data = data;
}
}
class DataLink {
Node head = null;
public void insertNode(int data){
Node newNode = new Node(data);
if(head == null)
this.head = newNode;
else{
Node temp = head;
while(temp.link !=null) temp = temp.link;
temp.link = newNode;
}
}
public int searchNode(int data){
Node temp = this.head;
int index = 0;
while(temp.link != null){
index++;
if(data == temp.data){
return index;
} else temp = temp.link;
}
return -1;
}
public void printNode(){
Node temp = this.head;
System.out.printf("L = (");
while(temp != null){
System.out.print(temp.data);
temp = temp.link;
if(temp != null){
System.out.printf("--> ");
}
}
System.out.println(")");
}
}
public class P8_20111108_2 {
public static int[] numDivision(int num){
String temp = Integer.toString(num);
int result[] = new int[temp.length()];
for (int i = 0; i < temp.length(); i++)
result[i] = temp.charAt(i) - '0';
return result;
}
public static int nextNum(int num[], int p){
int total=0;
int temp=0;
for(int i=0; i< num.length; i++){
temp = num[i];
for(int k=0; k < p-1; k++)
temp = temp * num[i];
total += temp;
}
return total;
}
public static void main(String[] args){
// 파일 읽기
FileReading myfile = new FileReading("input.txt");
Condition[] conditions = myfile.makeCondition();
int startNum;
int power;
int temp[] = null;
int startResult;
int result;
// 연결 리스트를 통해 구현
// 주어진 갯수만큼의 연결 리스트 만들기
DataLink[] myLists = null;
myLists = new DataLink[conditions.length];
for(int i=0; i< myLists.length; i++)
myLists[i] = new DataLink();
// 각각의 주어진 연결리스트에서 순환구조 파악하기
for(int i=0; i< conditions.length; i++){
// conditions[i].print();
// 시작 숫자 설정
startNum = conditions[i].startNum;
power = conditions[i].power;
myLists[i].insertNode(startNum);
// 두 번째 숫자 설정
temp = numDivision(startNum);
startResult = nextNum(temp, power);
myLists[i].insertNode(startResult);
// 지속적으로 숫자를 연결 리스트에 넣고, 반복해서 나오는 숫자 찾기
result = nextNum(temp, power);
int finalResult;
while(true){
if(myLists[i].searchNode(result) != -1){
finalResult = myLists[i].searchNode(result) -1;
break;
}
temp = numDivision(result);
result = nextNum(temp, power);
myLists[i].insertNode(result);
}
System.out.println(finalResult);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment