目前更新到70题!

目前WA:飞机起飞速度;字符串替换;GPS通信协议。

前言

代码均可AC,解析参见我的博客NOJ(C)易错总结 (annesede.github.io)。(已更新到70题!)

部分题目由于过于简单一次性AC,未能考虑可读性(其实是懒得改)。

题目分类如下,请按题目名称查找:

  • 入门题:只涉及基本的输入输出
  • 简单题:不涉及算法但有较多易错点
  • 中等题:涉及简单的算法或数学知识
  • 难题:涉及复杂的算法或数学知识

入门题

Hello World

#includeint main(){printf("Hello World");return 0;}

A+B

#includeint main(){int a = 0, b = 0;scanf("%d %d",&a,&b);printf("%d",a+b);return 0;}

数据类型大小及范围

#include #include int main(){int id = 0;scanf("%d",&id);switch (id) {case 1: // charprintf("%llu,%d,%d\n",sizeof(char),CHAR_MIN,CHAR_MAX);break;case 2: // unsigned charprintf("%llu,%u,%u\n",sizeof(unsigned char),0,UCHAR_MAX);break;case 3: // shortprintf("%llu,%hd,%hd\n",sizeof(short),SHRT_MIN,SHRT_MAX);break;case 4: // unsigned shortprintf("%llu,%hu,%hu\n",sizeof(unsigned short),0,USHRT_MAX);break;case 5: // intprintf("%llu,%d,%d\n",sizeof(int),INT_MIN,INT_MAX);break;case 6: // unsigned intprintf("%llu,%u,%u\n",sizeof(unsigned int),0,UINT_MAX);break;case 7: //longprintf("%llu,%ld,%ld\n",sizeof(long),LONG_MIN,LONG_MAX);break;case 8: // unsigned longprintf("%llu,%u,%lu\n",sizeof(unsigned long),0,ULONG_MAX);break;case 9: // long longprintf("%llu,%lld,%lld\n",sizeof(long long),LLONG_MIN,LLONG_MAX);break;case 10: // unsigned long longprintf("%llu,%u,%llu\n",sizeof(unsigned long long),0,ULLONG_MAX);break;}return 0;}

平均值

#include int main(){int a = 0, b = 0;scanf("%d %d",&a,&b);int c = ((b-a)>>1)+a;printf("%d",c);return 0;}

进制转换

#include int main(){unsigned int a = 0;scanf("%d",&a);printf("%X,%o",a,a);return 0;}

浮点数输出

#include int main(){double a = 0.0f;scanf("%lf",&a);printf("%.6lf,%.2lf,%.8lf",a,a,a);return 0;}

动态宽度输出

#include int main(){int n = 0, m = 0, k = 0;scanf("%d %d",&n,&m);int tmp = n;while (tmp) {tmp /= 10;++k;}for (int i = 0; i < m-k; ++i) {printf("%d",0);}printf("%d",n);return 0;}

计算地球上两点之间的距离

#include #include #define RADIUS 6371.000000#define PI 3.1415926int main(){double phi1, phi2, lambda1, lambda2, distance;scanf("%lf %lf",&phi1,&lambda1);scanf("%lf %lf",&phi2,&lambda2);phi1 = phi1*PI/180;phi2 = phi2*PI/180;lambda1 = lambda1*PI/180;lambda2 = lambda2*PI/180;double havRatio = (1-cos(phi2-phi1))/2+cos(phi1)*cos(phi2)*(1-cos(lambda2-lambda1))/2;distance = asin(sqrt(havRatio))*2*RADIUS;printf("%.4lfkm",distance);return 0;}

风寒指数

#include #include int main(){double v,T;scanf("%lf %lf",&v,&T);int chill = 13.12f+0.6215f*T-11.37f*pow(v,0.16f)+0.3965f*T*pow(v,0.16f)+0.5f;printf("%d",chill);return 0;}

乘数模

#include int main(){long long a,b,m,r;scanf("%lld %lld %lld",&a,&b,&m);r = ((a%m)*(b%m))%m;printf("%lld",r);return 0;}

组合数

#includeint main(){int cnt = 0, n, m;scanf("%d",&n);for(int a = 0; a <= 9; ++a){for(int b = 0; b <= 9; ++b){for(int c = 0; c <= 9; ++c){for(int d = 0; d <= 9; ++d){m = a+b+c+d;if (m == n){++cnt;}}}}}printf("%d",cnt);return 0;}

余数和

#include int main() {unsigned int sum = 0, n, k;scanf("%u %u",&n,&k);for (unsigned int i = 1; i <= n; ++i) {sum += k%i;}printf("%u",sum);return 0;}

佩尔数

#include int PA(int n){if (n == 0) return 0;else if (n == 1) return 1;return 2*PA(n-1)+PA(n-2);}int PB(int n){int p0 = 0, p1 = 1, pn;for (int i = 0; i <= n; ++i) {if (i == 0) pn = p0;else if (i == 1) pn = p1;else {pn = 2 * p1 + p0;p0 = p1;p1 = pn;}}return pn;}int main() {int n, p;scanf("%d",&n);if (n&1) p = PA(n);else p = PB(n);printf("%d",p);return 0;}

可变参数累加

