Skip to content

Latest commit

 

History

History
157 lines (118 loc) · 2.65 KB

2020-7-4.md

File metadata and controls

157 lines (118 loc) · 2.65 KB

2020年7月4日 by HXuesong

算法编程

  • codeup-1928:日期差值
#include <stdio.h>

int month[13][2] = { {0, 0}, {31, 31}, {28, 29}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31} };

bool isLeap(int year) {
    return (!(year % 4) && year % 100) || (!(year % 400));
}

int main(void) {

	int time1, time2;

	while(scanf("%d%d", &time1, &time2) != EOF) {

		if(time1 > time2) {
			int tmp = time1;
			time1 = time2;
			time2 = tmp;
		}

		int y1, y2, m1, m2, d1, d2;
		y1 = time1 / 10000, m1 = time1 % 10000 / 100;
		d1 = time1 % 100;
		y2 = time2 / 10000, m2 = time2 % 10000 / 100;
		d2 = time2 % 100;

        int ans = 1;

        while(y1 < y2 || m1 < m2 || d1 < d2) {
            
            d1++;
            
            if(d1 == month[m1][isLeap(y1)] + 1) {
                m1++;
                d1 = 1;
            }

            if(m1 == 13) {
                y1++;
                m1 = 1;
            }
            
            ans++;
        }
        
        printf("%d", ans);
        return 0;
	}
}

对一个P进制的数,如果要转换为Q进制,需要分为两步:

  • 将P进制数x转换为十进制数y

  • 将十进制数y转换为Q进制数z

  • PAT-B1022:D进制的A+B

#include <stdio.h>

void hexConver(int a, int b, int hex) {
    int sum = a + b;
    int ans[31], num = 0;

    do{
        ans[num++] = sum % hex;
        sum /= hex;
    }while(sum);

    for(int i = num - 1; i >= 0; i--) {
        printf("%d ", ans[i]);
    }
}

int main(void) {
    int a, b, hex;
    scanf("%d%d%d", &a, &b, &hex);
    hexConver(a, b, hex);
    return 0;
}
  • codeup-5901:回文串
#include <stdio.h>
#include <string.h>

const int maxn = 256;

bool judge(char str[]) {
    int len = strlen(str);
    for(int i = 0; i < len / 2; i++) {
        if(str[i] != str[len - i - 1]) {
            return false;
        }
    }
    return true;
}

int main(void) {
    char str[maxn];
    while(gets(str)) {
        bool flag = judge(str);
        if(flag)
            printf("YES");
        else
            printf("NO");
        return 0;
    }
}
  • PAT-B1009:说反话
#include <stdio.h>
#include <string.h>

void reverseSpeaking(char str[]) {
    char ans[90][90];
    int len = strlen(str), r = 0, c = 0;
    for(int i = 0; i < len; i++) {
        if(str[i] != ' ') {
            ans[r][c++] = str[i];
        } else {
            ans[r][c] = '\0';
            r++;
            c = 0;
        }
    }

    for(int i = r; i >= 0; i--) {
        printf("%s", ans[i]);
        if(r > 0) printf(" ");
    }
}

int main(void) {
    char str[90];
    gets(str);
    reverseSpeaking(str);
}