첫 째줄에는 총 8개의 숫자가 주어지고
1 -> 8인지 8 -> 1인지를 판단하여 ascending, descending, mixed를 판단하여 출력하면 된다.
- 첫번째 들어온 숫자를 파악한다.
- 이후 1씩 증가하여 들어오는지 파악하고 아니라면 mixed 출력
- 8부터 시작하여 1로 끝나면 descending, 반대라면 ascending을 출력하도록 한다.
#include<iostream> #include<vector> using namespace std; int main(int argc, char** argv) { // 1. int start; cin >> start; string type; int A, B; A = start; for (int i = 2; i <= 8; ++i) { cin >> B; // 2. if (abs(A-B) != 1) { type = "mixed"; break; } if (start == 1) { type = "ascending"; } if (start == 8) { type = "descending"; } A = B; } // 3. cout << type; return 0; }
다른풀이
#include<iostream> #include<string> using namespace std; int main(int argc, char** argv) { string nums; for (int i = 0; i < 8; ++i) { int a; cin >> a; nums += to_string(a); } if (nums == "12345678") { cout << "ascending"; } else if (nums == "87654321") { cout << "descending"; } else { cout << "mixed"; } return 0; }
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine(), " "); // 1. int start = Integer.parseInt(st.nextToken()); String type = ""; int A, B; A = start; for (int i = 2; i <= 8; ++i) { B = Integer.parseInt(st.nextToken()); // 2. if (Math.abs(A-B) != 1) { type = "mixed"; break; } if (start == 1) { type = "ascending"; } if (start == 8) { type = "descending"; } A = B; } // 3. System.out.println(type); } }
import sys nums = sys.stdin.readline().rstrip() nums = nums.replace(" ", "") if (nums == "12345678"): print("ascending") elif (nums == "87654321"): print("descending") else: print("mixed")
JAVA
1. java에서 string 선언후에는 초기화를 해주어야 한다.