zoukankan      html  css  js  c++  java
  • 竖式问题

    题目来自于刘汝佳编著的《算法竞赛入门经典(第二版)》

    问题描述:
    找出形如 abc*de (三位数乘以两位数) 的算式,使得在完整的竖式中,所有数字属于一个特定的数字集合。输入数字集合 (相邻数字之间没有空格),输出所有竖式。每个竖式前应有编号,之后应有一个空行。最后输出解的总数。

    样例输入:

    2357

    样例输出:

    The number of solutions = 1

    博主我才疏学浅。。。思来想去还是觉得答案的方法更简便,于是就不贴出我之前的代码了,附上答案的代码,如果有大神想出了其他方法,欢迎在回复中贴出你们的代码,感谢!o(* ̄▽ ̄*)ブ

    答案代码(已修改,与原书不一致):

    #include<stdio.h>
    #include<string.h>
    int main() {
        char s[20], buf[99];
        int count = 0;
        scanf("%s", s);
        for (int abc = 100; abc <= 999; abc++) 
        {
            for (int de = 10; de <= 99; de++)
            {
                int x = abc*(de % 10);
                int y = abc*(de / 10);
                int z = abc * de;
                sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);
                int ok = 1;
                for (int i = 0; i < strlen(buf); i++)
                    if (strchr(s, buf[i]) == NULL) ok = 0;
                if (ok) {
                    printf("<%d>
    ", ++count);
                    printf("%5d
    X%4d
    -----
    %5d
    %4d
    -----
    %5d
    
    ", abc, de, x, y, z);
                }
            }                
        }
        printf("The numbver of solutions = %d
    ", count);
        return 0;
    }

    关于评论中 循环从 111 开始的疑问,博主也同样持怀疑态度,因此将代码答案更新为从 100 开始,关于这个疑问可以参考这个网址

    http://tieba.baidu.com/p/3177382627?red_tag=h1918510311

  • 相关阅读:
    密码系统的安全性
    liboqs-量子安全密码算法开源C库
    密码基础知识(1)
    同态加密
    NTL 库函数
    以太坊相关的区块链知识
    53字符流中第一个不重复的字符
    基础小知识总结
    52表示数值的字符串 记忆
    HIHOcoder编程总结
  • 原文地址:https://www.cnblogs.com/Breathmint/p/7217046.html
Copyright © 2011-2022 走看看