2023西工大NOJ (C语言版) 持续更新ing

这篇具有很好参考价值的文章主要介绍了2023西工大NOJ (C语言版) 持续更新ing。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

代码已同步至 gitee:2023NOJ(C语言版)!

题目过于垃圾和睿智,做这种题简直是浪费生命,甚至会让代码水平剧烈下降,也就81-90题值得做一下,有这功夫不如多打会儿游戏。
贴出 100 题中全部 AC 代码,并不保证代码正确性、可读性、简洁性、最佳性,只能保证AC;目前 4 道题 WA,还有 5 道题使用 CPP。具体情况如下:

  • 41-50:飞机起飞速度(WA);
  • 51-60:字符串替换(WA);
  • 61-70:GPS通信协议(CPP);
  • 71-80:卫星定位(WA),日出日落时间(WA),火箭发射模拟(CPP),晶体结构(CPP),原子计数(CPP);
  • 81-90:危险的组合(CPP)。

考试模板

作者考试时会携带的资料如下:

// 字符串操作函数(常用)
// 为避免内存泄漏和野指针问题, 通常只能对开在栈上字符串使用, 而不能对开在堆上的字符串使用
# include <string.h>
// 失败时均返回NULL
void *memset(void *str, int c, size_t n); // 复制字符c到str的前n个字符.
// 使用memset对数组初始化时, 字符c只能为0或-1或0x3f.
void *memcpy(void *dest, const void *src, size_t n); // 从src复制n个字符到dest, 一般不用于src和dest内存重叠的情况.
void *memmove(void *dest, const void *src, size_t n); // 从src复制n个字符到dest, src和dest内存重叠时推荐使用.
size_t strlen(const char *str); // 返回str长度, 即头指针到'\0'的距离(但不包含'\0').
char *strcat(char *dest, const char *src); // 将src追加到dest结尾.
char *strchr(const char *str, int c); // 返回str中第一次出现字符c的位置.
char *strstr(const char *haystack, const char *needle); // 返回在haystack中第一次出现needle的起始位置.
size_t strspn(const char *str1, const char *str2); // str1中第一个不在str2出现的字符的下标
char *strtok(char *str, const char *delim); // 以delim为分隔, 分解str.
// 字符串操作函数(常用)
// 为避免内存泄漏和野指针问题, 通常只能对开在栈上字符串使用, 而不能对开在堆上的字符串使用
# include <string.h>
// 失败时均返回NULL
void *memset(void *str, int c, size_t n); // 复制字符c到str的前n个字符.
// 使用memset对数组初始化时, 字符c只能为0或-1或0x3f.
void *memcpy(void *dest, const void *src, size_t n); // 从src复制n个字符到dest, 一般不用于src和dest内存重叠的情况.
void *memmove(void *dest, const void *src, size_t n); // 从src复制n个字符到dest, src和dest内存重叠时推荐使用.
size_t strlen(const char *str); // 返回str长度, 即头指针到'\0'的距离(但不包含'\0').
char *strcat(char *dest, const char *src); // 将src追加到dest结尾.
char *strchr(const char *str, int c); // 返回str中第一次出现字符c的位置.
char *strstr(const char *haystack, const char *needle); // 返回在haystack中第一次出现needle的起始位置.
size_t strspn(const char *str1, const char *str2); // str1中第一个不在str2出现的字符的下标
char *strtok(char *str, const char *delim); // 以delim为分隔, 分解str.
// 指针数组模板
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char id[64];
    int a;
    int b;
} PointerArray;

int main(){
    int n; scanf("%d", &n);
    PointerArray *arr[n];

    for (int i = 0; i < n; ++i) {
        arr[i] = (PointerArray*) calloc (sizeof(PointerArray), 1);
        scanf("%s %d %d", arr[i]->id, &arr[i]->a, &arr[i]->b);
    }

    for (int i = 0; i < n; ++i)
        printf("%s %d %d", arr[i]->id, arr[i]->a, arr[i]->b);
    return 0;
}
// 二维数组模板
#include <stdio.h>
#include <stdlib.h>

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 op(int **matrix, int n);

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", op(matrix, n));
    return 0;
}
// 快速排序模板: 从小到大
void quickSort(int arr[], int left, int right) {
	if (left >= right) return;
	int flag = arr[(left + right) / 2];
	int head = left - 1, tail = right + 1;
	while (head < tail) {
		do head++; while (arr[head] < flag); // 若从大到小需修改本行
		do tail--; while (arr[tail] > flag); // 若从大到小需修改本行
		if (head < tail) {
			int tmp = arr[head];
			arr[head] = arr[tail], arr[tail] = tmp;
		}
	}
	quickSort(arr, left, tail);
	quickSort(arr, tail + 1, right);
}
// 冒泡排序模板: 从小到大
#include <stdbool.h>

void bubble(int arr[], int n) {
    for (int i = 0; i < n - 1; ++i) {
        bool flag = false;
        for (int j = 0; j < n - i - 1; ++j) {
            if (arr[j] > arr[j + 1]) { // 若从大到小需修改本行
                int tmp = arr[j];
                arr[j] = arr[j + 1], arr[j + 1] = tmp, flag = true;
            }
        }
        if (!flag) break;
    }
}
// 二分查找模板
long long left = 1, right = 1e19, mid, ans;

while(left <= right) {
    mid = ((right - left) >> 1) + left;
    if (isLeft(mid)) right = mid - 1, ans = mid;
    else left = mid + 1;
}
// 文件读写模板
#include <stdio.h>
#include <stdlib.h>

int main(){
    int n; scanf("%d",&n);

    FILE *file = fopen("rr.dat", "w");
    for(int i=1;i<=n;i++)
        fprintf(file, "%d\n", i);
    fclose(file);

    file = fopen("rr.dat", "r");
    while (fscanf(file, "%d", &ans) == 1)
        printf("%d ", ans);
    fclose(file);
}

1-10

Hello World

#include<stdio.h>

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

A+B

#include<stdio.h>

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

数据类型大小及范围

#include <stdio.h>
#include <limits.h>

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

平均值

#include <stdio.h>

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

进制转换

#include <stdio.h>

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

浮点数输出

#include <stdio.h>

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

动态宽度输出

#include <stdio.h>

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 <stdio.h>
#include <math.h>
#define RADIUS 6371.000000
#define PI 3.1415926

int 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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <stdio.h>
#include <math.h>

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 <= c ? b : c);
}

