STEP: 8 数値の出現率
問題
解答
step8.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] occurrences = new int[10];
for(int i = 0; i < n; i++){
int number = sc.nextInt();
occurrences[number]++;
}
for(int i = 0; i < occurrences.length; i++){
if(i == occurrences.length - 1 ){
System.out.print(occurrences[i]);
} else {
System.out.print(occurrences[i] + " ");
}
}
}
}
結果
STEP: 9 英小文字の出現率
問題
解答
step9.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();
String line = sc.nextLine();
char[] charArrasys = line.toCharArray();
int[] occurrences = new int[26];
for(int i = 0; i < n; i++){
int unicode = Character.getNumericValue(charArrasys[i]);
occurrences[unicode-10]++;
}
for(int i = 0; i < occurrences.length; i++){
if(i < occurrences.length-1 ){
System.out.print(occurrences[i] + " ");
} else {
System.out.println(occurrences[i]);
}
}
}
}
結果
STEP: 10 文字列の出現率
問題
解答
step10.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, Integer> occurrences = new HashMap <String, Integer>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<String> list = new ArrayList<String>();
for(int i = 0; i < n; i++){
String letter = sc.next();
if(occurrences.containsKey(letter)){
int number = occurrences.get(letter);
number++;
occurrences.replace(letter, number);
} else {
occurrences.put(letter, 1);
list.add(letter);
}
}
Collections.sort(list);
for (int i = 0; i < list.size(); i++){
System.out.println(list.get(i) + " " + occurrences.get(list.get(i)));
}
}
}
結果
STEP: 11 価格の算出
問題
解答
step11.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
// 価格表の作成
Map<String, Integer> priceList = new HashMap<String, Integer>();
for(int i = 0; i < n; i ++){
String productName = sc.next();
int price = sc.nextInt();
priceList.put(productName, price);
}
// 価格の表示
for(int i = 0; i < m; i++){
String productName = sc.next();
if(priceList.containsKey(productName)){
System.out.println(priceList.get(productName));
}else{
System.out.println("-1");
}
}
}
}
結果
FINAL問題 商品の検索
問題
解答
final.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int q = sc.nextInt();
Map<String, Integer> productList = new HashMap<String, Integer>();
// 商品表作成
for(int i = 0; i < n; i++){
String productName = sc.next();
if(productList.get(productName) == null){
productList.put(productName, i+1);
}
}
// 商品検索
for(int i = 0; i < q; i++){
String searchProduct = sc.next();
if(productList.containsKey(searchProduct)){
System.out.println(productList.get(searchProduct));
}else{
System.out.println("-1");
}
}
}
}