2013-07-01から1ヶ月間の記事一覧

20130731【C言語】符号付き整数と符号なし整数

お題 符号付き整数と符号なし整数を扱う ソース #include <stdio.h> int main(void){ short int i; /* 符号付き短整数 */ unsigned short int u; /* 符号なし短整数 */ u = 33000; i = u; printf("%hd %hu\n", i, u); return 0;} 実行結果 -32536 33000 ※補足 33000は</stdio.h>…

20130730【Java】テンパズルを解く

お題 テンパズルを解く ソース public class List12_3 { static class Bunsuu { public final int bunbo; public final int bunsi; Bunsuu(int bunbo, int bunsi){ this.bunbo = bunbo; this.bunsi = bunsi; if(bunbo == 0){ throw(new RuntimeException("0…

20130729【Java】4つの数からすべての数式を作り出す

お題 4つの数から(逆ポーランド記法の)すべての数式を作り出す ソースpublic class List12_2 { // 与えられた数 private static final String number = "1234"; private static StringBuffer createdNum = new StringBuffer(); static boolean issued = {f…

20130728【C言語】switch文を使う②

お題 switch文を使う ソース #include <stdio.h>#include <conio.h> int main(void){ int sep,num,eng; char ch; sep = 0; num = 0; eng = 0; do { printf("文字を入力してください: "); ch = getche(); switch(ch){ case '.': sep++; break; case '1': num++; break; case '\</conio.h></stdio.h>…

20130727【C言語】switch文を使う

お題 switch文を使う ソース #include <stdio.h> int main(void){ int a, b; char ch; printf("実行したい演算は\n"); printf("加算(A),減算(S),乗算(M),除算(D)のどれですか?\n"); /* 必ず有効な答えを入力させる */ do { printf("アルファベットの1文字を入力</stdio.h>…

20130726【C言語】continue文を使う

お題 continue文を使う お題 #include <stdio.h> int main(void){ int total, i, j; total = 0; do{ printf("数字を入力してください(0で終了): "); scanf("%d", &i); printf("もう一度、同じ数を入力してください: "); scanf("%d", &j); if(i != j) { printf("入</stdio.h>…

20130725【C言語】入力した文字のASCIIコードに相当する数のピリオドを出力する

お題 入力した文字のASCIIコードに相当する数のピリオドを出力する ソース #include <stdio.h>#include <conio.h> int main(void){ int i; char ch; for(i = 0; i < 10; i++) { printf("\n文字を入力してください: "); ch = getche(); printf("\n"); for(; ch; ch--) printf("</conio.h></stdio.h>…

20130724【Java】逆ポーランド記法を解く

お題 逆ポーランド記法を解く ソース import java.io.*; public class List12_1 { static class Bunsuu { public final int bunbo; public final int bunsi; Bunsuu(int bunbo, int bunsi) { this.bunbo = bunbo; this.bunsi = bunsi; if(bunbo == 0) { thro…

20130723【C言語】ループのネスト

お題 ループのネスト ソース #include <stdio.h> int main(void){ int answer, count, chances, right; for(count = 1; count < 11; count++) { printf(" %d + %d は? ", count, count); scanf("%d", &answer); if(answer == (count + count)) printf("正解!\n"); el</stdio.h>…

20130722【C言語】do-while文を使う

お題 do-while文を使う ソース #include <stdio.h> int main(void){ int a, b; char ch; printf("実行したい演算は\n"); printf("加算(A)、減算(S)、乗算(M)、除算(D)のどれですか?\n"); /* 必ず有効な答えを入力させる */ do { printf("アルファベットの1文字を入力</stdio.h>…

20130721【Java】バックトラックトラック法で数値分割問題を解く

お題 10個の数列を3つの仕切りで分割し、グループの中の整数の和の最大値を最小にする分割を求める。 ソース import java.text.DecimalFormat; public class List11_3 { private static final int array = {15, 3, 7, 6, 10, 4, 13, 2, 3, 6}; private stat…

20130720【Java】バックトラック法による数列分割問題

お題 10個の数列を3つの仕切りで分割し、グループの中の整数の和の最大値を最小にする分割を求める。 ソース public class List11_2 { private static final int value = {15, 3, 7, 6, 10, 4, 13, 2, 3, 6}; private static final int separator = 3; priv…

20130719【Java】ナップザック問題を解く

お題 ナップザック問題を解く ソース public class List11_1 { static class Product { public final int size; public final int value; public Product(int size, int value){ this.size = size; this.value = value; } } private static Product product …

20130718【C言語】関係演算子と論理演算子を使う

お題 関係演算子と論理演算子を使う ソース #include <stdio.h> int main(void){ int i, j; printf("第一の数を入力してください: "); scanf("%d", &i); printf("第ニの数を入力してください: "); scanf("%d", &j); /* 関係演算 */ printf("i < j %d\n", i < j); pr</stdio.h>…

20130717【Java】幅優先探索で7パズルを解く

お題 幅優先探索で7パズルを解く ソース import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Vector; public class List10_2 { // 今までに現れた局面を記録するクラス static class Pattern { public final long hash; pub…

20130716【C言語】素数かどうか調べる

お題 素数かどうか調べる ソース /* 素数テスタ */ #include <stdio.h> int main(void){ int num = 0, i, is_prime; for(; num <= 1 ;){ printf("判定したい数を入力して下さい: \n"); scanf("%d", &num); } /* 約数があるかどうかを調べる */ is_prime = 1; for(i = </stdio.h>…

20130715【C言語】足し算と掛け算をする

お題 掛け算と足し算をする ソース #include <stdio.h> /* 関数 sum のプロトタイプ宣言 */float sum(float num1, float num2); /* 関数 multiply のプロトタイプ宣言 */float multiply(float num1, float num2); int main(void){ int choice,i; float num1, num2; wh</stdio.h>…

20130714【C言語】オンスをカップに変換する

お題 オンスをカップに変換する(1カップ = 8オンス) ソース #include <stdio.h> double o_to_c(double); int main(void){ double o,answer; printf("変換したいオンスを入力してください: "); scanf("%lf",&o); answer = o_to_c(o); printf("%fオンスは%fカップで</stdio.h>…

20130713【C言語】ドルをポンドに変換する

お題 ドルをポンドに変換する(1ポンド = 2ドル) ソース #include <stdio.h> double convert(double); int main(void){ double dollar,answer; printf("変換したいドルを入力してください: "); scanf("%lf",&dollar); answer = convert(10.0); printf("%f", answe</stdio.h>…

20130711【C言語】平方根を求める

お題 10の平方根を求める ソース #include <stdio.h>#include <math.h> /* sqrt()を使うために必要 */ int main(void){ double answer; answer = sqrt(10.0); printf("%f", answer); return 0;} 実行結果 3.162278</math.h></stdio.h>

20130711【C言語】地球の日数を木星の年数に変換する

お題 地球の日数を木星の年数に変換する ソース /* 地球の日数を木製の年数(Jovian years)に変換するプログラム */#include <stdio.h> int main(void){ float e_days; /* 地球での日数 */ float j_years; /* 木星での年数 */ /* 地球での日数を得る */ printf("地球で</stdio.h>…

20130710【Java】エイト・クイーン問題を解く

お題 エイト・クイーン問題を解く ソース public class List10_1 { static boolean board = new boolean[8][8]; static{ for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ board[i][j] = false; } } } // (x,y)にクイーンをおけるかどうかチェック…

20130709【Java】BM法による文字列検索

お題 BM法による文字列検索を行う ソース public class List9_3 { private static int bmSearch(String text, String pattern){ // char型であらわされるあらゆる文字について // 「その文字で不一致が生じた時の比較点の移動量」を // あらかじめ計算して…

20130708【Java】KMP法による文字列検索

お題 KMP法による文字列検索 ソース public class List9_2 { private static int kmpSearch(String text, String pattern){ int textIndex = 1; int patternIndex = 0; int cacheTable = new int[pattern.length() + 1]; cacheTable[0] = cacheTable[1] = 0;…

20130707【Java】単純な文字列検索

お題 単純な文字列を検索を行う。(TeamSwiftからifを検索) ソース public class List9_1 { // 見つかった文字を返す private static int simpleSearch(String text, String pattern){ label: for(int n = 0; n < text.length(); n++){ // 確認のための表示…

20130706【Java】5次方程式の解を説く

お題 5次方程式の解を説く ソース public class List8_5 { public static double func(double x){ return x * x * x * x * x - 10.0 * x * x * x * x + 25.0 * x * x * x + 40.0 * x * x + 200.0 * x -500.0; } public static double binarySearch() { doubl…

20130706【C言語】構造体の利用

お題 構造体を利用する ソース #include <stdio.h> /* 構造体Pearsonの宣言 */typedef struct Person{ int age; double weight; double height;}Person; int main(void){ Person person; printf("年齢を入力してください。\n"); scanf("%d",&person.age); printf("体</stdio.h>…

20130704【Java】ハッシュマップのプログラムを作る

お題 ハッシュマップのプログラムを作る ソース import java.io.BufferedReader;import java.io.InputStreamReader;public class List7_3 { static class Wordset{ public String english; public String japanese; Wordset(String english, String japanese…

20130703【Java】重率を掛けたハッシュ値を生成する

お題 重率を掛けたハッシュ値を生成する ソース public class List7_2 { public static int makeHash2(String str, int hasmax){ int hash = 0, weight = 0; for(int n = 0; n < str.length(); ++n, ++weight){ if (weight > 7){ weight = 0; } hash += 1 <<…

20130702【Java】ハッシュ値生成関数を作る

お題 ハッシュ値生成関数を作る ソース public class List7_1 { public static int makeHash1(String str, int hashmax){ int hash = 0; for (int n = 0; n < str.length(); ++n){ hash += (int)str.charAt(n); } return hash % hashmax; } public static vo…