#include #include int sum(int start,...){va_list vaList;int sum = 0;va_start(vaList,start);while (start){sum += start;start = va_arg(vaList,int);}va_end(vaList);return sum;}int main() {int a,b,c,d,e,f;scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f);int sumMinus = sum(a,b,0) - sum(c,d,e,f,0);printf("%d",sumMinus);return 0;}

可变参数平均

#include #include double avg(int num,...){va_list vaList;double sum = 0.0f;va_start(vaList,num);for (int i = 0; i < num; ++i) {sum += va_arg(vaList,int);}va_end(vaList);return sum/num;}int main() {int a,b,c,d,e;scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);double avgMinus = avg(2,a,b) - avg(3,c,d,e);printf("%.4f",avgMinus);return 0;}

航空旅行

#include #include void pass(int a, int b, int c, int d, int e) {bool flag = false;if (a <= e && (b + c) <= d) flag = true;if (b <= e && (a + c) <= d) flag = true;if (c <= e && (a + b) <= d) flag = true;if (flag) printf("YES\n");else printf("NO\n");}int main() {int n, a, b, c, d, e;scanf("%d",&n);while (n--){scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);pass(a, b, c, d, e);}return 0;}

波士顿房价预测

#include double nAvg(double arr[], int n) {double sum = 0;for (int i = 0; i < n; ++i) {sum += arr[i];}return sum / n;}int main() {int n;scanf("%d", &n);double x[n], y[n];for (int i = 0; i < n; ++i) {scanf("%lf %lf", &x[i], &y[i]);}double xBar = nAvg(x, n), yBar = nAvg(y, n);double sumUp = 0, sumDown = 0;for (int i = 0; i < n; ++i) {sumUp += (x[i] - xBar) * (y[i] - yBar);}for (int i = 0; i < n; ++i) {sumDown += (x[i] - xBar) * (x[i] - xBar);}double b = sumUp / sumDown;double a = yBar - b * xBar;printf("Y=%.4lf+%.4lf*X",a,b);return 0;}

蒙特卡罗方法求积分

#include #include #include double func1(double x) {return pow(x, 4) * exp(-x);}double func2(double x) {return x * x + 1;}double func3(double x) {return cos(x);}double func4(double x) {return sqrt(x) * (x - 2);}double func5(double x) {return 2 * sin(x) - 5 * cos(x);}double func(int m, double x) {switch (m) {case 1: return func1(x);case 2: return func2(x);case 3: return func3(x);case 4: return func4(x);case 5: return func5(x);default: return 0;}}double mtk(int m, double a, double b, int n) {srand(RAND_MAX);double w = b - a, sum = 0;for (int i = 1; i < n; ++i) {double x = ((double)rand() / RAND_MAX) * w + a;sum += func(m, x);}sum *= w / n;return sum;}int main() {int m, n;double a, b;scanf("%d %lf %lf %d", &m, &a, &b, &n);printf("%.6lf", mtk(m, a, b, n));return 0;}

大小写交换

#include void strSwapCase(char str[]) {for (int i = 0; str[i] != '\0'; ++i) {if ('a' <= str[i] && str[i] <= 'z')str[i] = (char) str[i] - 'a' + 'A';else if ('A' <= str[i] && str[i] <= 'Z')str[i] = (char) str[i] - 'A' + 'a';}}int main() {char str[1000] = "";scanf("%[^\n]", str);strSwapCase(str);puts(str);return 0;}

DNS双螺旋结构

#include void putsDna1(){printf(" AT \n");printf("T--A\n");printf(" A----T \n");printf("T------A\n");printf("T------A\n");printf(" G----C \n");printf("T--A\n");printf(" GC \n");}void putsDna2(){printf(" CG \n");printf("C--G\n");printf(" A----T \n");printf("A------T\n");printf("T------A\n");printf(" A----T \n");printf("A--T\n");printf(" GC \n");}void putsDna3(){printf(" AT \n");printf("C--G\n");printf(" T----A \n");printf("C------G\n");printf("C------G\n");printf(" T----A \n");printf("G--C\n");printf(" AT \n");}int main() {int n;scanf("%d", &n);for (int i = 1; i <= n/2; ++i) {if (i % 3 == 1) putsDna1();else if (i % 3 == 2) putsDna2();else putsDna3();}return 0;}

PID控制

#include typedef struct PIDController {double Kp, Ki, Kd;double preError, integral;} PIDData;double PIDCalculate(PIDData *pid, double setPoint, double measuredValue) {double error = setPoint - measuredValue;pid->integral += error;double differential = error - pid->preError;double output = pid->Kp * error + pid->Ki * pid->integral + pid->Kd * differential;pid->preError = error;return output;}int main() {double setPoint, measuredValue;int time;PIDData pid = {0};scanf("%lf %lf %lf", &pid.Kp, &pid.Ki, &pid.Kd);scanf("%lf %lf %d", &setPoint, &measuredValue, &time);for (int i = 1; i <= time; ++i) {double output = PIDCalculate(&pid, setPoint, measuredValue);measuredValue += output;printf("%d %.6lf\n", i, measuredValue);}return 0;}

简单题

颜色模型转换

