列出所有9位數,它的前n位能被n整除
最簡單的是窮舉,不過那可要O(9*109),不可取













































posted on 2007-04-16 17:29 Dain 閱讀(1126) 評論(5) 編輯 收藏 引用 所屬分類: 算法 、筆記
寫出一個可以工作的程序并不夠
最簡單的是窮舉,不過那可要O(9*109),不可取
posted on 2007-04-16 17:29 Dain 閱讀(1126) 評論(5) 編輯 收藏 引用 所屬分類: 算法 、筆記
#include<ctime>
#include<iostream>
using namespace std;
int cnt=0;
void dfs(int s,int i){
if(i>9){
cout<<s<<endl;
cnt++;
return;
}
s*=10;
int m=s%i;
if(m!=0) m=i-m;
for(; m<10; m+=i) dfs(s+m,i+1);
}
int main(){
for(int j=0; j<10000;++j){
cnt=0;
for(int i=1; i<10; ++i) dfs(i,2);
// cout<<cnt<<endl;
}
cout<<clock();
return 0;
}
答案有2492個,不計輸出用時約0.00058秒 回復 更多評論
組合數學內容,早知道它很有用,可是還是忘記的差不多了,唉... 回復 更多評論
只有注冊用戶登錄后才能發表評論。 | ||
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
![]() |
||
相關文章:
|
||
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
|
||
|