int main()
{
    int R, G, B;
    double r, g, b;
    double max, min;
    double H, S, V;

    scanf("%d %d %d",&R,&G,&B);

    r = (double)((R > 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;
}

11-20

乘数模

#include<stdio.h>

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;
}

方阵

#include<stdio.h>

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

    return 0;
}

分数的加、减、乘、除法

#include<stdio.h>

int gcd(int a,int b){
    if (b == 0) return a;
    else return gcd(b,a%b);
}

int main(){
    int aTop, aBot, bTop, bBot;
    scanf("%d",&aTop);
    getchar();
    scanf("%d",&aBot);
    getchar();
    scanf("%d",&bTop);
    getchar();
    scanf("%d",&bBot);
    getchar();

    int addTop, addBot, subTop, subBot, multTop, multBot, divTop, divBot;
    int addGcd, subGcd, multGcd, divGcd;

    addTop = aTop*bBot+aBot*bTop;
    addBot = aBot*bBot;
    addGcd = gcd(addTop,addBot);
    addTop /= addGcd;
    addBot /= addGcd;


    subTop = aTop*bBot-aBot*bTop;
    subBot = aBot*bBot;
    subGcd = gcd(subTop,subBot);
    subTop /= subGcd;
    subBot /= subGcd;

    multTop = aTop*bTop;
    multBot = aBot*bBot;
    multGcd = gcd(multTop,multBot);
    multTop /= multGcd;
    multBot /= multGcd;

    divTop = aTop*bBot;
    divBot = aBot*bTop;
    divGcd = gcd(divTop,divBot);
    divTop /= divGcd;
    divBot /= divGcd;

    printf("(%d/%d)+(%d/%d)=%d/%d\n",aTop,aBot,bTop,bBot,addTop,addBot);
    printf("(%d/%d)-(%d/%d)=%d/%d\n",aTop,aBot,bTop,bBot,subTop,subBot);
    printf("(%d/%d)*(%d/%d)=%d/%d\n",aTop,aBot,bTop,bBot,multTop,multBot);
    printf("(%d/%d)/(%d/%d)=%d/%d\n",aTop,aBot,bTop,bBot,divTop,divBot);

    return 0;
}

操作数

#include<stdio.h>

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<stdio.h>

int 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<stdio.h>
#include<math.h>

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<stdio.h>
#include<math.h>

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 <stdio.h>

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<stdio.h>

typedef unsigned long long uint64;

int fastPowerMod (uint64 t, uint64 e, uint64 m){
    uint64 r = 1;
    while (e){
        if (e&1){
            r = (1LL*r*t)%m;
        }
        t = (1LL*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 <stdio.h>

int main(){
    unsigned int t = 0;
    unsigned int arrn[100000];
    scanf("%u", &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;
}

21-30

余数和

#include <stdio.h>

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 <stdio.h>
#include <stdbool.h>

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;
}

倒水(原 AC 后 TE 现又 AC)

#include <stdio.h>
#include <string.h>

#define INF 0x3f3f3f3f

const int maxSize = 10000;
int distTable[10000][10000];
int queue[2][10000] = {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[0][head], b = queue[1][head];
        if (a == d || b == d) {
            return distTable[a][b];
        }

        ++head;
        if(head == maxSize-1) {
            head = 0;
        }

        // full
        if(distTable[a][m] == INF) {
            distTable[a][m] = distTable[a][b]+1;
            ++tail;
            queue[0][tail] = a;
            queue[1][tail] = m;
        }
        if(distTable[n][b] == INF) {
            distTable[n][b] = distTable[a][b]+1;
            ++tail;
            queue[0][tail] = n;
            queue[1][tail] = b;
        }

        // empty
        if(distTable[a][0] == INF) {
            distTable[a][0] = distTable[a][b]+1;
            ++tail;
            queue[0][tail] = a;
            queue[1][tail] = 0;
        }
        if(distTable[0][b] == INF) {
            distTable[0][b] = distTable[a][b]+1;
            ++tail;
            queue[0][tail] = 0;
            queue[1][tail] = b;
        }

        // pour
        if(a+b <= n && distTable[a+b][0] == INF){
            distTable[a+b][0] = distTable[a][b]+1;
            ++tail;
            queue[0][tail] = a+b;
            queue[1][tail] = 0;
        }
        if(a+b > n && distTable[n][a+b-n] == INF){
            distTable[n][a+b-n] = distTable[a][b]+1;
            ++tail;
            queue[0][tail] = n;
            queue[1][tail] = a+b-n;
        }
        if(a+b <= m && distTable[0][a+b] == INF){
            distTable[0][a+b] = distTable[a][b]+1;
            ++tail;
            queue[0][tail] = 0;
            queue[1][tail] = a+b;
        }
        if(a+b > m && distTable[a+b-m][m] == INF){
            distTable[a+b-m][m] = distTable[a][b]+1;
            ++tail;
            queue[0][tail] = a+b-m;
            queue[1][tail] = m;
        }

        if (tail == maxSize) {
            tail = 0;
        }
    }

    return -1;
}

int main(){
    int n,m,d;
    scanf("%d %d %d",&n,&m,&d);
    printf("%d", bfs(n,m,d));
    return 0;
}

好数字

#include <stdio.h>

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 <stdio.h>

unsigned long long pythagoras(unsigned int sum){
    // a as short catheti, b as long catheti, c as hypotenuse
    unsigned a,b,c;
    for (a = 1; a <= sum/4; ++a){
        for (b = a+1; b <= sum/2; ++b){
            c = sum-a-b;
            if ((a+b > c) & (c-a < b) & (a*a+b*b == c*c)){
                return a*b*c;
            }
        }
    }
}

int main(){
    unsigned int n;
    scanf("%u",&n);
    printf("%d", pythagoras(n));
    return 0;
}

竖式乘法

#include <stdio.h>
#include <math.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>
#include <stdbool.h>
 
typedef unsigned long long int64;
 
int64 primeFactNum = 0;
int64 prime[20] = {0}, num[20] = {0};

bool isMulti(int64 n){
    int64 primeNum, tmp;
    for (int64 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(int64 k){
    for (int64 i = 2; i*i <= k; ++i) {
        if(k%i == 0){
            ++primeFactNum;
            prime[primeFactNum] = i;
            while (k%i == 0){
                ++num[primeFactNum];
                k /= i;
            }
        }
    }
    if (k > 1){
        ++primeFactNum;
        prime[primeFactNum] = k;
        ++num[primeFactNum];
    }
}
 
int main(){
    int64 left = 1, right = 1e19, mid, n, k;
    scanf("%lld",&k);
    solveFact(k);
 
    while(left <= right){
        mid = ((right-left)>>1)+left;
        if (isMulti(mid)){
            right = mid-1;
            n = mid;
        } else {
            left = mid+1;
        }
    }
    printf("%lld",n);
    return 0;
}

方案数

#include <stdio.h>

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;
}

31-40

哈沙德数

#include <stdio.h>

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 <stdio.h>
#include <stdbool.h>
#include <string.h>

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 <stdio.h>

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 <stdio.h>
#include <stdbool.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>
#include <stdarg.h>

int sum(int start,...){
    va_list vaList;
    int sum = 0;
    int curr = start;
    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 <stdio.h>
#include <stdbool.h>

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;
}

可变参数平均

#include <stdio.h>
#include <stdarg.h>

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;
}

41-50

航空旅行

#include<stdio.h>
#include<stdbool.h>

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 <stdio.h>
#include <math.h>
#include <stdlib.h>

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;
}

飞机起飞速度(WA)

稀疏矩阵

#include <stdio.h>

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 <stdio.h>
#include <stdbool.h>

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 <stdio.h>
#include <stdbool.h>

#define MAXSIZE 301

int 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] == 0 || arr[k + i][j] == 0) 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 <stdio.h>

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 <stdio.h>

