#include <stdio.h>
static int is_prime(int n) {
if (n < 2) return 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
int main(void) {
/* Case 1: Classic Ex 1 — unique 3-digit numbers formed from {1, 2, 3, 4} */
int perm_count = 0;
for (int a = 1; a <= 4; a++) {
for (int b = 1; b <= 4; b++) {
for (int c = 1; c <= 4; c++) {
if (a != b && a != c && b != c) perm_count++;
}
}
}
int c1_pass = (perm_count == 24);
/* Case 2: Classic Ex 12 & 13 — primes in [101,200] and 3-digit narcissistic numbers */
int prime_cnt = 0;
for (int n = 101; n <= 200; n++) {
if (is_prime(n)) prime_cnt++;
}
int narc[4] = {0}, ni = 0;
for (int n = 100; n <= 999; n++) {
int h = n / 100, t = (n / 10) % 10, u = n % 10;
if (h * h * h + t * t * t + u * u * u == n && ni < 4) {
narc[ni++] = n;
}
}
int c2_pass = (prime_cnt == 21 && ni == 4 &&
narc[0] == 153 && narc[1] == 370 && narc[2] == 371 && narc[3] == 407);
/* Case 3: Extra Classic — Matrix row swap (swap row 0 and row 2 of 3x3 matrix) */
int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int col = 0; col < 3; col++) {
int tmp = m[0][col];
m[0][col] = m[2][col];
m[2][col] = tmp;
}
int c3_pass = (m[0][0] == 7 && m[0][2] == 9 && m[2][0] == 1 && m[2][2] == 3);
/* Case 4: Extra Classic — Monkey eating peaches (day 10 has 1 left -> day 1 total) */
int peaches = 1;
for (int day = 9; day >= 1; day--) {
peaches = (peaches + 1) * 2;
}
int c4_pass = (peaches == 1534);
printf("{\n");
printf(" \"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\",\n");
printf(" \"cases\": [\n");
printf(" {\"name\": \"classic_ex1_unique_3digit\", \"expected\": 24, \"actual\": %d, \"passed\": %s},\n",
perm_count, c1_pass ? "true" : "false");
printf(" {\"name\": \"classic_primes_and_narcissistic\", \"expected\": [21, 153, 370, 371, 407], \"actual\": [%d, %d, %d, %d, %d], \"passed\": %s},\n",
prime_cnt, narc[0], narc[1], narc[2], narc[3], c2_pass ? "true" : "false");
printf(" {\"name\": \"matrix_row_swap\", \"expected\": [7, 9, 1, 3], \"actual\": [%d, %d, %d, %d], \"passed\": %s},\n",
m[0][0], m[0][2], m[2][0], m[2][2], c3_pass ? "true" : "false");
printf(" {\"name\": \"monkey_eating_peaches\", \"expected\": 1534, \"actual\": %d, \"passed\": %s}\n",
peaches, c4_pass ? "true" : "false");
printf(" ]\n");
printf("}\n");
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。
入口文件:projects/c-100-examples-lab/explore-100-examples.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-100-examples.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3208.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip