欢 迎 光 临 天 道 无 名 的 博 客
西湖BLOG正在为您加载数据中……www.xhblog.com

 || 天 道 无 名 

这就是我
西湖BLOG正在为您加载数据中……www.xhblog.com
西湖BLOG正在为您加载数据中……www.xhblog.com
最新公告
西湖BLOG正在为您加载数据中……www.xhblog.com
站点日历
<<  < 2008 - >  >>
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
最新日志
西湖BLOG正在为您加载数据中……www.xhblog.com
最新回复
西湖BLOG正在为您加载数据中……www.xhblog.com
最新留言
西湖BLOG正在为您加载数据中……www.xhblog.com
 日志搜索

友情链接
其他信息


·数字迷阵(zt)     -|jt74 发表于 2008-7-16 8:28:00

小可可参观科学博物馆时,看到一件藏品,上面有密密麻麻的数字,如下所示:
1    2    3    5    8    13    21    34    55    89    144 ...
4    7    11  18    29    47    76    123  199    322    521 ...
6    10  16  26    42    68    110  178  288    466    754 ...
9    15  24  39    63    102  165  267  432    699    1131 ...
12  20  32  52    84    136  220  356  576    932    1508 ...
14  23  37  60    97    157  254  411  665    1076  1741 ...
17  28  45  73    118  191  309  500  809    1309  2118 ...
19  31  50  81    131  212  343  555  898    1453  2351 ...
22  36  58  94    152  246  398  644  1042  1686  2728 ...
25  41  66  107  173  280  453  733  1186  1919  3105 ...
27  44  71  115  186  301  487  788  1275  2063  3338 ...
...
仔细一分析,发现还挺有规律。
原来,第一行是Fibonacci数列。即,该行中除了第一个和第二个数分别为1和2之外,其他数都是其左侧相邻的两个数之和。
其后各行也类似于Fibonacci数列。只是第i行的第一个数是前 行中未出现的最小正整数,而其第二个数与该行第一个数以及所在行的编号相关,即A[i,2]=A[i,1]*2-(i-1) 。如在第一行中未出现的最小正整数为4,前三行中未出现的最小正整数为9。故第二行以4和7开头,而第四行以9和15开头。
小可可高兴地把这个发现告诉了爷爷。爷爷问道:你能否一口报出第i行、第j列的那个数对m取模的结果是多少呢?
聪明的小可可通过心算就能知道答案。你是否能编写程序求解呢?

输入:每行有三个分别用一个空格隔开的正整数,分别是i、j和m。
其中,i, j<=1000000000 ,2<=m<=10000 。
输出:每行输出对应的第i行、第j列的那个正整数对m取模的结果。

样例:
(1) 输入(matrix.in):
    1 2 99
输出(matrix.out):
    2
(2) 输入(matrix.in):   
9 1 999
输出(matrix.out):
    22

原贴见:
http://bbs.chinaunix.net/viewthread.php?tid=1028591&extra=page%3D4%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D134

给出一个自己的程序,不过没找到什么规律,挺笨的!希望高手能给个更优的方法出来。
#i nclude<iostream>
#i nclude<vector>
#i nclude<algorithm>
#i nclude<queue>
#i nclude<bitset>
#i nclude<cstdlib>
using namespace std;
const unsigned long MAX=10000L;
int minIndex(const bitset<MAX>&b){
   static int index=0;
   if(index>MAX-1){
     cout<<index<<endl;
     throw "index overflow";
   }
   while(b[++index]);
   return index;
}
int main(int argc,char *argv[]){
  bitset<MAX>b;
  int rownum=0,colnum=0;
  cout<<"please input the row and col number:";
  cin>>rownum>>colnum;
  vector< vector<int> > matrix;
  int row=1;
  while(row<=rownum){
    vector<int> *v=new vector<int>(colnum);
    (*v)[0]=minIndex(b);
    (*v)[1]=(*v)[0]*2-(row-1);
      b[(*v)[0]]=1;
      b[(*v)[1]]=1;
    for(int i=2;i<colnum;i++){
      (*v)[i]=(*v)[i-1]+(*v)[i-2];
      if((*v)[i]<MAX) b[(*v)[i]]=1;
    }
    matrix.push_back(*v);
    delete v;
    row++;
  }
  for(int i=0;i<matrix.size();i++){
    for(int j=0;j<matrix[i].size();j++){
      cout<<matrix[i][j]<<'\t';
    }
    cout<<endl;
  }
  cout<<"please input the i,j and m:";
  int i,j,m;
  cin>>i>>j>>m;
  if(i<0||i>=rownum||j<0||j>=colnum){
    cerr<<"range error"<<endl;
    exit(1);
  }
  cout<<"The result is:"<<matrix[i][j]%m<<endl;
  }
[阅读全文 | 回复(1) | 引用通告 | 编辑]

·Re:数字迷阵(zt)     -|雪茄(游客)发表评论于2008-7-16 8:55:00
雪茄(游客)咳,这人和人的差距就是没法说。玩的东西都不一样.
[个人主页 | 引用 | 返回 | 删除 | 回复]

发表评论:
西湖BLOG正在为您加载数据中……www.xhblog.com
Google Search
© COPYRIGHT 2004 ALL RIGHTS RESERVED 西湖BLOG正在为您加载数据中……www.xhblog.com