Java構文とアルゴリズム(Array)


質問です。



ホットスポット。

  • 入力値がnextInt()->nextLine()の場合、問題
  • nextInt()は改行文字(enter)を含み、
  • をスキップする.
  • nextLine()前のenterをチェックしますが、値は入力されません.
    ->解決策:nextInt()->nextLie()で穴ベイカー->nextLine()を削除して入力値を取得します.
    Scanner scan=new Scanner(System.in);
    int num=scan.nextInt();
    scan.nextLine();//上書き文字の削除
    String str=scan.nextLine();
  • ホットスポット。


    splitを使用して入力した値をnextLine()として保存します.
  • で入力された文字列を空白に分割します.
    String []test=str.split("");
    System.out.println(Arrays.toString(test));
  • コード#コード#

    package inflearn.section2_array;
    import java.util.*;
    
    public class Main1 {
    
        public int[] solution(int num,String[] str) {
    
    
            // Array의 첫번째 인자, string -> int
            int tmp=Integer.parseInt(str[0]);
    
            // 저장되는것은 동적이기 때문에 arrayList사용
            ArrayList<Integer>arrayList=new ArrayList<>();
            arrayList.add(tmp);
    
            // for문 돌면서 다음꺼랑 비교
            for (int i=1;i<num;i++) {
                // 앞에꺼랑 비교 [1] [0]
                int next=Integer.parseInt(str[i]);
    
                if (next>tmp) {
                    arrayList.add(next);
                }
                tmp=next;
            }
    
            // arrayList -> array로 변경
            int []answer=new int[arrayList.size()];
            for (int i=0;i<arrayList.size();i++) {
                answer[i]=arrayList.get(i);
            }
    
    
            return answer;
    
        }
    
    
        public static void main(String[] args) {
            Main1 main=new Main1();
            Scanner scan=new Scanner(System.in);
            int num=scan.nextInt();
            scan.nextLine(); // 개행 문자 제거
            String str=scan.nextLine();
    
            // 입력받은 문자열을 공백 기준으로 나눈다.
            String []test=str.split(" ");
            System.out.println(Arrays.toString(test));
    
            main.solution(num,test);
            // 전달받은 인자가 array이기 때문에 for문으로 받는다.
            for (int i:main.solution(num,test)) {
                System.out.print(i+" ");
            }
        }
    }
    

    質問です。



    話題


    メスステロイド
    小数を検索する方法.この方法はふるいのように、「エラトスのふるい」という数を濾過します.

    に道を教える

  • の数でアレイを作成
    ex)20の場合、21個のアレイが作成されます.
    int []=new int [21];
  • forドアを回して
    2-1. If ch[i]が0の場合、++(これは小数)と答える.
    ドアi回りの排水はすべて1番検査
  • なので

    コード#コード#

    package inflearn.section2_array;
    import java.util.*;
    
    public class Main5 {
    
        public int solution(int num) {
            int answer=0;
    
            int []ch=new int[num+1];
    
    
            for (int i=2;i<=num;i++) {
                if (ch[i]==0) {
                    answer++; // 소수
    
                    // 소수확인
    //                System.out.print(i+" ");
    
                    // 나머지는 1로 변경하기
                    for (int j=i;j<=num;j=j+i) {
                        ch[j]=1;
                    }
                }
            }
            return answer;
        }
    
    
        public static void main(String[] args) {
            Main5 main=new Main5();
            Scanner scan=new Scanner(System.in);
            System.out.println(main.solution(scan.nextInt()));
        }
    }
    

    質問です。



    ホットスポット。


    whileとfor文の使用を試みます
    cntが
  • 1の場合のみ、cntは増加する->ただし0の場合は
  • を続行できません.
    したがって、
  • は、0時にltのインデックスを1格子
  • だけ移動する.
  • は解決したものの、n^2時間複雑度
  • 講師の解決方法

  • cntは0
  • に初期化する.
  • str配列の値が1の場合にのみcntが増加し、答えに
  • が加算されます.
  • 0が0の場合、cntは0に初期化されます.
  • これにより、時間複雑度をn
  • として計算することができる.
    明確
  • コード
  • コード#コード#

    package inflearn.section2_array;
    import java.util.*;
    public class Main7 {
    
        public int solution(int num, String []str) {
            int answer=0;
    
    //        System.out.println("string"+Arrays.toString(str));
    
            int lt=0;
            int rt=num-1;
    
            while (lt<=rt) {
                // 초기값은 0
                int tmp=0;
                for (int i=lt;i<=rt;i++) {
                    if (str[i].equals("1")) {
                        tmp++;
                    }
                    else {
                        break;
                    }
                }
                // tmp가 0이면 한칸 증가 해야됨 (핵심)
                if (tmp==0) {
                    lt+=1;
                }
                else {
                    lt+=tmp;
                }
    
    //            System.out.println("tmp:"+tmp);
                int sum=0;
                for (int i=1;i<=tmp;i++) {
                    sum+=i;
                }
                answer+=sum;
    
            }
            return answer;
    
        }
        // 강사님 코드
        // cnt를 0으로 초기화
        // str배열의 값이 1일때만 cnt 증가하고 answer에 더해줌
        // 만약 0이면 cnt를 0으로 초기화
        public int solution2(int num, String []str) {
            int answer=0;
            int cnt=0;
    
            for (int i=0;i<num;i++) {
                if (str.equals("1")) {
                    cnt++;
                    answer+=cnt;
                }
                // 여기가 핵심, str이 0이면 0으로 초기화
                else {
                    cnt=0;
                }
            }
    
            return answer;
        }
    
        public static void main(String[] args) {
            Main7 main=new Main7();
            Scanner scan=new Scanner(System.in);
            int num=scan.nextInt();
            scan.nextLine();
            String []str=scan.nextLine().split(" ");
    
    
            System.out.println(main.solution(num,str));
            System.out.println(main.solution2(num,str));
        }
    }
    

    REF


  • https://www.inflearn.com/course/%EC%9E%90%EB%B0%94-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4-%EC%BD%94%ED%85%8C%EB%8C%80%EB%B9%84/dashboard

  • https://cote.inflearn.com/contest/10/problems