The beauty of programming 4.10 digital dummies and replies

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.