#define MAX_SIZE 10

void swapRows(double matrix[MAX_SIZE][MAX_SIZE], int row1, int row2, int n) {
    for (int i = 0; i < n; i++) {
        double temp = matrix[row1][i];
        matrix[row1][i] = matrix[row2][i];
        matrix[row2][i] = temp;
    }
}

double calculateDeterminant(double matrix[MAX_SIZE][MAX_SIZE], int n) {
    int i, j, k;
    double determinant = 1.0;

    for (i = 0; i < n; i++) {
        if (matrix[i][i] == 0.0) {
            for (j = i + 1; j < n; j++) {
                if (matrix[j][i] != 0.0) {
                    swapRows(matrix, i, j, n);
                    determinant *= -1.0;
                    break;
                }
            }
        }

        if (matrix[i][i] == 0.0) {
            return 0.0;
        }

        double pivot = matrix[i][i];
        determinant *= pivot;
        for (j = i; j < n; j++) {
            matrix[i][j] /= pivot;
        }

        for (j = i + 1; j < n; j++) {
            double factor = matrix[j][i];
            for (k = i; k < n; k++) {
                matrix[j][k] -= factor * matrix[i][k];
            }
        }
    }

    return determinant;
}

int main() {
    int n;
    scanf("%d", &n);

    double matrix[MAX_SIZE][MAX_SIZE];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%lf", &matrix[i][j]);
        }
    }

    double determinant = calculateDeterminant(matrix, n);
    printf("%.0lf\n", determinant);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

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 <stdio.h>

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 <stdio.h>
#include <stdbool.h>

#define NUM (int)1e7+1

static 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);
}

51-60

字符串替换(WA)

删除前后缀

#include <stdio.h>
#include <string.h>

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;
}

大小写交换

#include <stdio.h>

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 input[1000] = "";
    scanf("%[^\n]",input);
    strSwapCase(input);
    puts(input);
    return 0;
}

前后缀移除

#include <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <limits.h>

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 <= '9') {
            tmp = (*pStr - '0') + tmp * 10;
            if ((tmp * sgn) >= 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;
}

元宇宙A+B

#include <stdio.h>
#include <string.h>

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; ++i) B[i] = metaToDec(b[lenB - i - 1]);

    int carry = 0;
    int lenC = lenA > lenB ? lenA : lenB;
    for (int i = 0; i < lenC; ++i) {
        C[i] = A[i] + B[i] + carry;
        carry = C[i] / 36;
        C[i] %= 36;
    }
    if (carry != 0) {
        C[lenC] = carry;
        ++lenC;
    }

    for (int i = lenC - 1; 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 <stdio.h>
#include <string.h>

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 < 0) end += len;
    if (end >= begin && step > 0) {
        for (int i = begin; i < end; i += step) {
            slice[pos] = str[i];
            ++pos;
        }
    } else if (end < begin && step < 0) {
        for (int i = begin; i > 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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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");
            char *p = ans;
            return;
        }
        case 11: {
            strcpy(ans, "eleven");
            char *p = ans;
            return;
        }
        case 12: {
            strcpy(ans, "twelve");
            char *p = ans;
            return;
        }
        case 13: {
            strcpy(ans, "thirteen");
            char *p = ans;
            return;
        }
        case 14: {
            strcpy(ans, "fourteen");
            char *p = ans;
            return;
        }
        case 15: {
            strcpy(ans, "fifteen");
            char *p = ans;
            return;
        }
        case 16: {
            strcpy(ans, "sixteen");
            char *p = ans;
            return;
        }
        case 17: {
            strcpy(ans, "seventeen");
            char *p = ans;
            return;
        }
        case 18: {
            strcpy(ans, "eighteen");
            char *p = ans;
            return;
        }
        case 19: {
            strcpy(ans, "nineteen");
            char *p = ans;
            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;
}

61-70

时钟A-B

#include <stdio.h>
#include <time.h>

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);
	printf("%.6lf", difftime(tmBegin, tmEnd));
	return 0;
}

加密字串

#include <stdio.h>

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');
		else
			cipher[i] = (char) ((plain[i] - 'a' + x) % 26 + 'a');
	}
	puts(cipher);
	return 0;
}

Arduino显示

#include <stdio.h>

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 <stdio.h>

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 <stdio.h>

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;
}

GPS通信协议(CPP)

感谢 Sekiro_2 提供的代码~

#include <bits/stdc++.h>

using namespace std;

string out[100];
int k=0;

int check(string str){
    int i,result;

    for(result=str[1],i=2;str[i]!='*';i++)
    {
        result^=str[i];
    }
    return result;
}

int convert(string str){
    int res=0;
    res=stoi(str,0,16);
    return res;
}


void convert_BeingTime(string utcTime){
    int  hour=stoi(utcTime.substr(0,2));
    int  B_hour=(hour+8)%24;
    if(B_hour/10==0)
        out[k++]="0"+to_string(B_hour)+":"+utcTime.substr(2,2)+":"+utcTime.substr(4,2);
    else
        out[k++]=to_string(B_hour)+":"+utcTime.substr(2,2)+":"+utcTime.substr(4,2);

}

