首页 > 其他 > 详细

竖式问题

时间:2017-07-21 13:16:31      阅读:362      评论:0      收藏:0      [点我收藏+]

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

问题描述:
找出形如 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 = 111; abc <= 999; abc++) 
    {
        for (int de = 11; 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>\n", ++count);
                printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n", abc, de, x, y, z);
            }
        }                
    }
    printf("The numbver of solutions = %d\n", count);
    return 0;
}

 

竖式问题

原文:http://www.cnblogs.com/Breathmint/p/7217046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!