Question 1: Find the nine-digit numbers that meet the condition. Each digit is different (1 ~ 9) to enable N-bit division by N. For example, abcdefghi, the two high AB nodes can be divisible by two, the three ABC nodes can be divisible by three, and so on.
A relatively simple solution
#include "stdafx.h"#include <iostream>using namespace std;void findNum(bool* used,int index,long sum){if(index !=0 && sum%index != 0){return;}else{if(index==9){cout<<"The number is "<<sum<<" ."<<endl;return;}}for(int i=1;i<=9;i++){if(used[i]==false){used[i]=true;findNum(used,index+1,sum*10+i);used[i]=false;}}return;}int main(){bool used[10]={false};int index=0;long sum=0;findNum(used,index,sum); system("pause"); return 0;}
Question 2: I am a great Buddhist temple *
Code
#include "stdafx.h"#include <vector>#include <iostream>using namespace std;int findPalindrome(int sum){vector<int> s;int temp=sum;while(temp!=0){int a=temp%10;s.push_back(a);temp=temp/10;}temp=0;vector<int>::iterator it=s.begin();while(it!=s.end()){temp=temp*10+*it;it++;}return temp;}void findNum(bool* used,int index,long sum){if(index==5){int temp=findPalindrome(sum);for(int i=1;i<=9;i++){if(used[i]==false){if(sum*i==temp){cout<<sum<<" * "<<i<<" = "<<temp<<endl;return;}}}return;}for(int i=1;i<=9;i++){if(used[i]==false){used[i]=true;findNum(used,index+1,sum*10+i);used[i]=false;}}return;}int main(){bool used[10]={false};int index=0;long sum=0;findNum(used,index,sum); system("pause"); return 0;}
The beauty of programming 4.10 digital dummies and replies