C++笔记(7)——一些模拟题:简单模拟、查找元素、图形输出、日期处理、进制转换、字符串处理

以下内容基本来自《算法笔记》,作者为胡凡,建议直接买书看,我这里只是摘抄部分当笔记,不完整的。

简单模拟

就是一类“题目怎么说你就怎么做”的题目。这类题目不涉及算法,只是根据题目描述来进行代码的编写。

例子:

思路:

#include <cstdio>
init main() {
    int n, step = 0;
    scanf("%d", &n);
    while(n != 1) {
        if(n%2==0) {
            n = n / 2;
        }
        else {
            n = (3*n+1) / 2;
        }
        ++step; // 计数器加1
    }
    printf("%d\n", step);
    return 0;
 }

查找元素

要求就是给定一些元素,然后查找某个满足某条件的元素。一个典型的算法就是二分查找。

思路:

#include <cstdio>
const int maxn = 210;
int a[maxn];    // 存放给定的元素
int main() {
    int n, x;
    while(scanf("%d", &n) != EOF) {
        for(int i=0; i < n; ++i) {
            scanf("%d", &a[i]); // 输入元素的个数
        }
        scanf("%d", &x);    //输入要查询的元素
        int k;  // 下标
        for(k=0; k<n; ++k) {    // 遍历数组
            if(a[k] == x) { // 找到了
                printf("%d\n", k);
                break;
            }
        }
        if(k==n) {  // 没有找到
            printf("-1\n");
        }
    }
}

图形输出

给定规则,要求按照规则用输出字符的方式来画图。

做法一般两种:

  1. 通过规律直接输出
  2. 定义一个二位字符数组,按照规律填充内容,然后输出整个二维数组

例子:

思路:

注意点:

#include <cstdio>
int main() {
    int row, col;
    char c;
    scanf("%d %c", &col, &c);
    if(col % 2 == 1) {
        row = col / 2 + 1;
    }
    else {
        row = col / 2;
    }
    for(int i=0; i<col; ++i) {
        printf("%c", c);
    }
    printf("\n");
    for(int i=2; i < row; ++i) {
        printf("%c", c);
        for(int j=0; j<col-2; ++j) {
            printf(" ");
        }
        printf("%c\n", c);
    }
    for(int i=0; i<col; ++i) {
        printf("%c", c);
    }
    return 0;
}

日期处理

这种类型主要是细节比较繁杂,细心处理好了就很简单了。

思路:

#include <cstdio>
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==0 && year%100!=0) || (year%400==0);
}

int main() {
    int time1, y1, m1, d1;
    int time2, y2, m2, d2;
    while(scanf("%d%d", &time1, &time2) != EOF) {
        // 确保大的日期在time2
        if(time1 > time2) {
            int tmp = time1;
            time1 = time2;
            time2 = tmp;
        }
        // 提取时间
        y1 = time1 / 10000, m1 = time1 % 10000 / 100, d1 = time1 % 100;
        y2 = time2 / 10000, m2 = time2 % 10000 / 100, d1 = time1 % 100;
        int ans = 1;
        while(y1 < y2 || m1 < m2 || d1 < d2) {
            ++d1;
            // 当天数超出该月最大天数,月份+1,天数归一
            if(d1 == month[m1][isLeap(y1)] + 1) {
                ++m1;
                d1 = 1;
            }
            // 和上面类似
            if(m1 == 13) {
                ++y1;
                m1 = 1;
            }
            ++ans;
        }
        printf("%d\n", ans);
    }
    return 0;
}

进制转换

转为10进制:

int y=0, product = 1;
while(x != 0) {
    y = y + (x % 10) * product;
    x = x / 10;
    product = product * P; // P进制
}

转为Q进制:

int z[40], num = 0; // z存放Q进制数y的每一位,num为位数
do {
    z[num++] = y % Q;
    y = y / Q;
} while(y!=0);

字符串处理

就是字符串处理……

例子:

思路:

这里的思路是通过字符串来判断的,其实还有别的方法,不过这里就不细说了。

#include <cstdio>
#include <cstring>

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-1-i]) {
            return false;
        }
    }
    return true;
}

int main() {
    char str[maxn];
    while(gets(str)) {
        bool flag = judge(str);
        if(flag==true) {
            printf("YES\n");
        }
        else {
            printf("NO\n");
        }
    }
    return 0;
}

吐槽下,这里的代码其实都是C语言的风格,不过我暂时不想改,因为我目前没办法访问CodeUp,不确定改了之后一定没问题(虽然说就算没改也不确定就是了)。

嘛,目前就这些。

猜你喜欢

转载自www.cnblogs.com/yejianying/p/cpp_notes_7.html
今日推荐