Guest User

Untitled

a guest
Oct 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. /*
  2. 자연수 N P를 주어준다.
  3. 처음 출력은 N, 두번째는 N*N%P 세번째는 두번째값*N%P 이런식으로 출력이 된다.
  4. 예를 들은 N=67 P=31의 경우
  5. 67 25 1 5 25 1 5 이런식으로 출력된다.
  6. 이중 25 1 5 는 계속 반복 된다.
  7. 이때 반복되는 숫자의 갯수는?
  8.  
  9. 67 31 출력 : 3
  10. 9 3 출력 : 1
  11. */
  12.  
  13. package ad;
  14.  
  15. import java.util.ArrayList;
  16. import java.util.Scanner;
  17.  
  18. public class ad순환순열 {
  19. static int N,P;
  20. static ArrayList<Integer> list=new ArrayList<Integer>();
  21.  
  22. public static void main(String[] args) {
  23. Scanner sc=new Scanner(System.in);
  24.  
  25. N=sc.nextInt();
  26. P=sc.nextInt();
  27.  
  28. list.add(N);
  29.  
  30. boolean check=true;
  31. int cnt=0;
  32. int num=N;
  33.  
  34. while(check==true){
  35. int tmp=(num*N)%P;
  36. num=tmp;
  37.  
  38. if(list.size()>=1){
  39. for(int i=0; i<list.size(); i++){
  40. if(tmp==list.get(i)){
  41. check=false;
  42. cnt=i;
  43. System.out.println(list.size()-cnt);
  44. }
  45. }
  46. if(check==true){
  47. list.add(tmp);
  48. }
  49. }
  50. }
  51. }
  52. }
Add Comment
Please, Sign In to add comment