int main(){
    string str;
    while(cin>>str){
        if(str=="END") break;
        if(str.compare(0,6,"$GPRMC")==0){
            size_t asteriskPos = str.find('*');
            if(asteriskPos!=string::npos){
                int checksum=check(str);
                
                int senchecksum=convert(str.substr(asteriskPos + 1, 2));
                if(checksum!=senchecksum) {
                    out[k++]="error";
          
                }
                else{
                    string utcTime = str.substr(7, 6);
     
                    convert_BeingTime(utcTime);
                }
            }
        }

    }
    for(int i=0;i<k;i++){
        cout<<out[i]<<endl;
    }
}

三元搜索

#include <stdio.h>

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[mid1]) right = mid1 - 1;
		else 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;
}

DNA双螺旋结构

#include <stdio.h>

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 <stdio.h>

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 <stdio.h>

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;
}

71-80

卫星定位(WA)

热能计算

#include <stdio.h>

int main() {
	int Ti, Tf, cL, cV;
	double mL, mV;
	scanf("%d %d %lf %d %lf %d", &Ti, &Tf, &mL, &cL, &mV, &cV);
	double QL = cL * mL * (Tf - Ti);
	double QV = cV * mV * (Tf - Ti);
	double Q = QL + QV;
	printf("%.2lfkJ,%.2lf%%,%.2lf%%\n", Q / 1000, QV / Q, QL / Q);
	return 0;
}

/*
20 80
0.250 4186
0.500 900
*/

几何约束

#include <stdio.h>
#include <stdbool.h>

int maxi(int a, int b) {
	return a > b ? a : b;
}

int mini(int a, int b) {
	return a < b ? a : b;
}

int cross(int x1, int y1, int x2, int y2) {
	return x1 * y2 - y1 * x2;
}

bool insert(int line1[4], int line2[4]) {
	if (maxi(line2[0], line2[2]) < mini(line1[0], line1[2]) ||
	maxi(line1[0],line1[2]) < mini(line2[0], line2[2]) ||
	maxi(line2[1], line2[3]) < mini(line1[1], line1[3]) ||
	 maxi(line1[1], line2[3]) < mini(line2[1],line2[3])) return false;
	if (cross(line1[2] - line1[0], line1[3] - line1[1], line2[0] - line1[0], line2[1] - line1[1]) *
		cross(line1[2] - line1[0], line1[3] - line1[1], line2[2] - line1[0], line2[3] - line1[1]) > 0 ||
		cross(line2[2] - line2[0], line2[3] - line2[1], line1[0] - line2[0], line1[1] - line2[1]) *
			cross(line2[2] - line2[0], line2[3] - line2[1], line1[2] - line2[0], line1[3] - line2[1]) > 0) return false;
	return true;
}

int main() {
	int n;
	scanf("%d", &n);
	int line[n][4];
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < 4; ++j) scanf("%d", &line[i][j]);

	int cnt = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = i + 1; j < n; ++j) {
			if (insert(line[i], line[j])) {
				printf("X: #%d #%d\n", i + 1, j + 1);
				++cnt;
			}
		}
	}
	printf("n=%d\n", cnt);
	return 0;
}

/*
5
1 5 4 5
2 5 10 1
3 2 10 3
6 4 9 4
7 1 8 1
*/

日出日落时间(WA)

中位数

#include <stdio.h>

int arr[1000];

double mid(int n) {
	if (n & 1) return arr[n / 2];
	return (arr[n / 2] + arr[n / 2 - 1]) / 2.0f;
}

int main() {
	int flag, cnt = 0;
	while(1) {
		scanf("%d", &flag);
		if (flag == -1) break;
		while (1) {
			if (flag == 0) {
				for (int i = 0; i < cnt; ++i) printf("%d ", arr[i]);
				printf("%.6lf\n", mid(cnt));
				break;
			}
			arr[cnt++] = flag;
			scanf("%d", &flag);
		}
	}
	return 0;
}

原子计数(CPP)

感谢 Cubeist 提供的代码~

#include <iostream>
#include <map>
using namespace std;

string s;

int getnum(int x)
{
    int res = 0;
    for (; s[x]>='0' && s[x]<='9' && s[x]; x ++)
        res = res * 10 + s[x] - '0';
    return res + !res;
}

int main()
{
    getline(cin, s);
    map<string, int> mp;
    for (int i = 0; s[i]; i ++)
    {
        if (!(s[i]>='A' && s[i]<='Z')) continue;
        string ele = "";
        ele += s[i];
        
        if (s[i+1]>='a' && s[i+1]<='z')
        {
            ele += s[i+1];
            mp[ele] += getnum(i+2);
        }
        else 
            mp[ele] += getnum(i+1);
    }

    for (auto& p : mp)
        cout << p.first << " " << p.second << endl;
        
    return 0;
}

成绩单

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct tag {
	long long id;
	char name[31];
	int score;
} ST;

void load(ST* arr[], int n) {
	for (int i = 0; i < n; ++i) {
		arr[i] = (ST*) malloc(sizeof(ST));
		scanf("%lld %s %d", &arr[i]->id, arr[i]->name, &arr[i]->score);
	}
}

void sort(ST* arr[], int n) {
	ST* tmp = NULL;
	for (int i = 0; i < n; ++i) {
		bool isSwapped = false;
		for (int j = 0; j < n - 1 - i; ++j)
			if ((arr[j]->score < arr[j + 1]->score) ||
				(arr[j]->score == arr[j + 1]->score && arr[j]->id > arr[j + 1]->id))
				tmp = arr[j], arr[j] = arr[j + 1], arr[j + 1] = tmp, isSwapped = true;
		if (!isSwapped) break;
	}
}

void traverse(ST* arr[], int n) {
	for (int i = 0; i < n; ++i)
		printf("%lld %s %d\n", arr[i]->id, arr[i]->name, arr[i]->score);
}

int main() {
	int n;
	scanf("%d", &n);
	ST* grade[n];
	load(grade, n);
	sort(grade, n);
	traverse(grade, n);
	return 0;
}

/*
6
2001900001 Jerry 88
2001900005 Tom 92
2001900006 Milla 85
2001900002 Alice 80
2001900003 Mickey 85
2001900004 Aladdin 83
*/

水下声学定位

#include<stdio.h>
#include<math.h>

int main()
{
	const double pi = 3.1415926f;
	double a, b, c, d, diag;
	scanf("%lf %lf %lf %lf %lf", &a, &b, &c, &d, &diag);
	double p = (a + b + diag) / 2, q = (c + d + diag);
	double s = sqrt(p * (p - a) * (p - b) * (p - diag)) + sqrt(q * (q - c) * (q - d) * (q - diag));
	double angle = atan((4 * s) / (pow(b, 2) + pow(d, 2) - pow(a, 2) - pow(c, 2)));
	angle *= 180 / pi;
	printf("%.6lf %.1lf", s, angle);
}

火箭发射模拟(CPP)

感谢 De1ta_Zer0 提供的代码~

#include <iostream>
#include <iomanip>
 
const double timeStep = 0.1;
 
int main()
{
    double totalMass, rocketMass, burnTime, cE, g;
    std::cin >> totalMass >> rocketMass >> burnTime >> cE >> g;
    double propellantMass = totalMass - rocketMass;
    double massFlow = propellantMass / burnTime;
    double T = massFlow * cE;
    double altitude = 0, V = 0;
    double timeLeft = burnTime + timeStep;
    while(timeLeft >= 0)
    {
        double a = T / totalMass;
        double deltaV = a * timeStep;
        double deltaAltitude = V * timeStep;
        double deltaM = massFlow * timeStep;
        V += deltaV;
        altitude += deltaAltitude;
        totalMass -= deltaM;
        timeLeft -= timeStep;
    }
    std::cout << std::fixed << std::setprecision(3) << altitude / 1000 << "km" << std::endl;
    return 0;
}

晶体结构(CPP)

感谢 De1ta_Zer0 提供的代码~

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
 
struct Atom
{
    std::string name;
    double mass;
    double APF;
    double r;
};
 
Atom elemList[] =
{
    { "Po",   208.998, 0.52360, 1.68 },
    { "Li",     6.941, 0.68017, 1.52 },
    { "Na", 22.989770, 0.68017, 1.86 },
    { "Cr",   51.9961, 0.68017, 1.28 },
    { "Mn", 54.938049, 0.68017, 1.27 },
    { "Fe",    55.845, 0.68017, 1.26 },
    { "Mo",     95.94, 0.68017, 1.39 },
    { "Ta",  180.9749, 0.68017, 1.46 },
    { "Al", 26.981538, 0.74048, 1.43 },
    { "Ca",    40.078, 0.74048, 1.97 },
    { "Ni",   58.6934, 0.74048, 1.24 },
    { "Cu",    63.546, 0.74048, 1.28 },
    { "Ge",     72.64, 0.74048, 1.22 },
    { "Ag",  107.8682, 0.74048, 1.44 },
    { "Pt",   195.078, 0.74048, 1.39 },
    { "Au", 196.96655, 0.74048, 1.44 },
    { "Pb",     207.2, 0.74048, 1.75 }
};
 
template <int N> double pow(double a) { return a * pow<N - 1>(a); }
 
template <> double pow<0>(double a) { return 1; }
 
int main()
{
 
    int n;
    std::cin >> n;
    for (int i = 0; i < n; ++i)
    {
        std::string in;
        std::cin >> in;
        for(const auto& e : elemList)
        {
            if(e.name == in)
            {
                double V = 4.0 * M_PI * pow<3>(e.r) / 3.0;
                std::cout << std::fixed << std::setprecision(2) << 10.0 * e.mass * e.APF / 6.022 / V << std::endl;
                break;
            }
        }
    }
    return 0;
}

81-90

上楼梯

#include <stdio.h>
#include <string.h>

int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	long long dp[n + 1];
	memset(dp, -1, (n + 1) * sizeof(long long));
	for (int i = 0; i < m; ++i) {
		int tmp;
		scanf("%d", &tmp);
		dp[tmp] = 0;
	}

	dp[1] = dp[1] ? 1 : 0;
	dp[2] = dp[2] ? (dp[1] ? 2 : 1) : 0;
	for (int i = 3; i <= n; ++i)
		if(dp[i]) dp[i] = (dp[i - 1] + dp [i - 2]) % (long long) (1e9 + 7);
	printf("%lld\n", dp[n]);
	return 0;
}

绝对差

#include <stdio.h>
#include <limits.h>
#include <time.h>
#include <stdlib.h>

void quickSort(int arr[], int left, int right) {
	if (left >= right) return;
	srand(time(NULL));
	int idx = rand() % (left - right) + left;
	int flag = arr[idx], head = left - 1, tail = right + 1;
	while (head < tail) {
		do head++; while(arr[head] < flag);
		do tail--; while(arr[tail] > flag);
		if (head < tail) {
			int tmp = arr[head];
			arr[head] = arr[tail];
			arr[tail] = tmp;
		}
	}
	quickSort(arr, left, tail);
	quickSort(arr, tail + 1, right);
}

int minAbsSub(int arr[], int n) {
	int min = INT_MAX;
	for (int i = 0; i < n - 1; ++i) {
		int tmp = arr[i + 1] - arr[i];
		if (tmp < min) min = tmp;
	}
	return min;
}

int main() {
	int n;
	scanf("%d", &n);
	int arr[n];
	for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);

	quickSort(arr, 0, n - 1);
	printf("%d", minAbsSub(arr, n));
	return 0;
}

挑选

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>

void quickSort(long long arr[], long long left, long long right) {
	if (left >= right) return;
	srand(time(NULL));
	long long idx = rand() % (left - right) + left;
	long long flag = arr[idx], head = left - 1, tail = right + 1;
	while (head < tail) {
		do head++; while(arr[head] < flag);
		do tail--; while(arr[tail] > flag);
		if (head < tail) {
			long long tmp = arr[head];
			arr[head] = arr[tail];
			arr[tail] = tmp;
		}
	}
	quickSort(arr, left, tail);
	quickSort(arr, tail + 1, right);
}

long long maxi(long long arr[], long long n) {
	long long max = LLONG_MIN;
	for (long long i = 0; i < n; ++i)
		if (max < arr[i]) max = arr[i];
	return max;
}

long long sumMax(long long arr[], long long n) {
	long long dp[n];
	dp[0] = arr[0];
	for (long long i = 1; i < n; ++i) {
		if (arr[i] == arr[i - 1])
			dp[i] = dp[i - 1] > (arr[i] + dp[i - 1]) ? dp[i - 1] : (arr[i] + dp[i - 1]);
		else {
			long long j = i - 1;
			while (j >= 0 && arr[j] >= arr[i] - 1) --j;
			dp[i] = arr[i] + (j >= 0 ? dp[j] : 0);
		}
	}
	return maxi(dp, n);
}

