Blue___
코딩배우는 학생🌎
Blue___
전체 방문자
오늘
어제
  • 코딩배우는 학생🧀 (242)
    • Algorithms (145)
      • BOJ[Java] (107)
      • Programmers[Java] (32)
      • Coding_Contest (3)
    • Web (22)
      • .NET Core C# (2)
      • Java (1)
      • Oracle SQL (7)
      • Web-ProJect (3)
      • Error처리 (1)
      • Web지식 (4)
      • Javascript (1)
      • Vue (3)
    • Git (4)
    • Java_beginner(Repl.it) (55)
      • Auto-Graded-Course(AP CS A) (54)
    • 프로젝트 직딩일기 (3)
    • Hanyang_Assignment (0)
    • 이모저모 (4)
      • 잡담 (1)
      • 2021 오픈소스 컨트리뷰터 아카데미 (1)
      • DDD - 6기! (1)
    • 북리뷰 (1)
      • 리팩토링 2판 (1)
      • 클린코드 (0)

블로그 메뉴

  • 🐰GITHUB
  • ☘️포트폴리오
  • 🌸MBC개발_투표 2022
  • 🍭MBC_APP

공지사항

인기 글

태그

  • Java tutorial
  • 알고리즘
  • Bakjoon
  • 자바
  • 프로그래머스
  • repl.it
  • 백준
  • java basic
  • coding
  • 레플릿
  • 코딩
  • 코딩배우는학생
  • REPL
  • 코딩배우는 학생
  • auto-graded course
  • AP CS A
  • Java
  • algorithm
  • programmers
  • 프로그래밍

최근 댓글

최근 글

티스토리

hELLO
Blue___

코딩배우는 학생🌎

[백준/7326번] Number Steps (Tehran Site 2000)[Java]
Algorithms/BOJ[Java]

[백준/7326번] Number Steps (Tehran Site 2000)[Java]

2019. 12. 17. 23:15

문제

Starting from point (0,0) on a plane, we have written all non-negative integers 0,1,2,... as shown in the figure. For example, 1, 2, and 3 has been written at points (1,1), (2,0), and (3,1) respectively and this pattern has continued.

You are to write a program that reads the coordinates of a point (x, y), and writes the number (if any) that has been written at that point. (x, y) coordinates in the input are in the range 0...5000.

입력

The first line of the input is N, the number of test cases for this problem. In each of the N following lines, there is x, and y representing the coordinates (x, y) of a point.

출력

For each point in the input, write the number written at that point or write "No Number" if there is none.

 


풀이

어떻게 풀어줄까 생각하다가, x와 y의 관계가 2가지로 정의되는 것을 발견했다. 1.x==y || 2. y=x-2 의 두가지 경우이다. 각각의 경우에 수의 증가분이 1++,3++로 증가되는 것을 알 수 있는데 이는 x의 값에 따라 번갈아 나타나기 때문에 이를 반복문을 통해 처리해 주었다. 처음 수의 경우 증가분이 없기 때문에 이같은 경우에는 조건을 하나 더 설정해 주었다. ㅇㅅㅇ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    import java.util.*;
    
    class Main {
          public static void main(String args[]) {
              Scanner sc=new Scanner(System.in);
              int n=sc.nextInt();
              int temp=0;
              int sum=0;
              out:for(int i=0;i<n;i++) {
                  int x=sc.nextInt();
                  int y=sc.nextInt();
                  if(x==y) {
                      sum=0;
                      fir:for(int j=0;j<=x;j++) {
                              if(x==0) break fir;
                              if(j%2==1) {                              
                              sum+=1;
                              }
                              else if(j%2==0 && j!=0) {
                              sum+=3;
                              }
                  }                 
                  }
                  if(y==x-2 && x>=2) {
                      sum=2;
                      sec:for(int j=2;j<=x;j++) {
                              if(x==2) break sec;
                              if(j%2==1) {                              
                              sum+=1;
                              }
                              else if(j%2==0 && j!=2){
                              sum+=3;
                              }
                              }    
                                          
                              }
                  
                  if(y!=x && y!= x-2) {
                      System.out.println("No Number");
                      continue;
                  }
                  System.out.println(sum);    
              
              }              
              sc.close();
          }
    }
 
 

https://www.acmicpc.net/problem/7326

 

7326번: Number Steps

Starting from point (0,0) on a plane, we have written all non-negative integers 0,1,2,... as shown in the figure. For example, 1, 2, and 3 has been written at points (1,1), (2,0), and (3,1) respectively and this pattern has continued. You are to write a pr

www.acmicpc.net

 

반응형
저작자표시 (새창열림)

'Algorithms > BOJ[Java]' 카테고리의 다른 글

[백준/15803번] PLAYERJINAH’S BOTTLEGROUNDS (2018 SCCC Programming Contest)[Java]  (0) 2019.12.21
[백준/2676번] 라스칼 삼각형(2011 Greater New York Programming Contest )[Java]  (0) 2019.12.20
[백준/10709번] 기상캐스터(JOI 2015)[Java]  (0) 2019.12.17
[백준/3034번] 앵그리 창영(COCI 2006/2007) [Java]  (0) 2019.12.17
[백준/2822번] 점수 계산(COCI 2011/2012) [Java]  (0) 2019.12.14
    'Algorithms/BOJ[Java]' 카테고리의 다른 글
    • [백준/15803번] PLAYERJINAH’S BOTTLEGROUNDS (2018 SCCC Programming Contest)[Java]
    • [백준/2676번] 라스칼 삼각형(2011 Greater New York Programming Contest )[Java]
    • [백준/10709번] 기상캐스터(JOI 2015)[Java]
    • [백준/3034번] 앵그리 창영(COCI 2006/2007) [Java]
    Blue___
    Blue___
    완전 연소한 불은 재를 남기지않는다 : 코딩배우는학생 🌎

    티스토리툴바