Skip to content

Instantly share code, notes, and snippets.

@geta6
Last active January 3, 2016 20:29
Show Gist options
  • Save geta6/8515449 to your computer and use it in GitHub Desktop.
Save geta6/8515449 to your computer and use it in GitHub Desktop.
システムプログラミング最終試験

E1

自分の氏名、学籍番号、login名、学年、座席位置を出力せよ。

E2

stdinから与えられた10進の文字をscanf("%d", ...)を用いて取得し、数字の各桁の合計を求めよ。

% ./a.out
? 768042
27

E3

2つの文字列を入力し、最初の文字列の中に2つ目の文字列が何度出現するか、重複も考慮にいれ数えよ。

入力にはgetsまたはfgetsを用いること。

% ./a.out
? ABABA
? ABA
2

E4

引数として与えられたテキストファイルの最も長い行と最も短い行を出力するプログラムを作成せよ。空行は無視すること。

This is a first line.
The second line is longer.
Third line is not.
This line is a little bit longer.
So, find out the longest one.
% ./a.out a.txt
Longest: line 4
  Third line is a little bit longer.
Shortest: line 3
  Third line is not.

E5

フィボナッチ数列を40回求める時間を、setitimerを用いてシグナルで計測せよ。

int fib(int n) {
  if (n == 0 || n == 1) return 1;
  else return fib(n - 1) + fib(n - 2);
}
#include <stdio.h>
int main()
{
printf("氏名: ***\n");
printf("学籍番号: ***\n");
printf("login 名: ***\n");
printf("学年: 4\n");
printf("列: 1\n");
printf("行: 5\n");
}
#include <stdio.h>
int main()
{
int j, i, a;
printf("? ");
scanf("%d", &j);
for (i = 1;;) {
if (j < i * 10) break;
i = i * 10;
}
a = 0;
while (0 < i) {
a += j / i;
j = j - i * (j / i);
i = i / 10;
}
printf("%d\n", a);
}
/*
空行の無視を忘れた
*/
#include <stdio.h>
#include <string.h>
int main() {
int i, j, a, tl, sl, m;
char target[BUFSIZ], search[BUFSIZ], tmp[BUFSIZ];
printf("? ");
gets(target);
printf("? ");
gets(search);
tl = strlen(target);
sl = strlen(search);
m = tl - sl + 1;
for (i = 0; i < m; i++) {
memset(tmp, '\0', BUFSIZ);
for (j = 0; j < sl; j++)
sprintf(tmp, "%s%c", tmp, target[i+j]);
if (strcmp(search, tmp) == 0) a++;
}
printf("%d\n", a);
}
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fp;
char buf[BUFSIZ];
char l[BUFSIZ], s[BUFSIZ];
int x = 0, sl = 0, ll = 0;
fp = fopen(argv[1], "r");
while (fgets(buf, BUFSIZ, fp)) {
if (x == 0) {
sl = x;
strcpy(s, buf);
}
x++;
if (strlen(l) < strlen(buf)) {
ll = x;
strcpy(l, buf);
}
if (strlen(s) > strlen(buf)) {
sl = x;
strcpy(s, buf);
}
}
printf("Longest: line %d\n", ll);
printf(" %s", l);
printf("Shortest: line %d\n", sl);
printf(" %s", s);
}
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
int a;
int fib (int n)
{
if (n == 0 || n == 1) return 1;
else return fib(n - 1) + fib(n - 2);
}
void sig(int signal)
{
a++;
}
int main()
{
struct itimerval itv;
int n;
signal(SIGALRM, sig);
n = 0;
itv.it_interval.tv_sec = 1;
itv.it_interval.tv_usec = 0;
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &itv, NULL);
for (n = 0; n <= 40; n++) {
printf("fib(%d) = %d\n", n, fib(n));
}
printf("total %d seconds\n", a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment