由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问个C++算法
相关主题
static initialization dependency c++关于C/C++里的Static variable的memory allocation/initializa
一道 memset in C++的题c++ initialize struct
c++ 不自动initialize变量么?Learn C++ 11 in 20 Minutes (视频)
抠字眼:assignment and initialize in C++pthread_create inside a constructor
码工试题 (转载)请教大家一道C的面试题
C++里面如何最方便的表示这个数组的数组?C++: Static initialization dependency
再问C++初始化问题。问个copy constructor的问题
static vector 怎么 initialize ?C++ 中 myobject * a =new myobject[n] 的问题
相关话题的讨论汇总
话题: crc话题: int话题: itemtype话题: xor话题: tmp
进入Programming版参与讨论
1 (共1页)
y**c
发帖数: 6307
1
我有一个程序计算CRC码,需要求任意2项或者以上的合并项,
比如,计算2项的合并:
void printCRCComb2(itemType *CRC, int n)
{
for(int i = 0; i < n; ++i){
for(int j = i+1; j < n; ++j){
itemType tmp = XOR(CRC[i],CRC[j]);
printCRC(&tmp,1);
}
}
}
}
但是如果计算3项,就有3个loop:
for(int i = 0; i < n; ++i){
for(int j = i+1; j < n; ++j){
itemType tmp1 = XOR(CRC[i],CRC[j]);
for(int k = j+1; k < n; ++k){
itemType tmp2 = XOR(tmp1,CRC[k]);
...
}
}
}
如果更多项,loop就更多了,容易出错,程序也不通用。有没有更好的办法?谢谢!
i***c
发帖数: 26
2
递归?
h*******u
发帖数: 15326
3
用一个vector存循环变量,可以做n维的

★ 发自iPhone App: ChineseWeb 7.8

【在 y**c 的大作中提到】
: 我有一个程序计算CRC码,需要求任意2项或者以上的合并项,
: 比如,计算2项的合并:
: void printCRCComb2(itemType *CRC, int n)
: {
: for(int i = 0; i < n; ++i){
: for(int j = i+1; j < n; ++j){
: itemType tmp = XOR(CRC[i],CRC[j]);
: printCRC(&tmp,1);
: }
: }

y**c
发帖数: 6307
4
嗯,这是一个办法。

【在 i***c 的大作中提到】
: 递归?
y**c
发帖数: 6307
5
具体一点?

【在 h*******u 的大作中提到】
: 用一个vector存循环变量,可以做n维的
:
: ★ 发自iPhone App: ChineseWeb 7.8

b*****e
发帖数: 474
6
The sketch:
int K = number of loop variables
Stack p = { 0, 1, 2..., K-1 };
// initialize i, j, k, ..., since the constraint is i // also declare necessary intermediate
// values v[] based on i,j,k values
while ( true ) {
do something with current content of p and v;
// print out p[], v[] for debugging
int t = n-1;
while (p.peek() == t-- )
{ p.pop(); /* also adjust v[p.size()-1] accordingly */ }
if ( p.empty() ) break; // done!
int c = ++p[p.size()-1]; // increment the stack top value
while (p.size() < K ) // fill up the rest of the stack
{ p.push(++c); /* also adjust v[p.size()-1] accordingly */ }
}
}

【在 y**c 的大作中提到】
: 我有一个程序计算CRC码,需要求任意2项或者以上的合并项,
: 比如,计算2项的合并:
: void printCRCComb2(itemType *CRC, int n)
: {
: for(int i = 0; i < n; ++i){
: for(int j = i+1; j < n; ++j){
: itemType tmp = XOR(CRC[i],CRC[j]);
: printCRC(&tmp,1);
: }
: }

O*******d
发帖数: 20343
7
如果有不确定的循环套循环,用递归比较好。

【在 y**c 的大作中提到】
: 我有一个程序计算CRC码,需要求任意2项或者以上的合并项,
: 比如,计算2项的合并:
: void printCRCComb2(itemType *CRC, int n)
: {
: for(int i = 0; i < n; ++i){
: for(int j = i+1; j < n; ++j){
: itemType tmp = XOR(CRC[i],CRC[j]);
: printCRC(&tmp,1);
: }
: }

w****6
发帖数: 796
8
does this work:
void printCRCCombs()
{
for(int ii=0; ii printCRCCombs_2(CRC,N,CRC[ii],ii);
}
}
void printCRCCombs_2(const itemType *CRC,int N,cont itemType &val,int idx)
{
for(int jj=idx+1; jj itemType tmp = XOR(val,CRC[jj]);
printCRC(&tmp,1);
printCRCCombs_2(CRC,N,tmp,jj);
}
}
1 (共1页)
进入Programming版参与讨论
相关主题
C++ 中 myobject * a =new myobject[n] 的问题码工试题 (转载)
Test your C++ knowledge...C++里面如何最方便的表示这个数组的数组?
difference between: char** p and char*p[] ??再问C++初始化问题。
question about const referencestatic vector 怎么 initialize ?
static initialization dependency c++关于C/C++里的Static variable的memory allocation/initializa
一道 memset in C++的题c++ initialize struct
c++ 不自动initialize变量么?Learn C++ 11 in 20 Minutes (视频)
抠字眼:assignment and initialize in C++pthread_create inside a constructor
相关话题的讨论汇总
话题: crc话题: int话题: itemtype话题: xor话题: tmp