算法入门经典P43-3-4(sprintf和strchr)

#include<iostream>
#include<string.h>
using namespace std;
int main(){
    char s[8],buf[20];
    int abc,de,x,y,z,count = 0;
    bool ok;
    scanf("%s",s);
    for(abc=100;abc<=999;abc++){
        for(int de=10;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);
            ok = true;
            for(int i=0;i<strlen(buf);i++){
                if(strchr(s,buf[i])==NULL){
                    ok = false;
                }
            }
            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;
} 

 

posted @ 2021-11-02 16:06  智人心  阅读(59)  评论(0)    收藏  举报