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

  • 相关阅读:
    CSS3 定位| Position研究
    ASP.NET中Partial Class部分类
    如何安全实现“记住我”的功能
    JavaScript后台代码操作HTML TABLE的方法
    强悍的CSS工具组合:Blueprint, Sass, Compass
    CSS的力量:用一个DIV画图
    ASP.NET记录错误日志的方式
    ASP.NET私有构造函数作用
    c#.net实现浏览器端大文件分块上传
    asp.net实现浏览器端大文件分块上传
  • 原文地址:https://www.cnblogs.com/Breathmint/p/7217046.html
Copyright © 2011-2022 走看看