int main() {
	long long n;
	scanf("%lld", &n);
	long long arr[n];
	for (long long i = 0; i < n; ++i) scanf("%lld", &arr[i]);

	quickSort(arr, 0, n - 1);
	printf("%lld\n", sumMax(arr, n));
	return 0;
}

三角形

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void quickSort(int arr[], int left, int right) {
	if (left >= right) return;
	srand(time(NULL));
	int idx = rand() % (left - right) + left;
	int flag = arr[idx], head = left - 1, tail = right + 1;
	while (head < tail) {
		do head++; while(arr[head] < flag);
		do tail--; while(arr[tail] > flag);
		if (head < tail) {
			int tmp = arr[head];
			arr[head] = arr[tail];
			arr[tail] = tmp;
		}
	}
	quickSort(arr, left, tail);
	quickSort(arr, tail + 1, right);
}

void findTri(int arr[], int n) {
	for (int i = n - 1; i > 1; --i)
		if (arr[i] < arr[i - 1] + arr[i - 2]) {
			printf("%d %d %d\n", arr[i - 2], arr[i - 1], arr[i]);
			return;
		}
	printf("-1\n");
}

int main() {
	int n;
	scanf("%d", &n);
	int arr[n];
	for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);

	quickSort(arr, 0, n - 1);
	findTri(arr, n);
	return 0;
}

子数组最大和

#include <stdio.h>
#include <string.h>

int maxSum(int arr[], int n) {
	int dp[n];
	memset(dp, 0, n * sizeof(int));
	int maxi = arr[0];
	dp[0] = arr[0];
	for (int i = 1; i < n; ++i) {
		dp[i] = arr[i] > (dp[i - 1] + arr[i]) ? arr[i] : (dp[i - 1] + arr[i]);
		maxi = dp[i] > maxi ? dp[i] : maxi;
	}
	return maxi;
}

int main() {
	int n;
	scanf("%d", &n);
	int arr[n];
	for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);
	printf("%d\n", maxSum(arr, n));
	return 0;
}

和字符串

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

long long substrToNum(char str[], int pos, int len) {
	long long num = 0;
	for (int i = 0; i < len; ++i)
		num = num * 10 + str[pos + i] - '0';
	return num;
}

long long getLen(long long n) {
	int cnt = 0;
	do ++cnt, n /= 10; while(n);
	return cnt;
}

bool backTracking(char str[], int strLen, int begin, int len1, int len2) {
	if (begin + len1 + len2 >= strLen) return true;
	long long num1 = substrToNum(str, begin, len1);
	long long num2 = substrToNum(str, begin + len1, len2);
	long long num3 = substrToNum(str, begin + len1 + len2, getLen(num1 + num2));
	if (num1 + num2 == num3) return backTracking(str, strLen, begin + getLen(num1), getLen(num2), getLen(num3));
	return false;
}

void partition(char str[]) {
	int strLen = strlen(str);
	for (int i = 1; i <= strLen / 2; ++i) {
		if (backTracking (str, strLen, 0, i, i)) {
			printf("true\n");
			return;
		}
	}
	printf("false\n");
}

int main() {
	char str[1000] = "";
	scanf("%s", str);
	partition(str);
	return 0;
}

汤包

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

typedef struct {
	int guest;
	int end;
} Tag;

void quickSort(Tag* arr[], int left, int right) {
	if (left >= right) return;
	srand(time(NULL));
	int idx = rand() % (left - right) + left;
	int flag = arr[idx]->end, head = left - 1, tail = right + 1;
	while (head < tail) {
		do head++; while(arr[head]->end < flag);
		do tail--; while(arr[tail]->end > flag);
		if (head < tail) {
			Tag *tmp = arr[head];
			arr[head] = arr[tail];
			arr[tail] = tmp;
		}
	}
	quickSort(arr, left, tail);
	quickSort(arr, tail + 1, right);
}

void traverse(Tag *arr[], int n) {
	for (int i = 0; i < n; ++i)
		printf("%d ", arr[i]->guest);
}

int main() {
	int n;
	scanf("%d", &n);
	Tag *arr[n];
	for (int i = 0; i < n; ++i) {
		arr[i] = (Tag*)malloc(sizeof(Tag));
		arr[i]->guest = i + 1;
		int begin, duration;
		scanf("%d %d", &begin, &duration);
		arr[i]->end = begin + duration;
	}

	quickSort(arr, 0, n -1);
	traverse(arr, n);
	return 0;
}

打字机

#include <stdio.h>
#include <string.h>

long long dp[100];
long long seg[100] = {0};

long long method(char str[]) {
	memset(dp, -1, 100 * sizeof(long long));
	char *iter = str;
	int part = 0, ans = 1;
	seg[part] = 1;
	while (*iter) {
		if (*iter == 'm' || *iter == 'w') return 0;
		if (*iter == 'n' && *(iter + 1) == 'n') {
			int cnt = 1;
			dp[0] = 1, dp[1] = 2, iter += 2;
			while(*iter == 'n') {
				++cnt, ++iter;
				dp[cnt] = dp[cnt - 1] + dp[cnt - 2];
			}
			seg[++part] = dp[cnt];
		}
		else if (*iter == 'u' && *(iter + 1) == 'u') {
			int cnt = 1;
			dp[0] = 1, dp[1] = 2, iter += 2;
			while(*iter == 'u') {
				++cnt, ++iter;
				dp[cnt] = dp[cnt - 1] + dp[cnt - 2];
			}
			seg[++part] = dp[cnt];
		}
		else ++iter;
	}
	for (int i = 0; seg[i] ; ++i) ans *= seg[i];
	return ans;
}

int main() {
	char str[1000];
	scanf("%s", str);
	printf("%lld", method(str));
	return 0;
}

游乐园

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

int dist[12][12];
int n, m;
bool pass[12];
int ans = -1;

bool check(int v, int u) {
	return !pass[u] && dist[v][u] != 0x3f3f3f3f;
}

bool noway(int v) {
	for (int i = 0; i < n; ++i)
		if (check(v, i)) return false;
	return true;
}

