Re: 求助:5道算法题
生活随笔
收集整理的這篇文章主要介紹了
Re: 求助:5道算法题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://www.newsmth.net/frames.html
發信人: cutepig (cutepig), 信區: Algorithm
標 ?題: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 18:25:06 2007), 站內
1)given a integer, output its previous and next neighbor number which has the same number of bit 1 in their binary representation.
(1)只想到一種很笨的方法,就是將這個數遞增或者遞減,直到找到一個和它1的位數一樣多的為止,應該有更好的方法
(2)
滿足條件的比這個數大的數應該是這個數從最低位開始,找到的第一個的01組合對調。
滿足條件的比這個數小的數應該是這個數從最低位開始,找到的第一個的10組合對調。
2) Given 1 GB memory, input a file which contians 4 billion integers, output one integer that is not in the file. What if you have only 10 MB memory?
這個如果用一位表示一個數的存在與否的話,需要2^32bits=2^32/8bytes=2^29=512MB內存,用10M的話似乎只能讀多次文件,每次判斷某一部分數存在與否,有沒有更好的辦法?
3) how to divide an integer array into 2 sub-arrays and make their averages equal? e.g. a[left_portion]/left_portion_num == a[right_portion]/right_portion_num.
4)Given n unsigned integer, output 2 integers which has the maximum result after XOR.
莫非要遍歷所有可能的組合?
5)Input an integer array of size n and an integer k (k<=n), output all subsets of size k.
(1)這個類似于全排列的生成算法吧,想出一個遞歸的回溯方法
array,n:數組和數組大小
k:子數列大小
void output(int *array,int n,int k)
{
?? static int used[n];//這個靜態數組記錄是否該元素已經輸出了,初始化為0
?? static int outdata[k];//記錄已經輸出的元素
?? if k<=0 ,return;
?? 對于array的每一個未輸出的元素array[i]
??{
?? ? 將該元素放到outdata中,標記used[i]=1;
?? ? 如果滿k個了,則輸出outdata的數據
?? ? 否則,遞歸調用output(array,n,k-1)
?? ? 回溯,令used[i]=0;
??}
}
(2)或者寫一個Next函數用來計算當前排列的下一個排列
BOOL?Next(int?*data,int?nmax,int?k)
{
int?i;
#define?MyMax(i)?(nmax-k+i)
for?(i=k-1;i>=0?;i--)//從后向前找到第一個可以增加的數
{
ASSERT(data[i]>=0?&&?data[i]<=MyMax(i));
if(data[i]<MyMax(i))
{
break;
}
}
if(i>=0)//將該位++,后面各位遞增
{
data[i]++;
for?(int?j=i+1;j<k;j++)
{
data[j]=data[j-1]+1;
ASSERT(data[j]<=MyMax(j))
}
return?TRUE;
}
else
return?FALSE;
}
初始化data={0,...,k-1},再一直調用Next就可以得到所有的排列
發信人: scottfield (金蛇郎君), 信區: Algorithm
標 ?題: Re: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 20:18:14 2007), 站內
第三個,可以參考CLRS,可以線性時間求得,是weighted-select problem
把值看成權就行了。
發信人: scottfield (金蛇郎君), 信區: Algorithm
標 ?題: Re: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 20:24:24 2007), 站內
第四題,XOR是 00->1 11->1是不?
則只要找出兩個最高位相同的倍數最多的不就行了?
用Significant-bit Radix Sort
發信人: ttl (小驢|主ID), 信區: Algorithm
標 ?題: Re: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 20:36:27 2007), 站內
【 在 wlalbert (找個打我球的女朋友) 的大作中提到: 】
如何用c++實現?
【 在 cutepig (cutepig) 的大作中提到: 】
: 似乎真的是這樣呀
: 滿足條件的比這個數大的數應該是這個數從最低位開始,找到的第一個的01組合對調。
: 滿足條件的比這個數小的數應該是這個數從最低位開始,找到的第一個的10組合對調。
: ...................
void find(int i)
{
?? ? ? ?int f = 3; // 11
?? ? ? ?int pf = 2; // 10
?? ? ? ?int nf = 1; // 01
?? ? ? ?int p = 0; // 小
?? ? ? ?int n = 0; // 大
?? ? ? ?while (f < 4 * i)
?? ? ? ?{
?? ? ? ? ? ? ? ?int tmp = f & i;
?? ? ? ? ? ? ? ?if (!p)
?? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ?if (0 == (tmp ^ pf))
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?p = i ^ f;
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?if (!n)
?? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ?if (0 == (tmp ^ nf))
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?n = i ^ f;
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?f ?<<= 1;
?? ? ? ? ? ? ? ?pf <<= 1;
?? ? ? ? ? ? ? ?nf <<= 1;
?? ? ? ?}
?? ? ? ?cout << p << endl;
?? ? ? ?cout << n << endl;
}
發信人: zgx03 (時間旅客), 信區: Algorithm
標 ?題: Re: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 21:07:26 2007), 站內
第4題這么做,
設原數組為A,里面的元素取反后形成第二個數組B,把這兩個數組合起來形成數組C。
把C排一下序,找C的相鄰兩個分別屬于A和B且二進制最高幾位連續相同最多的,即可。
復雜度NlogN。
發信人: cutepig (cutepig), 信區: Algorithm
標 ?題: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 18:25:06 2007), 站內
1)given a integer, output its previous and next neighbor number which has the same number of bit 1 in their binary representation.
(1)只想到一種很笨的方法,就是將這個數遞增或者遞減,直到找到一個和它1的位數一樣多的為止,應該有更好的方法
(2)
滿足條件的比這個數大的數應該是這個數從最低位開始,找到的第一個的01組合對調。
滿足條件的比這個數小的數應該是這個數從最低位開始,找到的第一個的10組合對調。
2) Given 1 GB memory, input a file which contians 4 billion integers, output one integer that is not in the file. What if you have only 10 MB memory?
這個如果用一位表示一個數的存在與否的話,需要2^32bits=2^32/8bytes=2^29=512MB內存,用10M的話似乎只能讀多次文件,每次判斷某一部分數存在與否,有沒有更好的辦法?
3) how to divide an integer array into 2 sub-arrays and make their averages equal? e.g. a[left_portion]/left_portion_num == a[right_portion]/right_portion_num.
4)Given n unsigned integer, output 2 integers which has the maximum result after XOR.
莫非要遍歷所有可能的組合?
5)Input an integer array of size n and an integer k (k<=n), output all subsets of size k.
(1)這個類似于全排列的生成算法吧,想出一個遞歸的回溯方法
array,n:數組和數組大小
k:子數列大小
void output(int *array,int n,int k)
{
?? static int used[n];//這個靜態數組記錄是否該元素已經輸出了,初始化為0
?? static int outdata[k];//記錄已經輸出的元素
?? if k<=0 ,return;
?? 對于array的每一個未輸出的元素array[i]
??{
?? ? 將該元素放到outdata中,標記used[i]=1;
?? ? 如果滿k個了,則輸出outdata的數據
?? ? 否則,遞歸調用output(array,n,k-1)
?? ? 回溯,令used[i]=0;
??}
}
(2)或者寫一個Next函數用來計算當前排列的下一個排列
BOOL?Next(int?*data,int?nmax,int?k)
{
int?i;
#define?MyMax(i)?(nmax-k+i)
for?(i=k-1;i>=0?;i--)//從后向前找到第一個可以增加的數
{
ASSERT(data[i]>=0?&&?data[i]<=MyMax(i));
if(data[i]<MyMax(i))
{
break;
}
}
if(i>=0)//將該位++,后面各位遞增
{
data[i]++;
for?(int?j=i+1;j<k;j++)
{
data[j]=data[j-1]+1;
ASSERT(data[j]<=MyMax(j))
}
return?TRUE;
}
else
return?FALSE;
}
初始化data={0,...,k-1},再一直調用Next就可以得到所有的排列
發信人: scottfield (金蛇郎君), 信區: Algorithm
標 ?題: Re: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 20:18:14 2007), 站內
第三個,可以參考CLRS,可以線性時間求得,是weighted-select problem
把值看成權就行了。
發信人: scottfield (金蛇郎君), 信區: Algorithm
標 ?題: Re: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 20:24:24 2007), 站內
第四題,XOR是 00->1 11->1是不?
則只要找出兩個最高位相同的倍數最多的不就行了?
用Significant-bit Radix Sort
發信人: ttl (小驢|主ID), 信區: Algorithm
標 ?題: Re: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 20:36:27 2007), 站內
【 在 wlalbert (找個打我球的女朋友) 的大作中提到: 】
如何用c++實現?
【 在 cutepig (cutepig) 的大作中提到: 】
: 似乎真的是這樣呀
: 滿足條件的比這個數大的數應該是這個數從最低位開始,找到的第一個的01組合對調。
: 滿足條件的比這個數小的數應該是這個數從最低位開始,找到的第一個的10組合對調。
: ...................
void find(int i)
{
?? ? ? ?int f = 3; // 11
?? ? ? ?int pf = 2; // 10
?? ? ? ?int nf = 1; // 01
?? ? ? ?int p = 0; // 小
?? ? ? ?int n = 0; // 大
?? ? ? ?while (f < 4 * i)
?? ? ? ?{
?? ? ? ? ? ? ? ?int tmp = f & i;
?? ? ? ? ? ? ? ?if (!p)
?? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ?if (0 == (tmp ^ pf))
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?p = i ^ f;
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?if (!n)
?? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ?if (0 == (tmp ^ nf))
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?n = i ^ f;
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?f ?<<= 1;
?? ? ? ? ? ? ? ?pf <<= 1;
?? ? ? ? ? ? ? ?nf <<= 1;
?? ? ? ?}
?? ? ? ?cout << p << endl;
?? ? ? ?cout << n << endl;
}
發信人: zgx03 (時間旅客), 信區: Algorithm
標 ?題: Re: 求助:5道算法題
發信站: 水木社區 (Sat Nov 10 21:07:26 2007), 站內
第4題這么做,
設原數組為A,里面的元素取反后形成第二個數組B,把這兩個數組合起來形成數組C。
把C排一下序,找C的相鄰兩個分別屬于A和B且二進制最高幾位連續相同最多的,即可。
復雜度NlogN。
轉載于:https://www.cnblogs.com/cutepig/archive/2007/11/10/955539.html
總結
以上是生活随笔為你收集整理的Re: 求助:5道算法题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp.net实现在网页上自动显示超链接
- 下一篇: qq网名2017最新版女生