자바 문자열 곱하기 - jaba munjayeol gobhagi

n을 입력받으면 양의 곱으로 표현하기

난이도:(기초)

현우는 여동생한테 곱하기를 가르쳐 주던중 곱하기를 외우기 귀찮아하던 자신의 어릴적 모습이 생각이나

어떻게하면 극복을 할 수 있을까 고민을 하던중 한가지 꼼수를 생각했다!

그것은 n을 입력받고 두개의 양수의 곱이 n이 되는 모든 양수를 출력하는 프로그램을 만드는 것이다!

ex : 18 > (18,1)(9,2)(6,3)(3,6)(2,9)(1,18) #9,2와 2,9는다른것으로본다.

오늘도 귀찮은 현우를 위해 오늘도 힘써주자!

(난이도 up.ver: n과m을 입력받고 m개의 양수의 곱이 n이 되는 모든 양수출력!)

(이문제는 어디서 본거 같은 것을 생각해서 쓴거에요 근데 문제보다 스토리 짜기가 힘들어요..ㅠㅠ)

+1 추천받고싶드아. - leak, 2018/07/17 19:42 M D

import java.util.ArrayList;
import java.util.Scanner;
import java.util.ListIterator;

public class FindProduct {
    ArrayList<String> list = new ArrayList<>();
    ArrayList<Integer> coupleForTest = new ArrayList<>();
    static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        FindProduct findProduct = new FindProduct();
        int inputNumber;
        int size;

        // *** Get data about number and size.
        while(true) {
            System.out.print("$ Enter a number: ");
            inputNumber = scan.nextInt();
            System.out.print("$ Enter size: ");
            size = scan.nextInt();

            if(inputNumber<=0) 
                System.out.println("The number entered is less than 0.");
            else if(size<2)
                System.out.println("The size entered is less than 2.");
            else 
                break;
        }

        // *** Initialization
        findProduct.initArrayList(size);

        // *** Find couple of product & add new couple.
        findProduct.findCoupleAndAddCouple(inputNumber, size);

        // *** Print list of couples.
        findProduct.printList();
    }

    private void initArrayList(int size) {
        for(int i=0; i<size; i++) {
            coupleForTest.add(1);
        }
    }

    private void findCoupleAndAddCouple(int inputNumber, int size) {
        int count = 1;
        while(count < Math.pow(inputNumber, size)-Math.pow(inputNumber, size-1)+1) {
            // ** size-1 번째 인덱스에 더하기 1
            coupleForTest.set(size-1, coupleForTest.get(size-1)+1);

            // ** 정리
            ListIterator<Integer> iter1 = coupleForTest.listIterator(size);
            while(iter1.hasPrevious()) {
                int currentNum = iter1.previous();
                if(iter1.nextIndex()!=size-1) 
                    currentNum += 1;

                if(currentNum==inputNumber+1) {
                    iter1.set(1);
                }
                else {
                    iter1.set(currentNum);
                    break;
                }           
            }

            // ** 확인
            int result = 1;
            ListIterator<Integer> iter2 = coupleForTest.listIterator(0);
            while(iter2.hasNext()) {
                result *= iter2.next();
            }

            if(result == inputNumber) {
                Couple newCouple = new Couple(coupleForTest);
                list.add(0, newCouple.toString());
            }

            count++;
        }
    }

    private void printList() {
        System.out.print("  Result =>"  );
        ListIterator<String> iter = list.listIterator();

        while(iter.hasNext()) {
            System.out.print(iter.next());

            if(iter.nextIndex()!=list.size()) {
                System.out.print(",");
            }
        }   
    }
}

class Couple {
    private ArrayList<Integer> couple = new ArrayList<>();

    public Couple(ArrayList<Integer> newCouple) {
        couple = newCouple;
    }

    public String toString() {  
        String output = " (";
        int size = couple.size();

        for(int i=0; i<size; i++) {
            if(i!=size-1) {
                output += couple.get(i) + ", ";
            }else {
                output += couple.get(i);
            }
        }

        output += ")";

        return output;
    }
}