void backTracking(int v, int l) {
	if (noway(v)) {
		ans = ans > l ? ans : l;
		return;
	}
	for (int i = 0; i < n; ++i) {
		if (check(v, i) && i != v) {
			pass[i] = true;
			backTracking(i, l + dist[v][i]);
			pass[i] = false;
		}
	}
}

int main() {
	scanf("%d %d", &n, &m);
	memset(dist, 0x3f, sizeof(dist));
	for (int i = 0; i < n; ++i) dist[i][i] = 0;
	while (m) {
		int v, u, l;
		scanf("%d %d %d", &v, &u, &l);
		v -= 1, u -= 1;
		dist[v][u] = dist[v][u] < l ? dist[v][u] : l;
		dist[u][v] = dist[v][u], --m;
	}

	for (int i = 0; i < n; ++i) {
		memset(pass, 0, sizeof(pass));
		pass[i] = true;
		backTracking(i, 0);
	}
	printf("%d\n", ans);
	return 0;
}

危险的组合(CPP)

感谢 Cubeist 提供的代码~文章来源地址https://www.toymoban.com/news/detail-716836.html

#include <iostream>
using namespace std;

long long w[] = {0, 0, 0, 1, 3, 8, 20, 47, 107, 238, 520, 1121,
2391, 5056, 10616, 22159, 46023, 95182, 196132, 402873, 825259,
1686408, 3438828, 6999071, 14221459, 28853662, 58462800,
118315137, 239186031, 483072832, 974791728};

int main()
{
    int n;
    while (cin >> n, n)
    {
        if (n <= 0) exit(0);
        cout << w[n] << endl;
    }
    return 0;
}

91-100 考试模拟

【循环】圆周率

#include <stdio.h>

double pi(int n) {
	double sum = 3.0f;
	for (int i = 2; i <= n; ++i) {
		double sgn = i % 2 ? -1.0f : 1.0;
		sum += sgn * 4 / (2 * i * (2 * i - 1) * (2 * i - 2));
	}
	return sum;
}

int main() {
	int n;
	scanf("%d", &n);
	printf("%.7lf\n", pi(n));
	return 0;
}

【选择】马赫数

#include <stdio.h>
#include <math.h>

void mach(double v, double T) {
	double m = (v / 3.6f) / (331.3f * sqrt(1 + T / 273.15f));
	printf("%.3lf ", m);
	if (m - 0.8f < 1e-6) printf("subsonic\n");
	else if (m - 1.2f < 1e-6) printf("transonic\n");
	else if (m - 5.0f < 1e-6) printf("supersonic\n");
	else printf("hypersonic\n");
}

int main() {
	double v, T;
	scanf("%lf %lf", &v, &T);
	mach(v, T);
	return 0;
}

【IO】气体扩散

#include <stdio.h>
#include <math.h>

double rate(double m1, double m2) {
	return sqrt(m2 / m1);
}

int main() {
	double m1, m2;
	scanf("%lf %lf", &m1, &m2);
	printf("%.4lf\n", rate(m1, m2));
	return 0;
}

【字符串】左右操作

#include <stdio.h>
#include <string.h>

void quickSort(char str[], int left, int right) {
	if (left >= right) return;
	char flag = str[(left + right) / 2];
	int head = left - 1, tail = right + 1;
	while (head < tail) {
		do head++; while (str[head] > flag);
		do tail--; while (str[tail] < flag);
		if (head < tail) {
			char tmp = str[head];
			str[head] = str[tail], str[tail] = tmp;
		}
	}
	quickSort(str, left, tail);
	quickSort(str, tail + 1, right);
}

void reverse(char str[], int begin, int end) {
	int head = begin, tail = end;
	while (head <= tail) {
		char tmp = str[head];
		str[head] = str[tail], str[tail] = tmp;
		++head, --tail;
	}
}

int main() {
	char str[1005] = "";
	scanf("%s", str);
	int len = strlen(str);
	quickSort(str, 0, len / 2 - 1);
	int mid = (len & 1) ? (len /2 + 1) : (len / 2);
	reverse(str, mid, len - 1);
	printf("%s", str);
	return 0;
}

【结构体】空中交通管制

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct {
	char id[64];
	int x;
	int y;
} Plane;

int main() {
	int n;
	scanf("%d", &n);
	Plane *plane[n];
	for (int i = 0; i < n; ++i) {
		plane[i] = (Plane *) malloc(sizeof(Plane));
		scanf("%s %d %d", plane[i]->id, &plane[i]->x, &plane[i]->y);
	}

	double minDist = 1e9;
	int idx1, idx2;
	for (int i = 0; i < n - 1; ++i) {
		for (int j = i + 1; j < n; ++j) {
			double dist = sqrt(pow(plane[i]->x - plane[j]->x, 2) +
				pow(plane[i]->y - plane[j]->y, 2));
			if (dist < minDist) idx1 = i, idx2 = j, minDist = dist;
		}
	}
	printf("%s-%s %.4lf", plane[idx1]->id, plane[idx2]->id, minDist);
	return 0;
}

/*
6
UA057 2 3
AA044 12 30
BA1534 40 50
DL262 5 1
AF001 12 10
SK837 3 4
*/

【数组】重复元素

#include <stdio.h>

int arr[1005] = {0};

int main() {
	int n, cnt = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)	{
		scanf("%d", &arr[i]);
		for (int j = 0; j < i; ++j)
			if (arr[i] == arr[j]) {
				++cnt;
				break;
			}
	}
	printf("%d\n", cnt);
}

//10 1 10 20 1 25 1 10 30 25 1

【文件】平方根

#include <stdio.h>
#include <math.h>

int main() {
	int n;
	scanf("%d", &n);
	FILE *fp1 = fopen("rr.dat", "w");
	for (int i = 1; i <= n; ++i)
		fprintf(fp1, "%.6lf ", sqrt(i));
	fclose(fp1);

	FILE *fp2 = fopen("rr.dat", "r");
	for (int i = 1; i <= n; ++i) {
		double output;
		fscanf(fp2, "%lf", &output);
		printf("%.6lf ", output);
	}
	fclose(fp2);
	return 0;
}

 【算法】零钞

#include <stdio.h>

int main() {
	int s;
	scanf("%d", &s);

	int cnt[4], r = s;
	cnt[0] = r / 10, r -= cnt[0] * 10;
	cnt[1] = r / 5, r -= cnt[1] * 5;
	cnt[2] = r / 2, cnt[3] = r - cnt[2] * 2;

	if (cnt[3]) printf("1=%d\n", cnt[3]);
	if (cnt[2]) printf("2=%d\n", cnt[2]);
	if (cnt[1]) printf("5=%d\n", cnt[1]);
	if (cnt[0]) printf("10=%d\n", cnt[0]);
	return 0;
}

