/***************************/ /* ベクトルの大きさの計算 */ /* code by Y.Suganuma */ /***************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); double s; int i1, n; /* 初期設定 */ s = 0.; /* データの入力 */ System.out.print("ベクトルの次元は? "); n = Integer.parseInt(in.readLine()); double x[] = new double [n]; for (i1 = 0; i1 < n; i1++) { System.out.print((i1+1) + " 番目の要素は? "); x[i1] = Double.parseDouble(in.readLine()); } /* 計算 */ for (i1 = 0; i1 < n; i1 = i1+1) s += x[i1] * x[i1]; s = Math.sqrt(s); /* 出力 */ System.out.println("結果は=" + s); } }
/****************************/ /* ベクトルの和の計算 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int i1, n; /* 入力 */ System.out.print("ベクトルの次元は? "); n = Integer.parseInt(in.readLine()); double a[] = new double [n]; double b[] = new double [n]; double c[] = new double [n]; for (i1 = 0; i1 < n; i1++) { System.out.print("ベクトルaの " + (i1+1) + "番目の要素は? "); a[i1] = Double.parseDouble(in.readLine()); System.out.print(" ベクトルbの " + (i1+1) + "番目の要素は? "); b[i1] = Double.parseDouble(in.readLine()); } /* 実行 */ System.out.println("和は"); for (i1 = 0; i1 < n; i1++) { c[i1] = a[i1] + b[i1]; System.out.print(" " + c[i1]); } System.out.println(); } }
/****************************/ /* 内積の計算 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); double x; int n, i1; /* 初期設定 */ x = 0.0; /* データの入力 */ System.out.print("次元は? "); n = Integer.parseInt(in.readLine()); double a[] = new double [n]; double b[] = new double [n]; for (i1 = 0; i1 < n; i1++) { System.out.print(" ベクトルaの " + (i1+1) + " 番目の要素は? "); a[i1] = Double.parseDouble(in.readLine()); } for (i1 = 0; i1 < n; i1++) { System.out.print(" ベクトルbの " + (i1+1) + " 番目の要素は? "); b[i1] = Double.parseDouble(in.readLine()); } /* 計算 */ for (i1 = 0; i1 < n; i1++) x += a[i1] * b[i1]; /* 出力 */ System.out.println("内積=" + x); } }
/****************************/ /* データの交換 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int i1, k, n, x1; /* 初期設定 */ k = 0; /* 入力 */ System.out.print("データの数は? "); n = Integer.parseInt(in.readLine()); int a[] = new int [n]; int b[] = new int [n]; int c[] = new int [n]; int d[] = new int [n]; for (i1 = 0; i1 < n; i1++) { System.out.print("aの " + (i1+1) + " 番目のデータは? "); a[i1] = Integer.parseInt(in.readLine()); System.out.print(" bの " + (i1+1) + " 番目のデータは? "); b[i1] = Integer.parseInt(in.readLine()); } /* 実行 */ for (i1 = 0; i1 < n; i1++) { if (a[i1] > b[i1]) { c[k] = a[i1]; d[k] = b[i1]; x1 = a[i1]; a[i1] = b[i1]; b[i1] = x1; k++; } } /* 出力 */ System.out.println("操作回数=" + k); for (i1 = 0; i1 < k; i1++) System.out.println(" a,b " + c[i1] + " " + d[i1]); } }
/************************************/ /* 平均値の計算(平均点以下の出力) */ /* coded by Y.Suganuma */ /************************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); double mean1, mean2, sum1, sum2; int n, i1, kazu; /* 初期設定 */ sum1 = 0.0; sum2 = 0.0; kazu = 0; /* データの数の読み込み */ System.out.print("人数は? "); n = Integer.parseInt(in.readLine()); double x[] = new double [n]; double y[] = new double [n]; /* データの読み込み */ for (i1 = 0; i1 < n; i1++) { System.out.print((i1+1) + " 番目の人の英語の点は? "); x[i1] = Double.parseDouble(in.readLine()); System.out.print(" 英語の点は? "); y[i1] = Double.parseDouble(in.readLine()); sum1 += x[i1]; sum2 += y[i1]; } /* 結果の計算と出力 */ if (n <= 0) System.out.println("データがない!"); else { mean1 = sum1 / n; mean2 = sum2 / n; System.out.println(" 英語=" + mean1 + " 数学=" + mean2); for (i1 = 0; i1 < n; i1++) { if (x[i1] <= mean1 || y[i1] <= mean2) kazu++; } System.out.println(" 平均点以下の人数は " + kazu + " 人"); } } }
/****************************/ /* 原点までの距離の計算 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); double mean, sum, x1; int i1, k, n; /* 初期設定 */ sum = 0.0; k = 0; /* データの入力と距離の計算 */ System.out.print("点の数は? "); n = Integer.parseInt(in.readLine()); double x[] = new double [n]; double y[] = new double [n]; double r[] = new double [n]; for (i1 = 0; i1 < n; i1++) { System.out.print((i1+1) + " 番目の点のx座標は? "); x[i1] = Double.parseDouble(in.readLine()); System.out.print(" y座標は? "); y[i1] = Double.parseDouble(in.readLine()); x1 = x[i1] * x[i1] + y[i1] * y[i1]; r[i1] = Math.sqrt(x1); sum += r[i1]; } /* 計算 */ if (n <= 0) System.out.println("データがありません!"); else { mean = sum / n; System.out.println("平均距離は " + mean); System.out.println("点の座標は"); for (i1 = 0; i1 < n; i1++) { if (r[i1] >= mean) { k++; System.out.println(x[i1] + " " + y[i1]); } } System.out.println(" 点の数=" + k); } } }
/****************************/ /* 成分の%の計算 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new FileReader("input")); double data, sum; int max = 10, n, i1; double x[] = new double [max]; double y[] = new double [max]; String str; /* 初期設定 */ sum = 0.0; n = 0; /* データの読み込み */ while (null != (str = in.readLine())) { if (n >= max) { System.out.println("データが多すぎます!"); System.exit(1); } else { data = Double.parseDouble(str); x[n] = data; sum += data; n++; } } in.close(); /* %の計算 */ for (i1 = 0; i1 < n; i1++) y[i1] = x[i1] / sum * 100.0; /* 出力 */ PrintStream out = new PrintStream(new FileOutputStream("output")); for (i1 = 0; i1 < n; i1++) out.println((i1+1) + " 番目の成分 " + y[i1] + " %"); out.close(); } }
RANGE NUM 0- 10 2 ** 11- 20 5 ***** ・・・・ 91-100 3 ***
/****************************/ /* グラフの出力(1) */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int i1, i2, k, r1, r2, m, n; int num[] = new int [10]; /* 初期設定 */ r1 = 1; r2 = 10; for (i1 = 0; i1 < 10; i1=i1+1) num[i1] = 0; /* 入力 */ System.out.print("人数は? "); n = Integer.parseInt(in.readLine()); for (i1 = 0; i1 < n; i1++) { System.out.print((i1+1) + " 番目の人の点は? "); m = Integer.parseInt(in.readLine()); k = (m - 1) / 10; if (k < 0) k = 0; num[k] += 1; } /* 出力 */ System.out.println(" RANGE NUM"); for (i1 = 0; i1 < 10; i1++) { if (i1 == 0) System.out.print(" 0- 10 "); else { if (i1 == 9) System.out.print(" 91-100 "); else System.out.print(" " + r1 + "- " + r2 + " "); } if (num[i1] < 10) System.out.print(" " + num[i1] + " "); else { if (num[i1] < 100) System.out.print(" " + num[i1] + " "); else System.out.print(num[i1] + " "); } for (i2 = 0; i2 < num[i1]; i2++) System.out.print("*"); System.out.println(); r1 += 10; r2 += 10; } } }
* * * * * ・・・・・ * * * * * * * -------------- ・・・・・ --------- 1 2 3 ・・・・・ 11 12
/****************************/ /* グラフの出力(2) */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int i1, i2; int m[] = new int [12]; /* 入力 */ for (i1 = 0; i1 < 12; i1++) { System.out.print((i1+1) + " 月の売り上げは(0~9)? "); m[i1] = Integer.parseInt(in.readLine()); } /* 出力 */ System.out.println(""); for (i1 = 9; i1 > 0; i1--) { for (i2 = 0; i2 < 12; i2++) { if (m[i2] >= i1) System.out.print(" *"); else System.out.print(" "); } System.out.print("\n"); } System.out.print("------------------------------------\n"); for (i1 = 0; i1 < 12; i1++) { if (i1 > 8) System.out.print(" " + (i1+1)); else System.out.print(" " + (i1+1)); } System.out.print("\n"); } }
/****************************/ /* 売上の計算 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int i1, k1, k2, m, mean, n; int x[] = new int [30]; /* 初期設定 */ mean = 0; m = 0; /* データの入力 */ for (i1 = 0; i1 < 30; i1++) { System.out.print(" " + (i1+1) + " 日の売上は? "); x[i1] = Integer.parseInt(in.readLine()); } while (m < 1 || m > 30) { System.out.print("基準日(1から30)は? "); m = Integer.parseInt(in.readLine()); } /* 計算 */ k1 = m - 3; if (k1 < 1) k1 = 1; k2 = m + 3; if (k2 > 30) k2 = 30; n = k2 - k1 + 1; for (i1 = k1-1; i1 < k2; i1++) mean += x[i1]; mean /= n; /* 出力 */ System.out.println(" 平均=" + mean); } }
/****************************/ /* 0、1、... */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int i1, n, m, k1; int kk[] = new int [10]; /* 初期設定 */ for (i1 = 0; i1 < 10; i1++) kk[i1] = 0; /* 入力 */ System.out.print("データの数は? "); n = Integer.parseInt(in.readLine()); for (i1 = 0; i1 < n; i1++) { System.out.print(" 正の整数? "); m = Integer.parseInt(in.readLine()); k1 = m % 10; kk[k1]++; } /* 出力 */ for (i1 = 0; i1 < 10; i1++) { k1 = kk[i1] * 100 / n; System.out.print(i1 + " " + kk[i1] + " 個 " + k1 + " %\n"); } } }
/************************************/ /* 平均点と平均点以上の人数 */ /* coded by Y.Suganuma */ /************************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int i1, i2, max, mean, n, nin; /* 初期設定 */ mean = 0; nin = 0; max = 0; /* データの入力と平均点の計算 */ System.out.print("クラスの数は? "); n = Integer.parseInt(in.readLine()); int m[] = new int [n]; int num[] = new int [n]; int ten[][] = new int [n][0]; for (i1 = 0; i1 < n; i1++) { System.out.print((i1+1) + " 番目のクラスの人数は "); m[i1] = Integer.parseInt(in.readLine()); ten[i1] = new int [m[i1]]; nin += m[i1]; for (i2 = 0; i2 < m[i1]; i2++) { System.out.print(" " + (i2+1) + " 番目の人の点は? "); ten[i1][i2] = Integer.parseInt(in.readLine()); mean += ten[i1][i2]; } } mean /= nin; /* 比較 */ for (i1 = 0; i1 < n; i1++) { num[i1] = 0; for (i2 = 0; i2 < m[i1]; i2++) { if (ten[i1][i2] > max) max = ten[i1][i2]; if (ten[i1][i2] >= mean) num[i1]++; } } /* 出力 */ System.out.println("最高点=" + max + " 平均点=" + mean); System.out.print("平均点以上の人数は\n"); for (i1 = 0; i1 < n; i1=i1+1) System.out.println(" クラス " + (i1+1) + " 人数 " + num[i1]); } }
/****************************/ /* 点間の距離の計算 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); double max_r, r, r1, r2; int k1 = 0, k2 = 0, n, i1, i2; /* 初期設定 */ max_r = 0.0; /* データの入力 */ System.out.print("点の数は? "); n = Integer.parseInt(in.readLine()); double x[] = new double [n]; double y[] = new double [n]; for (i1 = 0; i1 < n; i1++) { System.out.print(" " + (i1+1) + " 番目の点のx座標は? "); x[i1] = Double.parseDouble(in.readLine()); System.out.print(" y座標は? "); y[i1] = Double.parseDouble(in.readLine()); } /* 計算 */ for (i1 = 0; i1 < n-1; i1++) { for (i2 = i1+1; i2 < n; i2++) { r1 = x[i1] - x[i2]; r2 = y[i1] - y[i2]; r = Math.sqrt(r1 * r1 + r2 * r2); if (r > max_r) { max_r = r; k1 = i1; k2 = i2; } } } /* 出力 */ System.out.println("点(" + x[k1] + "," + y[k1] + ")と(" + x[k2] + "," + y[k2] + ")間で距離は " + max_r); } }
/****************************/ /* 上位m人の平均 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); double mean, x; int i1, i2, m, n, num, min; /* 初期設定 */ num = 0; min = -1; mean = 0.0; /* 人数の入力 */ System.out.print("人数は? "); n = Integer.parseInt(in.readLine()); System.out.print("上位何人? "); m = Integer.parseInt(in.readLine()); double data[] = new double [m]; /* データの入力 */ for (i1 = 0; i1 < n; i1++) { System.out.print(" データは? "); x = Double.parseDouble(in.readLine()); // m人以下のとき if (num < m) { data[num] = x; if (min < 0 || x < data[min]) min = num; } // m人より多いとき else { if (x > data[min]) { data[min] = x; min = 0; for (i2 = 1; i2 < m; i2++) { if (data[i2] < data[min]) min = i2; } } } num++; } /* 平均値の計算 */ if (m <= 0 || n <= 0 || num <= 0) System.out.print("データがない!\n"); else { if (num < m) m = num; for (i1 = 0; i1 < m; i1++) mean += data[i1]; mean /= m; System.out.print("平均値=" + mean); } } }
/****************************/ /* 成分の%の計算(new) */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new FileReader("input")); double sum; int n, i1; /* 初期設定 */ sum = 0.0; /* データ数の読み込みとメモリの確保 */ n = Integer.parseInt(in.readLine()); double x[] = new double [n]; /* データの読み込み */ for (i1 = 0; i1 < n; i1++) { x[i1] = Double.parseDouble(in.readLine()); sum += x[i1]; } /* %の計算と出力 */ for (i1 = 0; i1 < n; i1++) System.out.print((i1+1) + " 番目の成分 " + (x[i1]/sum*100.0) + " %\n"); } }
/*******************************/ /* 平均点と平均点以下の人(new) */ /* coded by Y.Suganuma */ /*******************************/ import java.io.*; public class Test { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int i1, i2, mean = 0, n, nin = 0; /* データの入力と平均点の計算 */ System.out.print("クラスの数は? "); n = Integer.parseInt(in.readLine()); int m[] = new int [n]; // 各クラスの人数 int cl[][] = new int [n][0]; // 点数 String name[][] = new String [n][0]; // 名前 for (i1 = 0; i1 < n; i1++) { System.out.print((i1+1) + " 番目のクラスの人数は "); m[i1] = Integer.parseInt(in.readLine()); cl[i1] = new int [m[i1]]; name[i1] = new String [m[i1]]; nin += m[i1]; for (i2 = 0; i2 < m[i1]; i2++) { System.out.print(" " + (i2+1) + " 番目の人の名前は? "); name[i1][i2] = in.readLine(); System.out.print(" 点は? "); cl[i1][i2] = Integer.parseInt(in.readLine()); mean += cl[i1][i2]; } } mean /= nin; System.out.println("平均値 " + mean); /* 比較と出力 */ for (i1 = 0; i1 < n; i1++) { for (i2 = 0; i2 < m[i1]; i2++) { if (cl[i1][i2] <= mean) System.out.println(" " + cl[i1][i2] + " 点 " + name[i1][i2]); } } } }
菅沼ホーム | 演習解答例目次 | 本文目次 | 付録 | 索引 |