Introduction to Algorithm Competitions (5) – Vertical Arithmetic Problems

Words: 284

Problem description:
Find all formulas of the form abc*de (a three-digit number multiplied by a two-digit number) such that, in the complete long-multiplication layout, every digit belongs to a given digit set. Input the digit set (with no spaces between adjacent digits), and output all the layouts. Each layout must be preceded by a number and followed by a blank line. Finally, output the total number of solutions. See the sample output for the exact format (to make it easier to read, spaces in the layout are shown as dots, but your program should output spaces, not dots).

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
//样例输入
2357
//样例输出
<1>
..775
X..33
-----
.2325
2325.
-----
25575

The number of solutions = 1

Let me add a note on what this problem is actually trying to say
and I’m also still thinking about using a Markdown editor
for writing this kind of article

This problem asks for all
formulas of the form abc*de, a three-digit number times a two-digit number,
where every number used in the long-multiplication process
is in the set “s”

and to output such layouts plus how many there are in total

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<string.h>
int main() {
    int count = 0;
    char s[20],buf[99];
    scanf("%s",s);
    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);
            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 number of solutions = %d\n",count);
    return 0;
}

Algorithm Contest Entry Classic (2nd Edition), P41

Loading...