#include<stdio.h> intmain() { int nums[4] = {1, 2, 3, 4}; int output[4] = {1, 1, 1, 1}; //初值为1,避免出现乘积为0的情况 int i; for (i = 0; i < 4; i++) { if (i != 0) output[i] *= nums[0]; if (i != 1) output[i] *= nums[1]; if (i != 2) output[i] *= nums[2]; if (i != 3) output[i] *= nums[3]; } printf("["); for (i = 0; i < 3; i++) printf("%d,", output[i]); printf("%d]", output[3]); return0; }
#include<stdio.h> longlongfactorial(int x) { longlong sum = 1; int i; for (i = 1; i <= x; i++) sum *= i; return sum; } intcombinatorial(int m, int n) { if (m == 0) return1; returnfactorial(n) / (factorial(m) * factorial(n - m)); } intmain() { int n; scanf("%d", &n); int i = 0; n++; printf("["); for (i = 1; i <= n - 1; i++) { printf("%d,", combinatorial(i - 1, n - 1)); } printf("%d]", combinatorial(i - 1, n - 1)); return0; }
T3-2 买饮料
乐羊羊饮料厂正在举办一次促销优惠活动。乐羊羊 C 型饮料,凭 3 个瓶盖可以再换一瓶 C 型饮料,并且可以一直循环下去(但不允许暂借或赊账)。
请你计算一下,如果小明不浪费瓶盖,尽量地参加活动,那么,对于他初始买入的 n 瓶饮料,最后他一共能喝到多少瓶饮料。
#include<stdio.h> intmain() { int n, now, sum; scanf("%d", &n); sum = n; now = n; while (now >= 3) { sum += now / 3; now = now % 3 + now / 3; } printf("%d", sum); return0; }
#include<stdio.h> #include<string.h> #define MAXN 10000 intmain() { int n; scanf("%d", &n); int sum = n, p = 1, k = 0; bool vis[MAXN]; memset(vis, 1, sizeof(vis)); while (sum > 1) { if (p > n) p = 1; if (vis[p]) k++; if (k == 5) { vis[p] = 0; k = 0; sum--; } p++; } for (int i = 1; i <= n; i++) if (vis[i]) printf("%d ", i); return0; }