STEP: 40 インクリメント
問題
解答
step40.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int answer = addUp(n, 1);
System.out.println(answer);
sc.close();
}
/**
* n+mの足し算関数
* 流用性を持たせる為にmを引数としました
*
* @param n 標準入力の足し算したい数字
* @param m nに足し算したい数字
*
* @return 解答
* */
public static int addUp(int n, int m){
return n + m;
}
}
結果
STEP: 41 重複の判定
問題
解答
step.java
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("HND", "NRT", "KIX", "NGO", "NGO");
boolean result = checkForDuplicates(list);
System.out.println(result);
//test();
}
/**
* 重複確認をする関数
*
* @param list 重複確認対
*
* @return 重複結果
*/
public static boolean checkForDuplicates(List<String> list){
for(int i = 1; i <= list.size(); i++){
// i以降から最後までのサブリストを作成する
List<String> subList = list.subList(i, list.size());
// サブリストから要素を探す、存在した場合は、-1以外が返却される
if(subList.indexOf(list.get(i-1)) != -1){
return true;
}
}
return false;
}
private static List<String> testList = Arrays.asList("HND", "NRT", "KIX", "NGO", "NGA");
private static void test(){
if(checkForDuplicates(testList)){
System.out.println("NG");
} else {
System.out.println("OK");
}
}
}
例外処理つけるのわすれてた...
結果
STEP: 42 配列(リスト)の重複カウント
問題
解答
step42.java
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("HND", "NRT", "KIX", "NGO", "NGO", "NGO", "NGO", "NGO");
int result = checkForDuplicates(list);
System.out.println(result);
}
/**
* 重複数を返却する関数
*
* @param 対象のリスト
*
* @return 重複数
* */
public static int checkForDuplicates(List<String> list){
int count = 0;
for(int i = 1; i <= list.size(); i++){
List<String> subList = list.subList(i, list.size());
if(subList.indexOf(list.get(i - 1)) != -1){
count++;
}
// 検索対象だった要素の件数を足す
if(i == list.size()){
count++;
}
}
return count;
}
}
結果
STEP: 43 配列のソート
問題
解答
step43.java
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] array = {1, 3, 5, 6, 3, 2, 5, 23, 2};
// 昇順にソート
Arrays.sort(array);
for(int elemen: array){
System.out.println(elemen);
}
}
}
結果
STEP: 44 数字のみの出力
問題
解答
step44.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// 改行
sc.nextLine();
displayNumbers(n, sc);
}
/**
* 数字を表示する関数
* @param n 行数
* @param sc 標準入力 文字と整数の組み合わせ
* */
public static void displayNumbers(int n, Scanner sc){
for(int i = 0; i < n; i++){
sc.next();
System.out.println(sc.nextInt());
}
}
}
結果
STEP: 45 昇順ソート出力
問題
解答
step45.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// 改行
sc.nextLine();
displaySortedNumbers(n, sc);
sc.close();
}
/**
* ソートした数字を表示をする関数
*
* @param n 数字の数
* @param sc ソートしたい数字の標準入力
* */
public static void displaySortedNumbers(int n, Scanner sc){
int[] array = new int[n];
for(int i = 0; i < n; i++ ){
array[i] = sc.nextInt();
}
// 昇順でソートする
Arrays.sort(array);
for(int i = 0; i < n; i++){
System.out.println(array[i]);
}
}
}
結果
FINAL問題 文字と整数の組のソート2
問題
解答
final.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// 改行
sc.nextLine();
createMap(n, sc);
sc.close();
}
/**
* 標準入力のMapを作成する関数
*
* @param n 行数
* @param sc 標準入力 文字と数字の組み合わせ
*/
public static void createMap(int n, Scanner sc){
Map<String, Integer> map = new HashMap();
for(int i = 0; i < n; i++ ){
String key = sc.next();
// 既にKeyが存在する場合、上書きする
if(map.containsKey(key)){
int add = map.get(key) + sc.nextInt();
map.replace(key, add);
}else{
map.put(key, sc.nextInt());
}
}
// 降順でmapの内容を表示する
showSortResults(n, map);
}
/**
* ソートしたMap情報を表示する関数
* 降順で表示する
*
* @param n 行数
* @param map 文字と数字の組み合わせ
*/
public static void showSortResults(int n, Map<String, Integer> map){
int[] array = new int[n];
// ソート用にMapを作成
int i = 0;
for(Map.Entry<String, Integer> entry : map.entrySet()){
array[i] = entry.getValue();
i++;
}
// 昇順でソート
Arrays.sort(array);
// 配列を逆から参照する事で降順で出力する
for(int j = n - 1; j >= 0; j--){
for(Map.Entry<String, Integer> entry : map.entrySet()){
if(array[j] == entry.getValue()){
System.out.print(entry.getKey() + " ");
System.out.println(entry.getValue());
}
}
}
}
}