뭐가 문제인지 모르겠어요....
조회수 268회
import java.util.Scanner;
class ListNode { // 노드 구성하는 클래스
int coef;
int expo;
ListNode next;
public ListNode() {
next = null;
}
public ListNode(int coef, int expo) {
this.coef = coef;
this.expo = expo;
this.next = null;
}
}
class Polynomial { // 다항식을 구성하는 클래스
ListNode head;
char name;
int numOfTerms = 0;
Scanner in = new Scanner(System.in);
public Polynomial(char name,int numOfTerm) {
head = null;
this.name = name;
this.numOfTerms = numOfTerm;
for (int i = 1 ; i<= numOfTerm; i++) {
int coef = in.nextInt();
int expo = in.nextInt();
insertNode(coef,expo);
}
}
public Polynomial() {
}
public void insertNode(int coef, int expo) { // 입력받은 정수로 항을 추가하는 메서드
ListNode node = new ListNode(coef,expo);
if(head==null)
head = node;
else {
ListNode current = head;
while(current.next!=null) {
current = current.next;
}
current.next = node;
}
}
public void print() { // 항개수와 각 항의 계수와 지수를 출력하는 메서드
System.out.println(numOfTerms);
ListNode current = head;
while (current.next!=null) {
System.out.printf("%d %d\n",current.coef,current.expo );
current = current.next;
}
System.out.printf("%d %d\n",current.coef, current.expo);
System.out.println();
}
}
public class Main { // Main 클래스
Scanner in = new Scanner(System.in);
static Polynomial [] polys = new Polynomial [101];
int n = 0;
static Polynomial c1 = new Polynomial();
public void Main() {
}
public void CreatePolynomial () {
char name = in.next().charAt(0);
int numOfTerm = in.nextInt();
Polynomial p = new Polynomial(name,numOfTerm);
polys[n++] = p;
}
public static Polynomial AddPolynomial(Polynomial A,Polynomial B) {
ListNode a = A.head;
ListNode b = B.head;
Polynomial C = new Polynomial();
while(a!=null && b!=null) {
if(a.expo<b.expo) {
C.insertNode(b.coef, b.expo);
C.numOfTerms += 1;
b = b.next;
}
else if(a.expo > b.expo) {
C.insertNode(a.coef, a.expo);
C.numOfTerms += 1;
a = a.next;
}
else {
if (a.coef+b.coef == 0) {
a = a.next;
b = b.next;
}
else {
C.insertNode(a.coef+b.coef,a.expo);
C.numOfTerms += 1;
a = a.next;
b = b.next;
}
}
}
while (a!=null) {
C.insertNode(a.coef,a.expo);
C.numOfTerms += 1;
a = a.next;
}
while (b!=null) {
C.insertNode(b.coef, b.expo);
C.numOfTerms += 1;
b = b.next;
}
return C;
}
public void processCommand() {
Scanner in = new Scanner(System.in);
int PolyNum = Integer.parseInt(in.next());
for (int i = 1; i <=PolyNum ; i++)
CreatePolynomial();
for (int i = 0; i <= PolyNum-1 ; i++) {
c1 = AddPolynomial(c1,polys[i]);
}
c1.print();
}
public static void main(String[] args) {
Main app = new Main();
app.processCommand();
}
}
다항식을 덧셈해서 결과를 출력하는 프로그램인데... 제가 eclipse에서 돌릴때는 잘 되다가 컴파일 사이트에 들어가서 컴파일하면 NoSuchElementException 이라고 자꾸 뜨네요.... ㅠㅠ 몇시간째 해결해보려했는데 도저히 안될것같아서 질문드립니다....
댓글 입력