#include #include #include double findmax(double a, double b, double c) {return a >= b ? (a >= c ? a : c) : (b >= c ? b : c);}double findmin(double a, double b, double c) {return a <= b ? (a <= c ? a : c) : (b  0 ? R : 0) / 255.0f);g = (double)((G > 0 ? G : 0) / 255.0f);b = (double)((B > 0 ? B : 0) / 255.0f);max = findmax(r, g, b);min = findmin(r, g, b);V = max;if (max < 1e-9) {S = 0.0f;}else {S = (max - min) / max;}if (max - min < 1e-9) {H = 0.0f;}else {if (max == r) {H = 60.0f * (g - b) / (max - min);}else if (max == g) {H = 60.0f * (2.0f + (b - r) / (max - min));}else if (max == b) {H = 60.0f * (4.0f + (r - g) / (max - min));}else {return -1;}if (H < 1e-9) {H = H + 360.0f;}}printf("%.4lf,%.4lf%%,%.4lf%%", H, S * 100, V * 100);return 0;}

方阵

#include int main(){int n,m;scanf("%d",&n);for (int i = 0; i < n; ++i) {for (int j = 0; j  0 ? (i-j) : (j-i);printf("%d",m);}printf("\n");}return 0;}

分数的加、减、乘、除

#include int gcd(int a,int b) {if (b == 0) return a;else return gcd(b,a%b);}int main() {int a,b,c,d;scanf("%d",&a);getchar();scanf("%d",&b);getchar();scanf("%d",&c);getchar();scanf("%d",&d);printf("(%d/%d)+(%d/%d)=%d/%d\n",a,b,c,d,(a*d+b*c)/gcd(a*d+b*c,b*d),b*d/gcd(a*d+b*c,b*d));printf("(%d/%d)-(%d/%d)=%d/%d\n",a,b,c,d,(a*d-b*c)/gcd(a*d-b*c,b*d),b*d/gcd(a*d-b*c,b*d));printf("(%d/%d)*(%d/%d)=%d/%d\n",a,b,c,d,(a*c)/gcd(a*c,b*d),b*d/gcd(a*c,b*d));printf("(%d/%d)/(%d/%d)=%d/%d\n",a,b,c,d,(a*d)/gcd(a*d,b*c),b*c/gcd(a*d,b*c));return 0;}

操作数

#include int sumDig(int n){int sum = 0;while(n){sum += n%10;n /= 10;}return sum;}int main(){int n,cnt=0;scanf("%d",&n);while(n){n -= sumDig(n);++cnt;}printf("%d",cnt);return 0;}

比率

#include #include int gcd(int a,int b){if (b == 0) return a;else return gcd(b,a%b);}int cntDig(double x){int cnt = 0;while(x != floor(x)){++cnt;x *= 10;}return cnt;}int main(){double x;scanf("%lf",&x);int cnt = cntDig(x), n, m, g;m = pow(10,cnt);n = (int)(x*m);g = gcd(m,n);m /= g;n /= g;printf("%d/%d",n,m);return 0;}

级数和

#include #include double decimal (int n){double m = (double)n+1;while(floor(m)){m /= 10;}return m+n;}int main(){int n;scanf("%d",&n);double sum = 0.0f, term;for (int i = 1; i < n; ++i) {term = decimal(i);sum += term;printf("%g+",term);}term = decimal(n);sum += term;printf("%g=%g",term,sum);return 0;}

对称数

#include int main(){int n;scanf("%d",&n);int a = (n/100)%10;int b = (n/10)%10;int c = n%10;if ((b == 0 || b == 1 || b == 8) && ((a == 6 && c == 9) || (a == 9 && c == 6)|| ((a == 0 || a == 1 || a == 8) && (c == 0 || c == 1 || c == 8)))){printf("Yes\n");} else {printf("No\n");}return 0;}

倍数和

#include int main(){unsigned int t = 0;scanf("%u", &t);unsigned int arrn[t];for(unsigned int i = 0; i < t; i++){scanf("%u", &arrn[i]);}for(unsigned int j = 0; j < t; j++){unsigned int res = 0;for(unsigned int x = 1; x < arrn[j]; x++){if(x % 3 == 0 || x % 5 == 0){res += x;}}printf("%u\n", res);}return 0;}

最大数字

#include #include bool isNonDecr(unsigned int n){unsigned int left;while(n){left = (n/10)%10;if (left > (n%10)){return false;}n /= 10;}return true;}int main() {unsigned int n, res;scanf("%u",&n);while(!isNonDecr(n)){--n;}printf("%u",n);return 0;}

毕达哥拉斯三元组

#include unsigned long long pythagoras(unsigned int sum){// a as short catheti, b as long catheti, c as hypotenuseunsigned int a,b,c;for (a = 1; a <= sum/4; ++a){for (b = a+1; b  c)&& (a*a+b*b == c*c)){return a*b*c;}}}}int main(){unsigned int n;scanf("%u",&n);printf("%llu", pythagoras(n));return 0;}

竖式乘法

#include #include typedef unsigned int uint;uint digit(uint x){uint cnt = 0;if (x == 0){return 1;}while(x){x /= 10;++cnt;}return cnt;}uint getDigital(uint x, uint n){x /= (uint)pow(10,n-1);return x%10;}int main(){uint up, down;scanf("%u %u",&up,&down);uint ans = up*down, len = digit(ans)+1, downLen = digit(down);for (uint i = 0; i < len-digit(up); ++i) {printf(" ");}printf("%u\n",up);printf("x");for (uint i = 1; i <= len-downLen-1; ++i) {printf(" ");}printf("%u\n",down);for (uint i = 1; i <= len; ++i) {printf("-");}printf("\n");uint tmp;for (uint i = 1; i <= downLen; ++i) {tmp = getDigital(down,i)*up;if (i == downLen){printf("+");} else {for (uint j = 1; j <= len-digit(tmp)-i+1; ++j) {printf(" ");}}printf("%u\n",tmp);}for (uint i = 1; i <= len; ++i) {printf("-");}printf("\n");printf(" %u",ans);return 0;}

