你可以吧代码编译了 自己运行 看对头不 ~~~~
#include <stdio.h> int search_same(int arr[], int n); int main() { int arr[100]; int i; for(i=0; i<100; i++){ arr[i] = i; } arr[34] = 58; fprintf(stdout, "The same one:%d\n", search_same(arr, 100)); return 0; }上面缺失34,重复58,请写出你的算法找出重复的数58.
#include <vector> #include <string> #include <iostream> template <typename T> void LCS(const T& s1, const T& s2, T& s3) { const size_t len1 = s1.size(); const size_t len2 = s2.size(); std::vector< std::vector<unsigned int> > d(len1 + 1, std::vector<unsigned int>(len2 + 1)); std::vector< std::vector<unsigned int> > b(len1 + 1, std::vector<unsigned int>(len2 + 1)); const int w = 1; const int n = 2; const int nw = 3; for(int i = 1; i <= len1; ++i) { for(int j = 1; j <= len2; ++j) { if (s1[i-1] == s2[j-1]) { d[i][j] = d[i-1][j-1]+1; b[i][j] = nw; } else { if (d[i-1][j] >= d[i][j-1]) { d[i][j] = d[i-1][j]; b[i][j] = n; } else { d[i][j] = d[i][j-1]; b[i][j] = w; } } } } s3.resize(d[len1][len2]); int i=len1; int j=len2; int k=d[len1][len2]-1; while ( k>=0 && i>0 && j>0 ) { if ( b[i][j] == nw ) { s3[k--] = s1[i-1]; i -- ; j -- ; } else if (b[i][j] == n) i -- ; else if (b[i][j] == w) j --; else break; } } int main(int argc, char * argv[]) { std::string s1("adbccadebbca"); std::string s2("edabccadece"); std::string s3; LCS(s1, s2, s3); std::cout<< s3 <<std::endl; return 0; }
int search_same(int arr[], int n) { int i, xor = 0, a = 0, b = 0; for (i = 0; i < n; ++i) xor ^= arr[i] ^ i; xor -= xor & (xor - 1); for (i = 0; i < n; ++i) { (void)((i & xor) ? (a ^= i) : (b ^= i)); (void)((arr[i] & xor) ? (a ^= arr[i]) : (b ^= arr[i])); } /* notice that onluy one number in a and b is the dupicate one */ for (i = 0; i < n; ++i) if (arr[i] == a) return a; return b; }