乘法竖式问题(ACM)

来源:互联网 发布:南风知我意2百度云资源 编辑:程序博客网 时间:2024/05/01 20:29

乘法竖式问题(ACM)

Description

找出所有abc * de ( 三位数字乘以两位数字 ) 的算式, 使得在完整的竖式中, 所有数字都属于一个特定的数字集合.

输入数字集合 ( 相邻数字之间没有空格 ) , 输出所有竖式.

每个竖式前应有编号, 之后应有一个空行. 最后输出解的总数. 具体格式见样例输出.

( 横线固定为5个 ’ - ’ , 乘号为 ’ X ’ )
Input

输入

一组数字集合, 数字之间没有空格, 仅一组输入.(数字可能有重复, 不超过20位)

如:23457777 Output

输出

一个竖式. 每个竖式前应有编号 格式: (n为编号)

竖式输出完毕后有一个空行, 最后一行输出解的个数: The number of solutions = n (n为解的个数)

若无解则输出The number of solutions = 0

Sample Input

2357

Sample Output

<1>      775 X  33----- 23252325-----25575The number of solutions = 1

思路:
尝试所有的abc和de即可

    for(abc = 111; abc <= 999; abc ++) {        for(de = 11; de <= 99; de++) {            if(abc * de满足条件)                ....        }    }

重点:

— 关于输入:

使用sprintf输入, sprintf是指输出到字符串, 用法:

int abc = 111, de = 11, x = 0, y = 0, z = 0;char buf[99];sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);

无需对字符串转换, 直接将int放入了char *的数组
如果此时printf(“%s”, buf)则结果为11111000

接下来有两个问题:判断和输出

输出:

printf("%5d\nX%4d\n-----\n%5d\b%4d\b-----\n%5d\n\n", abc, de, x, y, z);//或者cout << setw(5) << abc << "\nX" << setw(4) << de << "\n-----\n" << setw(5) << x << "\n" << setw(4) << y << "\n-----\n" << setw(5) << z << "\n\n";

判断:
使用strchr(char* a, char ch)函数, 第一个参数是被匹配的数组, 第二个参数是要查找的字符, 若从数组a中找到了字符ch, 会返回第一次出现的位置的索引, 否则返回NULL

for(int i = 0; i < strlen(buf); i++) {    if(strchr(s, buf[i]) == NULL) {        flag = 0;//判断是否在数组        break;//若有一个字符不在数组集合中, 则此竖式不满足条件, 直接break即可    }}

c++的sstream库

sstream 包含了 istringstream(从 string 读)/ostringstream(向 string
写)/stringstream(读写 string)

iostream 和 fstream 是两个比较常用的IO 库,我们这里不再回顾,这里简单回顾一下 sstream。

如果你熟悉 C 语言,就知道将 int 转换为 string 类型其实是一件很麻烦的事情,虽然标准库中提供了 itoa()
这种函数,但是依然需要对转换后的 C 风格字符串(char *)通过 std::string 的构造函数构造为 std::string。
如果使用流操作,那么这将变得异常的简单:

#include <string>#include <sstream>#include <iostrea>int main() {    // std::stringstream 支持读写    std::stringstream stream;    std::string result;    int number = 12345;    stream << number;   // 将 number 输入到 stream    stream >> results;  // 从 stream 读取到 result    std::cout < result << std::endl; // 将输出为字符串"12345"}

如果希望让sstream 和 C 风格的字符串打交道,同样也可以:

#include <sstream>#include <iostream> int main(){    std::stringstream stream;    char result[6];    stream << 12345;    stream >> result;    std::cout << result << std::endl;}

需要注意的一点就是,在进行多次IO 操作时,如果希望结果彼此不影响,需要对 stream 对象进行一次 clear() 操作:

stream.clear()

乘法竖式解决代码:

#include <iostream>#include <sstream>#include <iomanip>using namespace  std;int main() {    int i, flag, abc, de, x, y, z, count = 0;    char s[20], buf[99];    cin >> s;    for(int abc = 111 ; abc <= 999; abc ++) {        for(int de = 11 ; de <= 99 ; de ++) {            x = abc * (de % 10);            y = abc * (de / 10);            z = abc * de;            sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);            flag = 1;            for(int i = 0 ; i < strlen(buf) ; i++) {                if(strchr(s, buf[i])== NULL){                    flag = 0;                }            }            if(flag) {                cout << "<" << ++count << ">" << endl;                //count ++;                cout << setw(5) << abc << "\nX" << setw(4) << de << "\n-----\n" << setw(5) << x << "\n" << setw(4) << y << "\n-----\n" << setw(5) << z << "\n\n";            }        }    }    cout << "The number of solutions = " << count << endl;}
0 0
原创粉丝点击