查找数列

#include int main(){int cnt = 1, sum = 0, n, res;scanf("%d",&n);while(n-sum > 0){sum += cnt;++cnt;}sum -= cnt;res = n-sum == 0 ? cnt : (n-sum-1);printf("%d",res);return 0;}

俄罗斯农夫乘法

#include int main(){int multi = 0, a, b;scanf("%d %d",&a,&b);while(a){printf("%d %d\n",a,b);if (a&1) {multi += b;}a >>= 1;b <<= 1;}printf("%d",multi);return 0;}

哈沙德数

#include int HarshadNumber(int n){int t = n, s = 0;while (t) {s += t%10;t /= 10;}if ((s == 0) || (n%s != 0)) return 0;if (s == 1) return 1;return n/s;}int main(){int cnt = 0, n;scanf("%d",&n);if (n == 1) cnt = 1;while ((n != 0) && (n != 1)) {n = HarshadNumber(n);if (n) ++cnt;}printf("%d",cnt);return 0;}

基思数

#include int arr[8] = {0};int init(int n){int cnt = 0;while (n) {arr[cnt++] = n%10;n /= 10;}return cnt;}void isKeith(int n, int len){int i = len - 1;while (arr[i] < n){int sum = 0;for (int j = 0; j < len; ++j) {sum += arr[(i-j+len)%len];}arr[i] = sum;i = (i-1+len)%len;}if (arr[i] == n) printf("Yes");else printf("No");}int main() {int n;scanf("%d",&n);isKeith(n,init(n));return 0;}

二进制表示

#include #include void binary(int a){bool flag = false;for (int i = 15; i >= 0 ; --i) {if ((a>>i)&1) {if (flag) printf("+");if (i >= 2){printf("2(");binary(i);printf(")");}if (i == 1) printf("2");if (i == 0) printf("2(0)");flag = true;}}}int main() {int a;scanf("%d",&a);binary(a);return 0;}

冰雹数列

#include int main() {int n;scanf("%d",&n);while (n != 1) {printf("%d ",n);if (n&1) n = 3 * n + 1;else n /= 2;}printf("1");return 0;}

稀疏矩阵

#include int main () {int raw, col, n, num = 0;scanf("%d %d", &raw, &col);for (int i = 0; i < raw; ++i) {for (int j = 0; j < col; ++j) {scanf("%d", &n);if (n) ++num;}}double ratio = (double)num / (raw * col);if (num == raw || num == col || (ratio - 0.05) <= 1e-9)printf("Yes\n");else printf("No\n");return 0;}

回文数之和

#include #include int dec[10] = {0}, kSys[32] = {0};bool isPalindrome(int arr[], int cnt){int head = 0, tail = cnt - 1;while (head < tail) {if (arr[head] != arr[tail]) return false;++head, --tail;}return true;}bool isBiPalindrome(int n, int k){int tmp = n, cnt = 0;while (tmp) {dec[cnt++] = tmp % 10;tmp /= 10;}if (!isPalindrome(dec, cnt)) return false;tmp = n, cnt = 0;while (tmp) {kSys[cnt++] = tmp % k;tmp /= k;}if (!isPalindrome(kSys, cnt)) return false;return true;}int main() {int n, k, sum = 0;scanf("%d %d", &n, &k);for (int i = 1; i <= n; ++i) {if (isBiPalindrome(i, k)) sum += i;}printf("%d", sum);return 0;}

前后缀移除

#include #include void strLeftStrip(char *str, char *chars) {int mov = 0;char *pStr = str;while (*pStr) {if (strchr(chars, *pStr)) ++mov;else break;++pStr;}if (mov) memmove(str - mov, str, strlen(str) + 1);}void strRightStrip(char *str, char *chars) {int len = 0;char *pStr = str + strlen(str) - 1;while (pStr >= str) {if (strchr(chars, *pStr)) ++len;else break;--pStr;}*(str + strlen(str) - len) = '\0';}int main() {char str1[1000] = "", chars[1000] = "", str2[1000] = "";scanf("%[^\n] %[^\n]", str1, chars);memcpy(str2, str1, strlen(str1) + 1);strLeftStrip(str1, chars);puts(str1);strRightStrip(str2, chars);puts(str2);strRightStrip(str1, chars);puts(str1);return 0;}

字符串后缀

#include #include void strEndsWith(char *str, char *suffix) {int len = strlen(suffix);char *pStr = str + strlen(str) - len, *pSuffix = suffix;while (*pSuffix && *pStr) {if (*pSuffix != *pStr) {printf("No\n");return;}else ++pSuffix, ++pStr;}printf("Yes\n");}int main() {char str[1000] = "", suffix[1000] = "";scanf("%[^\n] %[^\n]", str, suffix);strEndsWith(str, suffix);return 0;}

Atol转换

