알고리즘 정리/[swea]

[swea] 공부기록 day03

로미로미로 2024. 5. 6. 12:57

 

2001. 파리퇴치

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for(int i=0; i<n; i++)
        {
        	int a = scan.nextInt();
        	int b = scan.nextInt();
        	int x =0;
        	
        	int [][] arr = new int[a][a];
        	for(int j=0; j<a; j++)
        	{
        		for(int k=0; k<a; k++)
        			arr[j][k] = scan.nextInt();
        	}
        	int max =0;
        
        	
        	for(int j=0; j<a-b+1; j++)
        	{
        		for(int k=0; k<a-b+1; k++)
        		{
        			x = plus(j,k,b,arr);
        			if(max < x)
        				max = x;
        		}
        	}
        
        	
        	System.out.printf("#%d %d\n", i+1, max);
        }
    }
    
    private static int plus(int a, int b, int c,int arr[][])
    {
    	//좌표값 (a,b)를 받고, 구할 정사각형 한 변의 길이 c를 받음
    	int total=0;
    	for(int i=a; i<a+c; i++)
    	{
    		for(int j=b; j<b+c; j++)
    		{
    			total += arr[i][j];
    		}
    	}
    	
    	return total;
    }
}

 

 

1974. 스도쿠 검증

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int [][] arr = new int[9][9];
        for(int i=0; i<n; i++)
        {
        	for(int j=0; j<9; j++)
        	{
        		for(int k=0; k<9; k++)
        			arr[j][k] = scan.nextInt();
        	}
        	int plus = 0;
        	int isTrue = 1;
        	for(int j=0; j<9; j++)
        	{
        		plus = 0;
        		for(int k=0; k<9; k++)
        		{
        			plus += arr[j][k];
        		}
        		if(plus != 45)
        			isTrue = 0;
        		
        	}
        	
        	if(isTrue == 1)
        	{
        		for(int j=0; j<9; j++)
            	{
            		plus = 0;
            		for(int k=0; k<9; k++)
            		{
            			plus += arr[k][j];
            		}
            		if(plus != 45)
            			isTrue = 0;
            	}
        		
        		if(isTrue == 1)
        		{
        			for(int j=0; j<7; j+=3)
        				for(int k=0; k<7; k+=3)
        				{
        					if(plus(j,k,arr) != 45)
        						isTrue = 0;
        				}
        		}
        	}
        		
        	System.out.printf("#%d %d\n", i+1, isTrue);
        }
    }
    
    private static int plus(int a, int b, int arr[][])
    {
    	int total =0;
    	//좌표(a,b)를 받고 계산 
    	for(int i=a; i<a+3; i++)
    	{
    		for(int j=b; j<b+3; j++)
    		{
    			total += arr[i][j];
    		}
    	}
    	return total;
    }
    
    
}

 

 

2005. 파스칼의 삼각형

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for(int i=0; i<n; i++)
        {
        	int size = scan.nextInt();

        	System.out.printf("#%d ", i+1);
        	for(int j=0; j<=size; j++)
        	{
        		recomb(j);
        	}
        }
        
        
    }
    
    private static void recomb(int s)
    {
		for(int i=0; i<s; i++)
    	{
    		System.out.printf("%d ", comb(s-1, i));
    	}
		System.out.println();
    }
    
    private static int comb(int n, int r)
    {
    	if(n == r || r == 0)
    		return 1;
    	else
    		return comb(n-1, r-1) + comb(n-1,r);
    }
}

 

1976. 시각 덧셈

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       int n = scan.nextInt();
       for(int i=0; i<n; i++)
       {
    	   int h1 = scan.nextInt();
    	   int m1 = scan.nextInt();
    	   
    	   int h2 = scan.nextInt(); 
    	   int m2 = scan.nextInt();
    	   
    	   int h =0;
    	   int m = 0;
    	   if(m1+ m2 >= 60)
    	   {
    		   m = m1+m2 - 60;
    		   h += 1;
    	   }
    	   else
    		   m = m1+m2;
    	   	h = h + h1 +h2;
    	   if(h1 + h2 >12)
    	   {
    		   h = h-12;
    	   }
    	   System.out.printf("#%d %d %d\n",i+1, h, m);
    	   
       }
    }
}

 

1926. 간단한 369게임

노가다로 짜서 너무 복잡해진 답.. 모범 답안 보고 배우겠습니다.

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       int n = scan.nextInt();
       for(int i=1; i<n+1; i++)
       {
          if(0< i && i<=10)
          {
             if(i==3||i==6||i==9)
             {
                System.out.printf("- ");
             }
             else
                System.out.printf("%d ",i);
          }
          if(10< i && i <= 100) //2자리까지 체크
          {
             //23 일때, 30일떄, 96일때..
             if(i/10 == 3 || i/10 == 6 || i/10 == 9)
             {
                System.out.printf("-");
             
             if(i%10 == 3 || i%10 == 6 || i%10 == 9)
                System.out.printf("- ");
             else
                System.out.printf(" ");
             }
             else
            	 if(i%10 == 3 || i% 10 == 6 || i%10 == 9)
            		 System.out.printf("- ");
            	 else
            		 System.out.printf("%d ", i);
          }
          
          if(100 < i && i<=1000)
          {
             if(i/100 == 3 || i/100 == 6 || i/100 == 9)
             {
                System.out.printf("-");
                i = i-i/100;
                if(i/10 == 3 || i/10 == 6 || i/10 == 9)
                {
                	System.out.printf("-");
                	if(i%10 == 3 || i%10 == 6 || i%10 == 9)
                	{
                		System.out.println("-");
                	}
                	else
                		System.out.print(" ");
                }
               	if(i%10 == 3 || i%10 == 6 || i%10 == 9)
                	{
                		System.out.println("-");
                	}
                	else
                		System.out.print(" ");
             }
             else
             {
            	 i = i - i/100;
            	 if (i/10 == 3 || i/10 == 6 || i/10 == 9)
            	 {
            		 System.out.printf("-");
            		 if(i%10 == 3 || i%10 == 6 || i%10 == 9)
            		 {
            			 System.out.printf("- ");
            		 }
            		 else
            			 System.out.print(" ");
            	 }
            	 else {
            		 if(i%10 == 3 || i%10 == 6 || i%10 == 9)
                 	{
                 		System.out.println("-");
                 	}
                 	else
                 		System.out.print(" ");
            	 }
             }
            	 
            	 
          }

       }
    }
}