プログラミング+α学習ブログ

勉強したことの備忘録です。

2013-10-10から1日間の記事一覧

名前を尋ねて挨拶するプログラム

#include <stdio.h> int main(void) { char name[40]; printf("お名前は:"); scanf("%s", name); printf("はじめまして、%sさん!", name); return (0); } 実行結果 お名前は:Shibata はじめまして、Shibataさん!</stdio.h>

文字列を空文字列にするプログラム

#include <stdio.h> int main(void) { char str[] = "ABC"; printf("str=%s\n", str); // strは"ABC" str[0] = '\0'; // 配列の先頭要素にナル文字を代入 puts("文字列strを空文字列にしました。"); printf("str=%s\n", str); // strは空文字列 return (0); } 実行結</stdio.h>…

ABC\0DEFで初期化された文字列を表示するプログラム

#include <stdio.h> int main(void) { char str[] = "ABC\0DEF"; // 途中にナル文字がある printf("文字列strは%sです。\n", str); return (0); } 実行結果 文字列strはABCです。</stdio.h>

配列に文字列を格納して表示するプログラム(2)

#include <stdio.h> int main(void) { char str[] = "String"; // 初期化 printf("文字列strは%sです。\n", str); return (0); } 実行結果 文字列strはStringです。</stdio.h>

配列に文字列を格納して表示するプログラム(1)

#include <stdio.h> int main(void) { char str[7]; // 文字列を格納する配列 str[0] = 'S'; str[1] = 'T'; str[2] = 'R'; str[3] = 'I'; str[4] = 'N'; str[5] = 'G'; str[6] = '\0'; printf("文字列strは%sです。\n", str); return (0); } 実行結果 文字列strはSTRI</stdio.h>…

入力に現れた改行の数を表示するプログラム

#include <stdio.h> int main(void) { int ch; int n_count = 0; // 改行文字の数 while ((ch = getchar()) != EOF) if (ch == '\n') n_count++; printf("行数:%d\n", n_count); return (0); } 実行結果 Hello! This is a pen. 行数:2</stdio.h>

標準入力からの入力を標準出力にコピーするプログラム

#include <stdio.h> int main(void) { int ch; while ((ch = getchar()) != EOF) putchar(ch); return (0); } 実行結果 Hello! Hello! This is a pen. This is a pen.</stdio.h>

入力に出現する数字をカウントして縦向きの棒グラフで表示するプログラム

#include int main(void) { int i, j, ch; int cnt_max = 0; // 出現回数の最大値 int cnt[10] = {0}; // 数字文字の出現回数 while (1) { // 無限ループ ch = getchar(); if (ch == EOF) break; if (ch >= '0' && ch cnt_max) cnt_max = cnt[i]; puts("数字…

入力に出現する数字をカウントして横向きの棒グラフで表示するプログラム

#include <stdio.h> int main(void) { int i, j, ch; int cnt[10] = {0}; // 数字文字の出現回数 while (1) { // 無限ループ ch = getchar(); if (ch == EOF) break; if (ch >= '0' && ch <= '9') cnt[ch - '0']++; } puts("数字文字の出現回数"); for (i = 0; i < 10</stdio.h>…

入力に出現する数字をカウントして表示するプログラム

#include <stdio.h> int main(void) { int i, ch; int cnt[10] = {0}; // 数字文字の出現回数(全ての要素を0で初期化) while (1) { // 無限ループ ch = getchar(); if (ch == EOF) break; if (ch >= '0' && ch <= '9') cnt[ch - '0']++; } puts("数字文字の出現回数</stdio.h>…

ハノイの塔

わからない(ToT) #include <stdio.h> #define N 3 // 枚数 /*--- 円盤をx軸からy軸へ移動 ---*/ void move(int no, int x, int y) { if (no > 1) move(no - 1, x, 6 - x - y); printf("%dを%d軸から%d軸へ移動\n", no, x, y); if (no > 1) move(no - 1, 6 - x - y, y)</stdio.h>…

組み合わせの数を求めるプログラム

#include <stdio.h> /*--- 異なるn個からr個の整数を取り出す組み合わせの数を返す ---*/ int combination(int n, int r) { if (r == 0 || r == n) return (1); else if (r == 1) return (n); return (combination(n - 1, r - 1) + combination(n - 1, r)); } int mai</stdio.h>…