2023西工大NOJ (C语言版) 完结!!!

博客贴出100道C语言题目的全部AC代码,不保证代码质量,仅保证能AC。其中4道题WA,5道题使用CPP。作者认为题目质量不佳,仅81 - 90题值得做,还列出了考试时会携带的资料,包含各题目的具体内容。
该文章已生成可运行项目,

前言

代码已同步至 gitee:程序设计基础实验 (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 <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
本文章已经生成可运行项目
评论 93
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值