【枚举】机场翻牌显示

#include <stdio.h>
#include <ctype.h>

int cnt(char src, char dest) {
	if (src == dest) return 0;
	if (isupper(src))
		return (src < dest) ? (dest - src) : ('Z' - src + dest - 'A' + 1);
	if ('0' <= src && src <= '9') {
/*		if (src == '0') src = '9' + 1;
		if (dest == '0') dest = '9' + 1;
		return (src > dest) ? (src - dest) : (src - '0' + '9' + 1 - dest + 1);*/
		return (src < dest) ? (dest - src) : ('9' - src + dest - '0' + 1);
	}
	return -1;
}

int main() {
	char id1[10] = "", id2[10] = "";
	scanf("%s %s", id1, id2);
	int num = 0;
	for (int i = 0; id1[i] && id2[i] ; ++i) num += cnt(id1[i], id2[i]);
	printf("%d\n", num);
	return 0;
}

【递归】阿克曼数

#include <stdio.h>

int ack(int m, int n) {
	if (m == 0) return n + 1;
	if (n == 0) return ack(m - 1, 1);
	return ack(m - 1, ack(m, n -1));
}

int main() {
	int m, n;
	scanf("%d %d", &m, &n);
	printf("%d\n", ack(m, n));
	return 0;
}

到了这里,关于2023西工大NOJ (C语言版) 持续更新ing的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 西工大NOJ数据结构理论——013.以十字链表为存储结构实现矩阵相加(严5.27)

      我第一下拿到这个题目,第一反应就是先 定义好数据结构 ,然后构建好十字链表基础操作的函数,也就是“ 创插遍历 ”这些操作。下面是我的定义和函数操作。 typedef int ElemType; typedef struct OLNode{     int i,j;//行号和列号     ElemType elem;     struct OLNode *right;//右边元素 

    2023年04月26日
    浏览(85)
  • 西工大 ASLP 实验室在 WeNet 中开源基于 CPPN 的神经网络热词增强语音识别方案

    语境偏置 (Contextual biasing)旨在将语境知识集成到语音识别(ASR)系统中,以提高在相关领域词汇(俗称“ 热词 ”)上的识别准确率。在许多ASR场景中,待识别语音中可能会包含训练数据中数量很少或完全没出现的短语,例如一些领域专有名词、用户通讯录中的人名等,这

    2024年02月07日
    浏览(42)
  • 推理数据集(持续更新ing…)

    诸神缄默不语-个人CSDN博文目录 这两部分因为与GitHub上的有所重合,所以我全都归到那边了,这边的内容也放到那边了:PolarisRisingWar/Numerical_Reasoning_Collection: A collection for numerical reasoning, including datasets, algorithms and so on. LogiQA(中英文) 原始论文:(2020 IJCAI) LogiQA: A Challenge Da

    2024年02月10日
    浏览(75)
  • prompt工程(持续更新ing...)

    诸神缄默不语-个人CSDN博文目录 我准备想办法把这些东西整合到我的ScholarEase项目里。到时候按照分类、按照prompt生成方法列一堆选项,用户自己生成prompt后可以选择在ScholarEase里面聊天,也可以复制到别的地方(比如ChatGPT网页版之类的) 其实以现在GPT-4的能力来说,直接就

    2024年02月12日
    浏览(31)
  • sentinel踩坑记录——持续更新ing

    自定义被限流的后续操作@SentinelResource(blockHandler = \\\"blockHandler\\\"),其中blockHandler处理函数不执行 前置条件  定义好资源和流控规则之后,如果每秒访问超过2个请求,那么就会触发流控,从而应该执行 blockHandler(int a, BlockException e) 方法,但是实际情况是即使触发了流控规则依然

    2024年02月05日
    浏览(45)
  • Python虚拟环境管理(持续更新ing...)

    诸神缄默不语-个人CSDN博文目录 本文介绍Python语言管理虚拟环境的工具(anaconda,virtualenv) 使用虚拟环境主要是为了1. 防止新的包把整个环境搞乱 2. 有时需要跑不同版本的代码,这就需要机器上有不同版本的环境 最近更新时间:2023.5.31 最早更新时间:2023.5.31 优势是简单易

    2024年02月07日
    浏览(60)
  • Nginx 报错问题汇总(持续更新ing)

    目录 一、nginx: [emerg] invalid number of arguments in \\\"include\\\" directive in C:Program Filesnginx-1.15.4/conf/nginx.conf:61 总结: 二、nginx: [error] OpenEvent(\\\"Globalngx_reload_2152\\\") failed (5: Access is denied) 解决方案: 三、nginx: [emerg] unknown directive \\\"stream\\\" in /etc/nginx/nginx.conf:13 问题原因 解决办法 时间:2022-1

    2024年02月05日
    浏览(45)
  • 资源搜索引擎集合1(持续更新ing...)

    诸神缄默不语-个人CSDN博文目录 不是所有网址我都用过。如有失效请联系我。 电影 在线版 蓝鲸电影 电影狗 - 专业电影搜索引擎(电影狗发布页:http://www.haodyg.com/) 电影天堂 高清电台 APP 小小影视APP官网-全民影音播放器! 追风视频(可用应用宝下载) 555电影APP下载-555电影

    2024年01月20日
    浏览(33)
  • ChatGPT 3.5 API的调用不全指南(持续更新ing...)

    诸神缄默不语-个人CSDN博文目录 OpenAI platform:https://platform.openai.com/overview 最近更新时间:2023.6.14 最早更新时间:2023.5.17 关于怎么才能上ChatGPT、怎么才能获取API额度等等信息,建议直接见我的medium账号。 因为这不是能在内网发的内容。 本文不涉及相关网络问题。 我本来想靠

    2024年02月05日
    浏览(39)
  • K8s 生产环境问题汇总及解决方案(持续更新 ing)

    目录 一、前端页面显示 503 Service Temporarily Unavailable 问题背景 解决方案 原理 二、Dockfile 构建镜像失败:When using COPY with more than one source file, the destination must be a directory and end with a /  问题背景 解决方案 原理 三、前端页面显示 502 Bad Gateway 问题背景  解决方案 原理 时间:2

    2024年02月02日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包