配列を使用して入力した数字から最大値・最小値・平均値を求めるプログラム
入力が一定ではないならリストの方が良いんだけどなぁ…
っていうのは学生の課題だから仕方ない?
とりあえず、配列の要素数は5にしています。
いつも通り、機能要件を満たす最低限のコードしか書いてません。
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner s = new Scanner(System.in); int[] array = new int[5]; int min = Integer.MAX_VALUE; int max = 0; int sum = 0; for(int element: array){ element = s.nextInt(); min = Math.min(min,element); max = Math.max(max,element); sum += element; } System.out.printf("Min=%d\n",min); System.out.printf("Max=%d\n",max); System.out.printf("Ave=%f\n",(float)sum/array.length); } }
入力
3 6 7 9 12
出力
Min=3 Max=12 Ave=7.400000
.NETを使って「時間差」と「指定した時間が指定した時間帯か」を求めるプログラム
人のソース引用してます
C#で特定の時間帯に含まれるか判定
時間を表すTimeSpanオブジェクトを作成する、情報を取得する - .NET Tips (VB.NET,C#...)
using System; public class HowToUseDateTime{ public static void Main(){ DateTime a = new DateTime(2018, 9, 28, 10, 05, 0); DateTime b = new DateTime(2018, 9, 28, 15, 10, 0); if(InRange(a,1000,1015)){ Console.WriteLine("aのインスタンスは正しい時間帯"); } else { Console.WriteLine("aのインスタンスは正しくない時間帯"); } TimeSpan ts = a - b; Console.WriteLine("時間差は{0}です。",ts.ToString("c")); } public static bool InRange(DateTime dt, int from, int to){ string stringhhmm = dt.ToString("HHmm"); int inthhmm = Int16.Parse(stringhhmm); return inthhmm >= from && inthhmm <= to; } }
出力
aのインスタンスは正しい時間帯 時間差は-05:05:00です。
多次元配列を使って遊ぶプログラム
以下のような模様を出力してほしいらしい。
■□■□■□■□■□■ □□■□■□■□■□■ ■■■□■□■□■□■ □□□□■□■□■□■ ■■■■■□■□■□■ □□□□□□■□■□■ ■■■■■■■□■□■ □□□□□□□□■□■ ■■■■■■■■■□■ □□□□□□□□□□■ ■■■■■■■■■■■
コード
import java.util.*; public class Main { public static void main(String[] args) throws Exception { final int size = 11; boolean[][] drawFlags = new boolean[size][size]; for(int line = 0; line < size; ++line){ if(line%2==0){ for(int anotherLine = 0; anotherLine <= line; ++anotherLine){ drawFlags[line][anotherLine] = true; drawFlags[anotherLine][line] = true; } } } for(int row = 0; row < size; ++row){ for(int colomn = 0; colomn < size; ++colomn){ String mark = drawFlags[colomn][row] ? "■" : "□"; System.out.print(mark); } System.out.println(); } } }
出力
■□■□■□■□■□■ □□■□■□■□■□■ ■■■□■□■□■□■ □□□□■□■□■□■ ■■■■■□■□■□■ □□□□□□■□■□■ ■■■■■■■□■□■ □□□□□□□□■□■ ■■■■■■■■■□■ □□□□□□□□□□■ ■■■■■■■■■■■
sizeを変えると描画数も変わるように作った。
文字列1から文字列2を検索するプログラム+解説
strstrっていう関数がある
#include <string.h> char *strstr(const char *s1, const char *s2);
これを学習目的で自作する人が居たので、類似のソースを見つけて解説してみた
printfで出力の途中経過を混ぜてます
ソース
#include <stdio.h> char *myStrstr(const char *s1, const char *s2) { printf("start\n"); const char *p1 = s1; const char *p2 = s2; while (*p1 && *p2) { if (*p1 == *p2) { p1++; p2++; printf("true [p1:%s] [p2:%s]\n",p1,p2); } else { long diff = (p2-s2-1); p1 -= p2 - s2 - 1; p2 = s2; printf("false [p1:%s] [p2:%s] ",p1,p2); printf("p1 pointer moves %ld\n",diff); } } printf("end\n"); printf("p2 is [%s], [%s] - ([%s] - [%s]) = [%s]\n",p2,p1,p2,s2,(p1 - (p2 - s2))); return (*p2 ? NULL : (char *)(p1 - (p2 - s2))); } int main(void){ const char* str1 = "eyanyaoewar9nyan349reawr"; const char* str2 = "nyan"; char* str3 = myStrstr(str1,str2); printf("result is %s",str3); }
str1を「eyanyaoewar9nyan349reawr」
str2を「nyan」とした。
出力結果
start false [p1:yanyaoewar9nyan349reawr] [p2:nyan] p1 pointer moves -1 false [p1:anyaoewar9nyan349reawr] [p2:nyan] p1 pointer moves -1 false [p1:nyaoewar9nyan349reawr] [p2:nyan] p1 pointer moves -1 true [p1:yaoewar9nyan349reawr] [p2:yan] true [p1:aoewar9nyan349reawr] [p2:an] true [p1:oewar9nyan349reawr] [p2:n] false [p1:yaoewar9nyan349reawr] [p2:nyan] p1 pointer moves 2 false [p1:aoewar9nyan349reawr] [p2:nyan] p1 pointer moves -1 false [p1:oewar9nyan349reawr] [p2:nyan] p1 pointer moves -1 false [p1:ewar9nyan349reawr] [p2:nyan] p1 pointer moves -1 false [p1:war9nyan349reawr] [p2:nyan] p1 pointer moves -1 false [p1:ar9nyan349reawr] [p2:nyan] p1 pointer moves -1 false [p1:r9nyan349reawr] [p2:nyan] p1 pointer moves -1 false [p1:9nyan349reawr] [p2:nyan] p1 pointer moves -1 false [p1:nyan349reawr] [p2:nyan] p1 pointer moves -1 true [p1:yan349reawr] [p2:yan] true [p1:an349reawr] [p2:an] true [p1:n349reawr] [p2:n] true [p1:349reawr] [p2:] end p2 is [], [349reawr] - ([] - [nyan]) = [nyan349reawr] result is nyan349reawr
解説
p1のp2の1文字目を比較して
- 一致したら次の文字を比較、すなわち先頭文字を削る(p1++;p2++;)…①
- 一致しなかったらp1のポインタの位置をp2-s2-1分移動し、(p1 -= p2 - s2 - 1;)し、p2の文字列を元に戻す(p2=s2;)…②
p2-s2-1分、ポインタを移動するというのは、
①でp1を何度インクリメントしたかで結果が変わる
- 0回なら1個左に移動
- 1回なら移動しない
- 2回なら1個右に移動
- 3回なら2個右に移動
って感じ
そのとき、p1の文字列の状態は、
ポインタが、
- 1個左に移動したら、p1から1文字を削除
- 移動しないなら、p1を変更しない
- 1個右に移動したら、p1を1文字復元
- 2個右に移動したら、p1を2文字復元
って感じ
ループは、以下の条件を満たすまで回り続ける。
- ①が呼ばれることでp2のポインタが文字列外へ移動するか
- ②が呼ばれることでp1のポインタが文字列外へ移動するか
戻り値は、
p2のポインタの先頭が
- 存在すれば、NULL
- 存在しなければ、p2と一致した位置のp1のポインタ
を返す。
電球がONである数を数えるプログラム
問題
1からnまでの数字が振られたn個の電球がある。これらの電球に対して、次の操作を順に行う。
・1の倍数の電球のON/OFFを切り替える。
・2の倍数の電球のON/OFFを切り替える。
・mの倍数の電球のON/OFFを切り替える。
このとき、最終的にONになっている電球の数を出力するプログラム作成せよ。最初は全ての電球がOFFになっているものとする。
入力例 出力例
12 3 電球ONの数:6
25 9 電球ONの数:14
500 162 電球ONの数:256
1000 1000 電球ONの数:31
コード
#include <stdio.h> #include <stdlib.h> //電球をnの倍数で切り替える処理 void switchBulbs(int *bulbs, int multiple, int count){ for(int num = multiple-1; num < count; num += multiple){ if(bulbs[num] == 0){ bulbs[num] = 1; }else{ bulbs[num] = 0; } } } int main(void){ int n,m; scanf("%d%d",&n,&m); int *bulbs = (int*)malloc(n * sizeof(int)); for(int num = 0; num < n; ++num){ bulbs[num] = 0; } for(int num = 1; num < m + 1 ; ++num){ switchBulbs(bulbs, num, n); } int sum = 0; for(int num = 0; num < n; ++num){ if(bulbs[num] == 1){ ++sum; } } free(bulbs); printf("電球ONの数:%d",sum); }
入力
1000 1000
出力
電球ONの数:31
ただ、もっとスマートなコードがあったので
あまり宛てにしないでください。。。orz
2次元配列を使ったプログラム
#include <stdio.h> int main(void){ //NOTE:本当は、inputを可変長の配列にすると使い勝手がいい int input[2][5] = {}; for(int row = 0; row < 5; ++row){ scanf("%d%d",&input[0][row],&input[1][row]); } int rowSize = sizeof(input[0]) / sizeof(input[0][0]); int colomnSize = sizeof(input) / sizeof(input[0]); printf("======各行の平均======\n"); for(int row = 0; row < rowSize; ++row){ printf("%d %d %0.1f\n",input[0][row],input[1][row],(float)(input[0][row]+input[1][row])/colomnSize); } printf("======各列の平均======\n"); int sum[2] = {}; for(int row = 0; row < rowSize; ++row){ for(int colomn = 0; colomn < colomnSize; ++colomn){ sum[colomn] += input[colomn][row]; } printf("%d %d\n",input[0][row],input[1][row]); } printf("%0.1f %0.1f\n",(float)sum[0]/rowSize,(float)sum[1]/rowSize); }
入力
70 65 92 83 75 85 66 74 89 92
出力
======各行の平均====== 70 65 67.5 92 83 87.5 75 85 80.0 66 74 70.0 89 92 90.5 ======各列の平均====== 70 65 92 83 75 85 66 74 89 92 78.4 79.8
身長と標準体重の対応表を表示するプログラム
printfとscanfもっとうまく書ける気がする。。
#include <stdio.h> int main(void){ int from,to,each; printf("何cmから:"); scanf("%d",&from); printf("%d\n",from); printf("何cmまで:"); scanf("%d",&to); printf("%d\n",to); printf("何cmごと:"); scanf("%d",&each); printf("%d\n",each); for(from; from <= to; from = from + each){ float stdWeight = (from - 100) * 0.9; printf("%dcm %0.2fkg\n",from,stdWeight); } }
入力
150 190 5
出力
何cmから:150 何cmまで:190 何cmごと:5 150cm 45.00kg 155cm 49.50kg 160cm 54.00kg 165cm 58.50kg 170cm 63.00kg 175cm 67.50kg 180cm 72.00kg 185cm 76.50kg 190cm 81.00kg