#include #include int atol(char *str) {char *pStr = str;int sgn = 1;long long tmp = 0;if (*pStr == '+') ++pStr;else if (*pStr == '-') sgn = -1, ++pStr;while (*pStr) {if (*pStr == ' ') ;else if ('0' <= *pStr && *pStr = INT_MAX) return INT_MAX;else if ((tmp * sgn) <= INT_MIN) return INT_MIN;}else break;++pStr;}return tmp * sgn;}int main() {char str[1000] = "";scanf("%[^\n]", str);printf("%d", atol(str));return 0;}

字符串切片

#include #include int t, n, len;char str[1000];void strSlice(int begin, int end, int step) {char slice[1000] = "";int pos = 0;if (begin < 0) begin += len;if (end = begin && step > 0) {for (int i = begin; i < end; i += step) {slice[pos] = str[i];++pos;}} else if (end < begin && step  end; i += step) {slice[pos] = str[i];++pos;}}puts(slice);}void mode(void) {len = strlen(str);int begin, end, step;switch (n) {case 3: {scanf("%d %d %d", &begin, &end, &step);break;}case 2: {scanf("%d %d", &begin, &end);step = 1;break;}case 1: {scanf("%d", &begin);end = len, step = 1;break;}}strSlice(begin, end, step);}int main() {scanf("%[^\n] %d", str, &t);for (int i = 0; i < t; ++i) {scanf("%d", &n);mode();}return 0;}

分离字符串

#include #include void split(char *str, char *sep) {while (*str) {char *flag = strstr(str, sep);if (flag == NULL) break;char sub[101] = "";int len = flag - str;memcpy(sub, str, len);puts(sub);memmove(str - len - strlen(sep), str, strlen(str) + 1);}puts(str);}int main() {char str[2000] = "", sep[2000] = "";scanf("%[^\n] %[^\n]", str, sep);split(str, sep);return 0;}

Kids A+B

#include #include char ans[30] = "";int strToNum(char *str) {if(strstr(str, "zero")) return 0;if(strstr(str, "ten")) return 10;if(strstr(str, "eleven")) return 11;if(strstr(str, "twelve")) return 12;if(strstr(str, "thirteen")) return 13;if(strstr(str, "fourteen")) return 14;if(strstr(str, "fifteen")) return 15;if(strstr(str, "sixteen")) return 16;if(strstr(str, "seventeen")) return 17;if(strstr(str, "eighteen")) return 18;if(strstr(str, "nineteen")) return 19;int unit = 0, decade = 0;if(strstr(str, "one")) unit = 1;if(strstr(str, "two")) unit = 2;if(strstr(str, "three")) unit = 3;if(strstr(str, "four")) unit = 4;if(strstr(str, "five")) unit = 5;if(strstr(str, "six")) unit = 6;if(strstr(str, "seven")) unit = 7;if(strstr(str, "eight")) unit = 8;if(strstr(str, "nine")) unit = 9;if(strstr(str, "twenty")) decade = 20;if(strstr(str, "thirty")) decade = 30;if(strstr(str, "forty")) decade = 40;if(strstr(str, "fifty")) decade = 50;if(strstr(str, "sixty")) decade = 60;if(strstr(str, "seventy")) decade = 70;if(strstr(str, "eighty")) decade = 80;if(strstr(str, "ninety")) decade = 90;return unit + decade;}void numToStr(int n) {switch (n) {case 0: {strcpy(ans, "zero");return;}case 11: {strcpy(ans, "eleven");return;}case 12: {strcpy(ans, "twelve");return;}case 13: {strcpy(ans, "thirteen");return;}case 14: {strcpy(ans, "fourteen");return;}case 15: {strcpy(ans, "fifteen");return;}case 16: {strcpy(ans, "sixteen");return;}case 17: {strcpy(ans, "seventeen");return;}case 18: {strcpy(ans, "eighteen");return;}case 19: {strcpy(ans, "nineteen");return;}default:break;}int decade = (n / 10) % 10, unit = n % 10;switch (decade) {case 2: {strcpy(ans, "twenty");break;}case 3: {strcpy(ans, "thirty");break;}case 4: {strcpy(ans, "forty");break;}case 5: {strcpy(ans, "fifty");break;}case 6: {strcpy(ans, "sixty");break;}case 7: {strcpy(ans, "seventy");break;}case 8: {strcpy(ans, "eighty");break;}case 9: {strcpy(ans, "ninety");break;}default: {break;}}if (decade && unit) strcat(ans, "-");switch (unit) {case 1: {strcat(ans, "one");break;}case 2: {strcat(ans, "two");break;}case 3: {strcat(ans, "three");break;}case 4: {strcat(ans, "four");break;}case 5: {strcat(ans, "five");break;}case 6: {strcat(ans, "six");break;}case 7: {strcat(ans, "seven");break;}case 8: {strcat(ans, "eight");break;}case 9: {strcat(ans, "nine");break;}default: {break;}}}int main() {char a[30] = "", b[30] = "";scanf("%s %s", a, b);numToStr(strToNum(a) + strToNum(b));puts(ans);return 0;}

时钟A-B

#include #include int main(){struct tm begin = {0}, end = {0};scanf("%d %d %d", &begin.tm_year, &begin.tm_mon, &begin.tm_mday);scanf("%d %d %d", &end.tm_year, &end.tm_mon, &end.tm_mday);begin.tm_year -= 1900, begin.tm_mon -= 1;end.tm_year -= 1900, end.tm_mon -= 1;time_t tmBegin =mktime(&begin);time_t tmEnd = mktime(&end);// Wrong answer when the difference is negativeprintf("%.6lf", difftime(tmBegin, tmEnd));return 0;}

