标程¶
2045:【例5.13】蛇形填数¶
http://ybt.ssoier.cn:8088/problem_show.php?pid=2045
#include <iostream>
#include <cstdio>
using namespace std;
int a[25][25], n;
int idx;
int main()
{
    cin >> n;
    int i = 1, j = n;
    while (idx < n * n){
        while (i <= n && !a[i][j]) a[i++][j] = ++idx;
        i--, j--;
        while (j >= 1 && !a[i][j]) a[i][j--] = ++idx;
        j++, i--;
        while (i >= 1 && !a[i][j]) a[i--][j] = ++idx;
        i++, j++;
        while (j <= n && !a[i][j]) a[i][j++] = ++idx;
        i++, j--;
    }
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++) cout << a[i][j] << ' ';
        puts("");
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N][N], n;
int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};  // delta
int idx = 1;
int main(){
    cin >> n;
    int i = 1, j = n;
    while (idx < n * n){
        for (int k = 0; k < 4; k++){
            while (true){
                a[i][j] = idx++;
                i += dx[k], j += dy[k];
                if (i > n || i < 1 || j > n || j < 1 || a[i][j]){
                    i -= dx[k], j -= dy[k], idx--;
                    break;
                }
            }
        }
    }
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++) cout << a[i][j] << ' ';
        puts("");
    }
    return 0;
}
1974:【16NOIP普及组】回文日期¶
http://ybt.ssoier.cn:8088/problem_show.php?pid=1974
//注意时间复杂度,简单模拟题,细节TLE
#include <bits/stdc++.h>
using namespace std;
int d[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check02(int x)
{
    int a1 = x;
    int a2 = 0;
    while (x)
    {
        a2 = a2 * 10 + x % 10;
        x /= 10;
    }
    if (a1 == a2) return true;
    else return false;
}
bool check03(int x)
{
    if ((x % 4 == 0 && x % 100 != 0) || (x % 400 == 0))
        return true;
    return false;
}
bool check(int x)
{
    int year = x / 10000;
    int month = (x / 100) % 100;
    int date = x % 100;
    if (month > 12) return false;
    bool run = false;
    if (month == 2 && check03(year)) run = true;
    if ((!run && date > d[month]) || (run && month == 2 && date > 29))
        return false;
    return true;
}
int main()
{
    int st, ed;
    scanf("%d%d", &st, &ed);
    int ans = 0;
    for (int i = st; i <= ed; i++)
    {
        if (!check(i))  //非日期数字
            continue;
        if (!check02(i)) 
            continue;
        ans++;
    }
    printf("%d\n", ans);
    return 0;
}
枚举年份,构造回文
#include<bits/stdc++.h>
using namespace std;
int stt, ed, mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n1, n2;
int sum;
bool check(int x){
    int year = x / 10000;
    int y = x % 10000 / 100, z = x % 100;
    if (y == 0 || y > 12) return false;
    int run = 0;
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) run = 1;
    if (y == 2){
        if (z > mon[y] + run) return false;
        return true;
    }
    else{
        if (z > mon[y]) return false;
        return true;
    }
    return true;
}
int main ()
{
    scanf ("%d%d", &stt, &ed);
    n1 = stt / 10000;
    n2 = ed / 10000;
    for (int i = n1; i <= n2; i++) // years
    {
        int now = i, t = i;
        for (int j = 0; j < 4; j++){
            now = now * 10 + t % 10;
            t /= 10;
        }
        if (now < stt) continue;
        if (now > ed) break;
        if (check(now)){
            //printf("---%d\n", now);
            sum++;
        }
    }
    printf ("%d\n", sum);
    return 0;
}
枚举所有日期,判断是否在范围内
#include <bits/stdc++.h>
using namespace std;
int date1, date2;
int ret;
int mon[20] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(){
    cin >> date1 >> date2;
    for (int i = 1; i <= 12; i++)
        for (int j = 1; j <= mon[i]; j++){
            int y = i * 100 + j;
            int x = 0, t = y;
            for (int k = 0; k < 4; k++){
                x = x * 10 + t % 10;
                t /= 10;
            }
            x = x * 10000 + y;
            if (x >= date1 && x <= date2) ret++;
        }
    cout << ret << '\n';
    return 0;
}