Loading

2021级信息工程学院《C语言程序设计》阶段1考试

Problem A: 十六进制字符个数统计

Solution

每个数字转16进制,逐位判断即可

Code

#include <stdio.h>

int ans;
void check(int n){
    while(n){
        int tmp = n % 16;
        if(tmp >= 10 && tmp <= 15) ans++;
        n /= 16;
    }
}

signed main(){
    int i = 0;
    int l, r;
    scanf("%d", &l);
    scanf("%d", &r);
    for(i = l; i <= r; i++)
        check(i);
    printf("%d\n", ans);
    return 0;
}

Problem B: 16进制的简单运算

Solution

先找出字符串中运算符的位置,将前后两个数16进制转换成十进制进行运算,结果转化成8进制输出即可。

Code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
char s[100010];

int ans[55], cnt;
void change(int n){
    while(n){
        int add = n % 8;
        ans[++cnt] = add;
        n /= 8;
    }
}

signed main(){
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%s", s);
        int id, i = 0, len = strlen(s), a = 0, b = 0;
        for(i = 0; i < len; i++)
            if(s[i] == '+' || s[i] == '-'){
                id = i;
                break;
            }
        int tmp = 0; //次方
        for(i = id - 1; i >= 0; i--){
            int times = 0;
            if(s[i] >= 'A' && s[i] <= 'F') times = s[i] - 'A' + 10;
            else times = s[i] - '0';
            a += times * pow((double)16, (double)tmp);
            tmp++;
        }

        tmp = 0;
        for(i = len - 1; i > id; i--){
            int times = 0;
            if(s[i] >= 'A' && s[i] <= 'F') times = s[i] - 'A' + 10;
            else times = s[i] - '0';
            b += times * pow((double)16, (double)tmp);
            tmp++;
        }

        cnt = 0;
        if(s[id] == '-') tmp = a - b;
        else tmp = a + b;
        change(tmp);
        for(i = cnt; i >= 1; i--) printf("%d", ans[i]);
    }
    return 0;
}

Problem C: 排序归位

solution

这个题做过吧,比较大小,如果小的在前面,就交换两个数值的位置,注意每次交换不必都找第三个“空杯子”,交换值得过程可以也写个函数,主函数直接调用排序函数即可。

Code

#include <stdio.h>

void swap(int *a, int *b){
    int temp = *a;
    *a = *b;
    *b = temp;
}

void Sort(int *a, int *b, int *c){
    if(*a < *b) swap(a, b);
    if(*a < *c) swap(a, c);
    if(*b < *c) swap(b, c);
}

signed main(){
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    Sort(&a, &b, &c);
    printf("%d %d %d\n", a, b, c);
    return 0;   
}

Problem D: 第几天?

Solution

本质就是…………………………判断闰年???

Code

#include <stdio.h>

char s[15];
int a[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int year, month, day;
signed main(){
    while(scanf("%s", s) != EOF){
        year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
        month = (s[5] - '0') * 10 + (s[6] - '0');
        day = (s[8] - '0') * 10 + (s[9] - '0');
        int ans = 0, i;
        if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
            if(month == 1) ans = day;
            else if(month == 2) ans = 31 + day;
            else {
                for(i = 1; i <= month - 1; i++) ans += a[i];
                ans += day;
            }
        }
        else {
            for(i = 1; i <= month - 1; i++) ans += a[i];
            ans += day;
        }
        printf("%d\n", ans);
    }
    return 0;
}

Problem E

传送门

posted @ 2022-11-08 20:17  Gary_818  阅读(616)  评论(2编辑  收藏  举报