加密字串

#include static int freq[26] = {0};int main() {char plain[8000] = "";int x;scanf("%s %d", plain, &x);for (int i = 0; plain[i]; ++i) ++freq[plain[i] - 'a'];char cipher[8000] = "";for (int i = 0; plain[i]; ++i) {if (freq[plain[i] - 'a'] & 1)cipher[i] = (char) (((plain[i] - 'a' - x) % 26 + 26) % 26 + 'a');elsecipher[i] = (char) ((plain[i] - 'a' + x) % 26 + 'a');}puts(cipher);return 0;}

Arduino显示

#include static const int digit[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};int getUnit(int num) {int cnt = 0;do {cnt += digit[num % 10];num /= 10;} while (num);return cnt;}int main() {int n;scanf("%d", &n);n -= 4;if (n <= 0) printf("0");else {int cnt = 0;for (int i = 0; i <= 1111; ++i) {for (int j = 0; j <= 1111; ++j) {if (getUnit(i) + getUnit(j) + getUnit(i + j) == n) ++cnt;}}printf("%d", cnt);}return 0;}

三元搜索

#include int terSearch(int arr[], int n, int k) {int left = 0, right = n - 1, mid1 = (n - 1) / 3, mid2 = n - mid1;while(mid1 != mid2) {if (k > arr[right] || k < arr[left]) return -1;if (k == arr[mid1]) return mid1;if (k == arr[mid2]) return mid2;if (mid1 == mid2) break;if (k  arr[mid2]) left = mid2 + 1;else left = mid1 + 1, right = mid2 - 1;mid1 = left + (right - left) / 3, mid2 = right - (right - left) / 3;}return -1;}int main() {int n, k;scanf("%d", &n);int arr[n];for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);scanf("%d", &k);printf("%d in [%d]", k, terSearch(arr, n, k));return 0;}

循环排序

#include void swap(int *a, int *b) {int tmp = *a;*a = *b, *b = tmp;}void cycleSort(int arr[], int n) {for (int i = 0; i < n - 1; ++i) {int item = arr[i], pos = i;for (int j = i + 1; j < n; ++j) if (arr[j] < item) ++pos;if (pos == i) continue;swap(&arr[pos], &item);while(pos != i) {pos = i;for (int j = i + 1; j < n; ++j) if (arr[j] < item) ++pos;while (item == arr[pos]) ++pos;swap(&arr[pos], &item);}}}int main() {int n;scanf("%d", &n);int arr[n];for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);cycleSort(arr, n);for (int i = 0; i < n; ++i) printf("%d ", arr[i]);return 0;}

中等题

幂数模

#include typedef unsigned long long uint64;uint64 fastPowerMod (uint64 t, uint64 e, uint64 m){uint64 r = 1;while (e){if (e&1){r = (r*t)%m;}t = (t*t)%m;e >>= 1;}return r;}int main(){uint64 a,b,m,r;scanf("%llu %llu %llu",&a,&b,&m);r = fastPowerMod(a,b,m);printf("%llu",r);return 0;}

倒水

#include #include #define INF 0x3f3f3f3fconst int maxSize = 10000;int distTable[10000][10000];int queue[10000][2] = {0};int bfs(int n, int m, int d){int head = 0, tail = 1;memset(distTable, 0x3f, sizeof(distTable));distTable[0][0] = 0;while(tail != head){int a = queue[head][0], b = queue[head][1];if (a == d || b == d) {return distTable[a][b];}head = (head+1)%maxSize;// fullif(distTable[a][m] == INF) {distTable[a][m] = distTable[a][b]+1;tail = (tail+1)%maxSize;queue[tail][0] = a;queue[tail][1] = m;}if(distTable[n][b] == INF) {distTable[n][b] = distTable[a][b]+1;tail = (tail+1)%maxSize;queue[tail][0] = n;queue[tail][1] = b;}// emptyif(distTable[a][0] == INF) {distTable[a][0] = distTable[a][b]+1;tail = (tail+1)%maxSize;queue[tail][0] = a;queue[tail][1] = 0;}if(distTable[0][b] == INF) {distTable[0][b] = distTable[a][b]+1;tail = (tail+1)%maxSize;queue[tail][0] = 0;queue[tail][1] = b;}// pourif(a+b = n && distTable[n][a+b-n] == INF){distTable[n][a+b-n] = distTable[a][b]+1;tail = (tail+1)%maxSize;queue[tail][0] = n;queue[tail][1] = a+b-n;}if(a+b = m && distTable[a+b-m][m] == INF){distTable[a+b-m][m] = distTable[a][b]+1;tail = (tail+1)%maxSize;queue[tail][0] = a+b-m;queue[tail][1] = m;}}return -1;}int main(){int n,m,d;scanf("%d %d %d",&n,&m,&d);printf("%d", bfs(n,m,d));return 0;}

好数字

#include typedef unsigned long long uint64;const uint64 mod = 1e9+7;uint64 power(uint64 a, uint64 e){uint64 r = 1;while (e){if (e&1){r = (r*a)%mod;}a = (a*a)%mod;e >>= 1;}return r;}int main(){uint64 n, num;scanf("%llu",&n);num = power(4,n/2)*power(5,n-n/2)%mod;printf("%llu",num);return 0;}

