一个FLAG #03# 蛇形填数

例题3-3 蛇形填数

#include <stdio.h>
#include <string.h>

#define maxn 20

int a[maxn][maxn];

int main()
{
    int n, x, y, tot = 0;
    
    scanf("%d", &n); // 表示方阵大小 
    memset(a, 0, sizeof(a));
     
    tot = a[x = 0][y = n - 1] = 1;
    
    while (tot < n * n) {
        // ++x 是事前加,而不是事后。 x++是事后 
        while (x + 1 < n && !a[x + 1][y]) a[++x][y] = ++tot; // 先往下走
        while (y - 1 >= 0 && !a[x][y-1]) a[x][--y] = ++tot; //
        while (x - 1 >= 0 && !a[x-1][y]) a[--x][y] = ++tot; //
        while (y + 1 < n && !a[x][y+1]) a[x][++y] = ++tot; //// 第一条件是保证不出方块 第二条件是保证不“撞墙”,属于赋值前的试探         
    }    
    
    for    (x = 0; x < n; ++x) {
        for (y = 0; y < n; ++y) {
            printf("%3d", a[x][y]);
        }
        printf("
");
    } 
    
    return 0;
}

例题3-4 竖式问题

#include <stdio.h>
#include <string.h>

// 字符串处理 - 竖式问题 
int main()
{
    int count = 0;
    char s[20], buf[99];
        
    scanf("%s", s); // 注意,“scanf("%s", s)”遇到空白字符会停下来。
    // 空白字符是指 空格 回车 以及TAB 
    // 字符常量可以用单引号法表示。在语法上可以把字符当作int型使用。
    
    for (int abc = 111; abc <= 999; ++abc) {
        for (int de = 11; de <= 99; ++de) {
            int x = abc * (de % 10), 
            y = abc * (de / 10), 
            z = abc * de;
            
            sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z); 
            // sprintf函数定义在stdio.h。按指定格式输出到字符串。
            // 可胜任“花式”的字符串拼接工作。
            // 每次调用该函数,原有的内容会被清空。而非追加 
            // 如果成功,则返回写入的字符总数,不包括字符串追加在字符串末尾的空字符。
            // 如果失败,则返回一个负数。 
    
            // 算法竞赛入门经典原话:由于字符串的本质是数组,它也不是“一等公民”,只能用strcpy(a, b),
            // strcmp(a, b), strcat(a, b)来执行“赋值”、“比较”和“连接”操作,而不能用“=”、“==”、
            // “<=”、“+”等运算符。上述函数都在string.h中声明。        
            
            int ok = 1;            
            for (int i = 0; i != strlen(buf); ++i) {
                if (strchr(s, buf[i]) == NULL) {
                    // strchr返回一个指向该字符串中第一次出现的字符的指针
                    ok = 0;
                }
            }
            
            if(ok){
                printf("<%d>
", ++count);
                printf("%5d
X%4d
-----
%5d
%4d
-----
%5d

", abc, de, x, y, z);
            }
        }
    }
    
    printf("The number of solutions = %d
", count);
    return 0;
}

输入一个32位的二进制数X,输出X+1以及X+2(同样以二进制形式)[7]

#include <stdio.h>
#include <string.h>
 
int main()
{
    char buf[99];
    int n;
    
    scanf("%d", &n);
    
    for (int i = 0; i != n; ++i) {
        
        scanf("%s", buf);
        
        // X + 1
        buf[31] += 1;
        
        int k = 31;
        while (k > 0 && buf[k] > '1') {
            buf[k] = '0';
            buf[k - 1] += 1;
            k--;
        }
        
        printf("%s
", buf);
        
        // X + 3
        // 原基础再 加 2 即加二进制数 10 
        // 直接跳过第一位就可以了 
        buf[30] += 1;
        
        k = 30;
        while (k > 0 && buf[k] > '1') {
            buf[k] = '0';
            buf[k - 1] += 1;
            k--;
        }
        
        printf("%s
", buf);
    }    
    
    return 0;
}

调BUG

字符数组越界输入问题。

#include <stdio.h>
#include <string.h>
 
int main()
{
    char buf[32];
    int n;
    
    scanf("%d", &n);
    
    
    for (int i = 0; i != n; ++i) {
        printf("i = %d , n = %d, %d
", i, n, i != n);
        // 超出buf数组的范围进行输入,陷入死循环
        // 输出 i 和 n 发现,其值被改变了。
        // 说明 i 和 n 的内存地址恰好在 buf 数组之后 
        // 在范围内输入 一切正常。
        // 所以,应该适当把 buf 搞大点。不要刚刚好。 
        
        scanf("%s", buf);
        
        printf("%s
", buf); // 原样输出,超出 32 部分也输出来 
        
        printf("strlen = %d
", strlen(buf)); // 实际长度,不限制32  
        for (int j = 0; j != strlen(buf); ++j) {
            printf("%c", buf[j]);
        }
        printf(" end a loop .===================
");
    }    
    
    return 0;
}

参考

[1] C语言: !(-1)_C/C++_Ken的博客-CSDN博客

[2] C++中%3d,%-3d,%03d是什么意思?_百度知道

[3] C 库函数 – sprintf() | 菜鸟教程

[4] sprintf的使用_Java_weixin_43224243的博客-CSDN博客

[5] C 库函数 – strchr() | 菜鸟教程

[6] strchr_百度百科

[7] Problem A 二进制(2019计算机)_Closure-CSDN博客

原文地址:https://www.cnblogs.com/xkxf/p/12522876.html