算法競賽入門 3-3 豎式問題

實現代碼如下:

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string>

using namespace std;
bool Solute(int a,int b,int c1,int c2,int c,string s);
int GetLength(int n);
int main()
{
//    freopen("out.txt","w",stdout);

    int c1,c2,c,a,b;
    int counter = 0;

    string inputStr;
    cin >> inputStr;

    for(a=100 ; a<999;a++){
        for(b=10 ; b<99 ;b++){
            c1 = a * (b % 10);
            c2 = a * (b / 10);
            c = a * b;

            //lenth = c.length;
            if(Solute(a,b,c,c1,c2,inputStr)==true){
                int length = GetLength(c);
                counter ++;
                cout << "<" <<counter << ">" << endl;
                cout << setw(length) << a << endl;
                cout << "X" << setw(length-1) << b << endl;
                cout << "----" << endl;
                cout << setw(length) << c1 << endl;
                cout << setw(length-1) << c2 << endl;
                cout << "----" << endl;
                cout << setw(length) << c << endl;
            }
        }
    }
    cout << endl << "The number of solutions = "<< counter << endl;
    return 0;
}

bool ContainStr(string a,string b)
{
    for(string::iterator it=b.begin() ; it!=b.end() ; it++){
        if(a.find(*it) == string::npos){
            return false;
        }
    }
    return true;
}
bool Solute(int a,int b,int c1,int c2,int c,string s)
{
    bool result = false;
    char buf[100];
    string s1;

    sprintf(buf,"%d%d%d%d%d",a,b,c,c1,c2);
    s1 = buf;
    if(ContainStr(s,s1) == true){
        result = true;
    }
    return result;
}

int GetLength(int n)
{
    int length = 0;
    while(n != 0){
        length ++;
        n /= 10;
    }
    return length;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章