阶乘倍数

#include #include  typedef unsigned long long uint64;uint64 primeFactNum = 0;uint64 prime[20] = {0}, num[20] = {0};bool isMulti(uint64 n){uint64 primeNum, tmp;for (uint64 i = 1; i <= primeFactNum; ++i) {primeNum = 0;tmp = n;while (tmp) {primeNum += tmp/prime[i];tmp /= prime[i];}if(primeNum < num[i]) {return false;}}return true;}void solveFact(uint64 k){for (uint64 i = 2; i*i  1){++primeFactNum;prime[primeFactNum] = k;++num[primeFactNum];}} int main(){uint64 left = 1, right = 1e19, mid, n, k;scanf("%lld",&k);solveFact(k); while(left >1)+left;if (isMulti(mid)){right = mid-1;n = mid;} else {left = mid+1;}}printf("%lld",n);return 0;}

方案数

#include int main(){int cnt = 0, n;scanf("%d",&n);for (int i = 1; i*(i+1) <= 2*n; ++i) {if ((n-i*(i-1)/2)%i == 0){++cnt;}}printf("%d",cnt);return 0;}

素数

#include #include #include typedef unsigned long long uint64;uint64 primeNum(uint64 a, uint64 b){bool isPrime[b+1];memset(isPrime,1,b+1);uint64 cnt = 0;for (uint64 i = 2; i <= b; ++i){if (isPrime[i]){for (uint64 j = 2; j*i <= b; ++j){isPrime[j*i] = false;}}}for (uint64 i = a; i <= b; ++i){cnt += isPrime[i];}return cnt;}int main(){uint64 a,b,num;scanf("%llu %llu",&a,&b);num = primeNum(a,b);printf("%llu",num);return 0;}

光线追踪

#include unsigned int gcd(unsigned int a, unsigned int b){if (b == 0) return a;return gcd(b,a%b);}int main(){unsigned int n,x,l;scanf("%u %u",&n,&x);l = 3*(n-gcd(n,x));printf("%u",l);return 0;}

运动会

#include #include int phiEuler(int n){int phi[n+1],prime[n+1];bool isSieved[n+1];int sum = 0,cnt = 1, comp;prime[0] = 1;phi[1] = 1;for (int i = 2; i < n; ++i){if (!isSieved[i]){prime[cnt++] = i;phi[i] = i-1;}for (int j = 1; i*prime[j] <= n; ++j){comp = i*prime[j];isSieved[comp] = true;if (i%prime[j] == 0){phi[comp] = prime[j]*phi[i];break;} else{phi[comp] = (prime[j]-1)*phi[i];}}}for (int i = 1; i <= n-1; ++i) {sum += phi[i];}return sum;}int main() {int n, num;scanf("%d",&n);num = n == 1 ? 0 : (2*phiEuler(n)+1);printf("%d",num);return 0;}

行列式值

Guass消去

#include #define MAXSIZE 10void swap(double arr[MAXSIZE][MAXSIZE], int r1, int r2, int n) {for (int i = 0; i < n; i++) {double temp = arr[r1][i];arr[r1][i] = arr[r2][i];arr[r2][i] = temp;}}double calDet(double arr[MAXSIZE][MAXSIZE], int n) {int i, j, k;double det = 1.0;for (i = 0; i < n; i++) {if (arr[i][i] == 0.0) {for (j = i + 1; j < n; j++) {if (arr[j][i] != 0.0) {swap(arr, i, j, n);det *= -1.0;break;}}}if (arr[i][i] == 0.0) {return 0.0;}double v = arr[i][i];det *= v;for (j = i; j < n; j++) {arr[i][j] /= v;}for (j = i + 1; j < n; j++) {double e = arr[j][i];for (k = i; k < n; k++) {arr[j][k] -= e * arr[i][k];}}}return det;}int main() {int n;scanf("%d", &n);double arr[MAXSIZE][MAXSIZE];for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {scanf("%lf", &arr[i][j]);}}double det = calDet(arr, n);printf("%.0lf\n", det);return 0;}

按第1行展开(递归)

#include #include int** init(int n) {int** matrix = (int**)malloc(sizeof(int*) * (n + 1));for(int i = 0; i <= n; i++) {matrix[i] = (int*)malloc(sizeof(int) * (n + 1));}return matrix;}int det(int **matrix, int n) {if (n == 1) return matrix[1][1];int sum = 0;int **subMatrix = init(n - 1);for (int i = 1; i <= n; ++i) {for (int j = 1; j <= n - 1; ++j) {for (int k = 1; k <= n - 1; ++k) {if (k < i) subMatrix[j][k] = matrix[j + 1][k];else subMatrix[j][k] = matrix[j + 1][k + 1];}}int sgn = i % 2 == 0 ? -1 : 1;sum += sgn * matrix[1][i] * det(subMatrix, n - 1);}return sum;}int main() {int n;scanf("%d", &n);int **matrix = init(n);for (int i = 1; i <= n; ++i) {for (int j = 1; j <= n; ++j) {scanf("%d", &matrix[i][j]);}}printf("%d", det(matrix, n));return 0;}

货运优化