n과m을 입력받고 m개의 양수의 곱이 n이 되는 모든 양수출력되도록 짜보았습니다.

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num =sc.nextInt();
        for(int i=1; i<=num; i++) {
            for(int j=1; j<=num; j++) {
                if(num==i*j) {
                    System.out.print("(" + i + "," + j + ")");
                }
            }
        }
    }   
}

//n을 입력받으면 양의 곱으로 표현하기
import java.util.Scanner;

public class solve1 {

    public static void main(String[] args) {
        Scanner n=new Scanner(System.in);
        System.out.print("수 입력: ");
        int num=n.nextInt();
        System.out.println("");

        for(int i=1;i<=num;i++) {
            if(num%i==0) {
                System.out.print("("+i+","+num/i+") ");
            }
        }

    }

}

public class TEST09 {

    public static void main(String[] args) {
        String result = compute(256);
        System.out.println(result);
    }

    public static String compute(int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = n; i >= 1; i--) {
            for (int k = 1; k <= n; k++) {
                int multi = i * k;
                if (multi == n) {
                    sb.append("(").append(i).append(",").append(k).append(")");
                }
                else if (multi > n) {
                    break;
                }
            }
        }

        return sb.toString();
    }

}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void print(int n)
    {
        for(int i=1;i<=n;i++)
        {
            if(n%i==0)
            {
                System.out.printf("(%d,%d)\n",i,n/i);
            }
        }

    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input="0";
        try {
            input = br.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int n = Integer.parseInt(input);
        print(n);
    }



}

import java.util.Scanner;

public class RIGHTNOW {
    static int n;
    public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
            n = sc.nextInt();

            for (int i = 1; i <= n; i++) {
                if(n % i ==0) 
                    System.out.print("("+i + ", " + n/i+")");               
            }
    }
}

자바

import java.util.Scanner;//Scanner 함수 사용을 위한 import

public class decate {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();//숫자를 입력받음
        for(int i = 1; i <= num; i++) {//1~ num 까지 반복
            if(num % i == 0) {//i 가 num 의 약수(약수는 나누어 떨어짐) 인지 확인
                System.out.printf("{%d,%d}",i,(num/i));//i 와 num/i 를 {1,2} 같은 형식으로 출력
            }
        }
    }
}

class Main {

    static int n, a;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        a = n;
        b : for (int i=1; i<=n/2; i++) {
            for (int j=a; j>=i; j--) {
                if (i*j == n) {
                    System.out.print("("+ i + ", " + j + ")"+"("+ j + ", " + i + ")");
                    a = j;
                    break;
                }
                if (i>=a)
                    break b;
            }
        }
    }
}

*import java.util.;

class h2 { public static void main(String[] args) { int num,a,b,i,j; Scanner s = new Scanner(System.in); num=s.nextInt(); System.out.print(num+" > "); for (a=1;a<=num;a++) { for (b=1;b<=num;b++) { if(ab==num) { System.out.print("("+a+","+b+")"); } } } }* }```{.java}

```

곱셈을 가르치고 싶어하는것 같아 정말 곱셈을 만들었습니다.


import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ArrayList<String> result = new ArrayList<>();
        int input;

        try {
            System.out.println("입력해주세요  : ");
            input = scan.nextInt();

            for (int i = 1; i <= input; i++) {
                for (int j = 1; j <= input; j++) {
                    if (i * j == input) {
                        result.add(i + " * " + j);
                    }
                }
            }

            System.out.println(result);
        } catch (InputMismatchException e) {
            System.out.println("땍 똑바로 입력하지 못할까");
            return;
        }
    }
}

풀이 작성

※ 풀이작성 안내

  • 본문에 코드를 삽입할 경우 에디터 우측 상단의 "코드삽입" 버튼을 이용 해 주세요.
  • 마크다운 문법으로 본문을 작성 해 주세요.
  • 풀이를 읽는 사람들을 위하여 풀이에 대한 설명도 부탁드려요. (아이디어나 사용한 알고리즘 또는 참고한 자료등)
  • 작성한 풀이는 다른 사람(빨간띠 이상)에 의해서 내용이 개선될 수 있습니다.