#include int main() {int l3s2[4] = {0, 5, 3, 1};int n, x1, x2, x3, x4, x5, x6, s2, s1;while (1) {scanf("%d %d %d %d %d %d", &x1, &x2, &x3, &x4, &x5, &x6);if ((x1 + x2 + x3 + x4 + x5 + x6) == 0) break;n = (x3 + 3) / 4 + x4 + x5 + x6;s2 = 5 * x4 + l3s2[x3 % 4];if (x2 > s2) n += (x2 - s2 + 8) / 9;s1 = 36 * n - 36 * x6 - 25 * x5 - 16 * x4 - 9 * x3 - 4 * x2;if (x1 > s1) n += (x1 - s1 + 35) / 36;printf("%d\n",n);}return 0;}

素数筛法

#include #include #define NUM (int)1e7+1static bool isSieved[NUM];static int prime[NUM];int main() {int n, k = 0;scanf("%d", &n);isSieved[1] = true;for (int i = 2; i <= n; ++i) {if (!isSieved[i]) prime[++k] = i;for (int j = 1; prime[j] * i <= n; ++j) {isSieved[prime[j] * i] = true;if (i % prime[j] == 0) break;}}printf("%d", k);}

完美矩阵

#include #include #define MAXSIZE 301int arr[MAXSIZE][MAXSIZE] = {0};int preSum[MAXSIZE][MAXSIZE] = {0};void prefix(int n, int m){for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {preSum[i][j] = preSum[i - 1][j] + preSum[i][j - 1] - preSum[i - 1][j - 1] + arr[i][j];}}}int getSum(int x1, int x2, int y1, int y2) {return preSum[x2][y2] - preSum[x1 - 1][y2] - preSum[x2][y1 - 1] + preSum[x1 - 1][y1 - 1];}bool isPerfect(int x1, int x2, int y1, int y2) {int outer = getSum(x1, x2, y1, y2), inner;int len = 2 * (x2 - x1 + y2 - y1);if ((x2 - x1) == 1 || (y2 - y1) == 1) inner = 0;else inner = getSum(x1 + 1, x2 - 1, y1 + 1, y2 - 1);if (inner != 1 && inner != 0 && inner != -1) return false;if ((outer - inner) != len) return false;return true;}int perfectNum(int n, int m) {int cnt = 0;for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {for (int k = 1; k + i <= n && k + j <= m; ++k) {if (arr[i][k + j] == -1 || arr[k + i][j] == -1) break;if (isPerfect(i, i + k, j, j + k)) {++cnt;}}}}return cnt;}int main () {int n, m;scanf("%d %d", &n, &m);for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {scanf("%d", &arr[i][j]);if (arr[i][j] == 0) arr[i][j] = -1;}}prefix(n ,m);printf("%d", perfectNum(n, m));return 0;}

删除前后缀

#include #include void strRemovePrefix(char *str, char *prefix) {int cnt = 0;char *pStr = str, *pPrefix = prefix;while (*pPrefix && *pStr && *pStr == *pPrefix) {while (*pPrefix && *pStr && *pStr == *pPrefix)++pStr, ++pPrefix, ++cnt;pPrefix = prefix;}int len = strlen(prefix);int mov = (cnt / len) * len;if (mov) memmove(str - mov, str, strlen(str) + 1);}void strRemoveSuffix(char *str, char *suffix) {int len = strlen(suffix);char *pStr = str + strlen(str) - len, *pSuffix = suffix;while (*pSuffix && pStr >= str && *pStr == *pSuffix) {int cnt = 0;while (*pSuffix && pStr && *pStr == *pSuffix)++pStr, ++pSuffix, ++cnt;if (cnt == len) {pSuffix = suffix, pStr = pStr - 2 * len;*(pStr + len) = '\0';} else break;}}int main() {char str1[1000] = "", fix[1000] = "", str2[1000] = "";scanf("%[^\n] %[^\n]", str1, fix);memcpy(str2, str1, strlen(str1) + 1);strRemovePrefix(str1, fix);puts(str1);strRemoveSuffix(str2, fix);puts(str2);return 0;}

元宇宙A+B

#include #include const static char decToMeta[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";static char c[100] = "", a[100] = "", b[100] = "";static int C[100] = {0}, A[100] = {0}, B[100] = {0};int metaToDec(char m) {if ('0' <= m && m <= '9') return m - '0';return m - 'A' + 10;}void add(void) {int lenA = strlen(a), lenB = strlen(b);for (int i = 0; i < lenA; ++i) A[i] = metaToDec(a[lenA - i - 1]);for (int i = 0; i  lenB ? lenA : lenB;for (int i = 0; i = 0; --i) c[i] = decToMeta[C[lenC - i - 1]];c[lenC] = '\0';}int main() {scanf("%s %s", a, b);add();puts(c);return 0;}

有效表达式

#include int main() {long long n;scanf("%lld", &n);long long cnt = 1;for (long long i = n + 2; i <= 2 * n; ++i) cnt *= i;for (long long i = 1; i <= n; ++i) cnt /= i;printf("%lld", cnt);return 0;}

长安

#include int bx, by, px, py, cnt;void dfs(int x, int y) {if ((x == px && y == py) || x > bx || y > by) return;if (x == bx && y == by) {++cnt;return;}dfs(x + 1, y);dfs(x, y + 1);}int main() {while (1) {fflush(stdin);scanf("%d %d %d %d", &bx, &by, &px, &py);if (bx <= 0 || by <= 0 || px <= 0 || py <= 0) break;cnt = 0;dfs(1, 1);printf("%d\n", cnt);}return 0;}