J**********y 发帖数: 1891 | 1 class A
{
public:
int a;
};
class B
{
public:
int a;
B(int aa=0)
{
a=aa;
}
~B(){};
};
class C: public A, public B
{
//public:
// C(int cc):B(cc)
// {
// };
};
int _tmain(int argc, _TCHAR* argv[])
{
char aa[100]="0123456789";
cout << sizeof(func()) << endl;
cout << sizeof(int) << endl;
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
cout << sizeof(C) << endl;
A a;
B b;
C c;
cout << a.a << endl;
cout << b.a ... 阅读全帖 |
|
|
h**********c 发帖数: 4120 | 3 先前提到过 y = sin(a * pi /x), 对于质数a 在(1,a)是没有解得。
写了段程序,用牛顿法算 x, 跑了跑发现,原来这个一维系统有很多folds,又想了想
能不能用二维的系统来解这个问题呢?于是又了如下的构造
f = (f1,f2)^t
f1(x1,x2) = (x1 + exp(x2))* sin(pi * a /x1);
f2(x1,x2) = (x1*x1 -x2*x2)
求 f --〉0
这个系统看起来有个不错的jacobian,跑了跑程序,居然收敛了,但不知道收敛到哪里
去了,您有时间帮忙看看吧,能不能有更好的构造。或者用什么论证着干脆就是不可行
的,免得骑自行车去月球。
附程序c++
#include
#include
using namespace std;
#define PI (atan(1.0)*4.0)
#define __VERYSMALL (1.0E-10)
#define __ITERATIONLIMIT 20
bool isVerySmall (double d) {
return (a... 阅读全帖 |
|
h**********c 发帖数: 4120 | 4 先前提到过 y = sin(a * pi /x), 对于质数a 在(1,a)是没有解得。
写了段程序,用牛顿法算 x, 跑了跑发现,原来这个一维系统有很多folds,又想了想
能不能用二维的系统来解这个问题呢?于是又了如下的构造
f = (f1,f2)^t
f1(x1,x2) = (x1 + exp(x2))* sin(pi * a /x1);
f2(x1,x2) = (x1*x1 -x2*x2)
求 f --〉0
这个系统看起来有个不错的jacobian,跑了跑程序,居然收敛了,但不知道收敛到哪里
去了,您有时间帮忙看看吧,能不能有更好的构造。或者用什么论证着干脆就是不可行
的,免得骑自行车去月球。
附程序c++
#include
#include
using namespace std;
#define PI (atan(1.0)*4.0)
#define __VERYSMALL (1.0E-10)
#define __ITERATIONLIMIT 20
bool isVerySmall (double d) {
return (a... 阅读全帖 |
|
l*********y 发帖数: 142 | 5 #include
#include
#include
#include
#include
#include
#include
using namespace std;
class Counter {
public :
Counter() {
counter = 0;
}
void increment() {
counter++;
}
void decrement() {
counter --;
}
int getValue() {
return counter;
}
private:
int counter;
};
class COWString {
public:
COWString() {
pointer = NULL;
rc = new Counter();
}... 阅读全帖 |
|
C***U 发帖数: 2406 | 6 #include
#include
#include
#define MAX 10000
int main(int argc, char *argv[]){
std::vector numbers1;
std::vector numbers2;
int k = atoi(argv[3]);
if(argc < 4)
std::cout << "we need 2 files and a number." << std::endl;
std::cout << "we need find " << k << "th number" << std::endl;
std::cout << "reading first array of numbers ..." << std::endl;
std::ifstream f1(argv[1]);
if(!f1){
std::cout << "cannot open... 阅读全帖 |
|
z**********8 发帖数: 229 | 7 来自主题: JobHunting版 - 汉若塔问题 #include
using namespace std;
#include
#include
class Tower{
private:
int index;//indicate the number of tower
stack one_tower;
public:
Tower(int i=0):index(i){}//constructor
int getindex();
void add(int d);
void moveTopTo(Tower t);
void print();
void moveDisks(int n,Tower Destination,Tower buffer);
};
int Tower::getindex()
{
return index;
}
void Tower::add(int d)
{
if((!one_tower.empty())&&(d>one_tower.top()))
cout<<"... 阅读全帖 |
|
o********n 发帖数: 193 | 8 test case:
#include
void BinarySearch_Test()
{
int A1[1] = {4};
int A2[2] = {4, 5};
int A3[8] = {2,4,6,6,6,6,6,9};
int A4[8] = {2,4,6,6,8,8,8,9};
cout << FindInsertionPoint(A1, 1, 1) << endl;
cout << FindInsertionPoint(A1, 1, 10) << endl;
cout << FindInsertionPoint(A2, 2, 1) << endl;
cout << FindInsertionPoint(A2, 2, 10) << endl;
for (int i = 0; i <= 10; i++)
cout << i << "=" << FindInsertionPoint(A3, 8, i) << " ";
cout << endl;
for ... 阅读全帖 |
|
d**********x 发帖数: 4083 | 9 那题目貌似就没卡复杂度。。。枉我昨天下班后还捏着鼻子写了个treap。。。结果证
明是输出格式错了,只过了两三个case的可以考虑检查一下输出格式
思路大致就是用order statistics tree。每次查询都是O(logn)。手写最大问题在于没
法定制容器的行为。。。
好吧,然后想了一下,如果用两个heap加一个map的话也是可以的,因为只是查询
median,没有要求随机查询kth元素,所以还是写复杂了
附上代码,treap是个随机平衡树,可以拿去用。
#include
#include
#include
#include
using namespace std;
template
class Treap {
public:
class Node {
public:
Node(const T& value)
: value_(value),
left_(NULL),
right_(NULL),... 阅读全帖 |
|
c*******2 发帖数: 60 | 10 我也贴个我的, 直接计算出了循环节出现的位置, 因此是O(1)空间.
我是直接用cout输出的, 没有转成string.
int gcd(int i, int j)
{
return j ? gcd(j, i%j ) : i;
}
// Print N/D, e.g. 10/5 = 2.0, 2/4 = 0.5, 1/6 = 0.1(6)
void fracdec(int N, int D){
// Get rid of common divisor
int d1 = gcd(N, D);
N /= d1;
D /= d1;
int I = N/D; // Integer part
cout << I << '.';
if( N%D == 0){
cout << 0 << endl;
return;
}
// Compute multiplicity of 2 and 5 in D
int m2 = 0, m5 = 0, D1 = D;
while( D1%... 阅读全帖 |
|
f**********t 发帖数: 1001 | 11 // 一个deque,但是只支持pushBack,pushFront,popBack, 没有popFront
// 给一个1-N的排列,问能否把1-N按照从小到大的顺序push到deque,pop的时机可以
任选
// ,使得pop出的顺序刚好是给定的排列
// 比如: 给定 23145, 那么对12345对应的操作序列是pushBack,pushBack,
popBack,
// pushBack,popBack,popBack,pushBack,popBack,pushBack,popBack
// 要求如果可能,输出任意一个可能的操作序列,如果没有可能的操作序列,输出
// impossible
bool OpSequense(vector vi) {
deque dq;
size_t cur = 0;
vector indexes(vi.size() + 1);
for (size_t i = 0; i < vi.size(); ++i) {
indexes[vi[i]] = i;
}
for (int i =... 阅读全帖 |
|
y******g 发帖数: 4 | 12 #include
#include
#include
#include
using namespace std;
struct Item {
int counter;
char c;
Item(): counter(0), c('\0') {};
Item(int counter_, char c_) : counter(counter_), c(c_) {};
Item(const Item &anotherItem): counter(anotherItem.counter), c(
anotherItem.c) {};
};
struct ItemCompare {
Item* prev;
ItemCompare(Item* prevItem): prev(prevItem) {};
bool operator() (const Item &item1, const Item &item2) {
... 阅读全帖 |
|
g*********s 发帖数: 1782 | 13 class D_pro2: public D_pro {
public:
virtual void g() { cout << "D_pro2::g()\n"; }
void h() { cout << B_protected << endl; }
//void j() { cout << B_private << endl; }
void k() { cout << B_public << endl; }
};
class D_pri2: public D_pri {
virtual void g() { cout << "D_pri2::g()\n"; }
void h() { cout << B_protected << endl; }
//void j() { cout << B_private << endl; }
void k() { cout << B_public << endl; }
};
access_control.cpp: In member function ‘void D_pri2::h()’:... 阅读全帖 |
|
t******1 发帖数: 152 | 14 using namespace std;
#include
#include
int main(){
int *a,*b; //没有初始化的指针a和b
cout<<&a<<" "<<&b<
//分别为0x28ff3c和0x288ff38。注意二者之差为4,
//所以此环境为32位,即4Bytes
cout<
//是什么
//假设为0x100000和0x200000。
cout<<*a<<" "<<*b<
... 阅读全帖 |
|
m*********a 发帖数: 3299 | 15 const char i='x';
const char *cp=&i;
char *p;
p=const_cast(cp);
std::cout<<&i<<" "<
address
*p='y';
std::cout<<*p<
std::cout<<*cp<
std::cout<
同样的
char i='x';
const char *cp=&i;
char *p;
p=const_cast(cp);
std::cout<<&i<<" "<阅读全帖 |
|
s******t 发帖数: 150 | 16 ifstream input("story.txt");
vector words;
cout << words.size() << endl;
if(!input)
cerr << "Could not open story.txt!\n";
else
{
string line;
while(getline(input,line))
{
// get single word from each line
istringstream stream(line);
string word;
while(stream >> word)
words.push_back(word);
}
}
//close file
input.close();
cout << "Before sort(), ... 阅读全帖 |
|
w****g 发帖数: 206 | 17 运行一下 程序:
int main() {
const int i = 0;
int * j;
cout << "&j=" << &j << endl;
cout << "&i=" << &i << endl;
cout << "j=" << j << endl;
j = const_cast(&i);
*j = 2;
cout << "i=" << i << endl;
cout << "*j=" << *j << endl;
cout << "j=" << j << endl;
cout << "&i=" <<&i <
}
得到结果:
&j=0xbf93a658
&i=0xbf93a65c
j=0
i=0
*j=2
j=0xbf93a65c
&i=0xbf93a65c
这就是说:j 和&i是一样的,但是为什么i和*j不一样呢? |
|
j*******g 发帖数: 4 | 18 见过很多次这个题目了,一直没有招,下面写了一个又臭又长的, 方法很笨, 求建议
, 欢迎批评和指正
这样的代码面试可以通过吗?
////////////////////////////////////////////////////////////////
#include
#include
using namespace std;
//假设16位的整型
// -32768 , +32767
const char MAX_INT[] = "32767";
const char MIN_INT[] = "32768";
const int MAX_STRLEN = 5;
bool my_atoi(const char *str, int &res)
{
if(str == NULL)
{
cout << "Invalid pointer" << endl;
return false;
}
int index = 0;
if(str[index] == '-' || s... 阅读全帖 |
|
j*******g 发帖数: 4 | 19 见过很多次这个题目了,一直没有招,下面写了一个又臭又长的, 方法很笨, 求建议
, 欢迎批评和指正
这样的代码面试可以通过吗?
////////////////////////////////////////////////////////////////
#include
#include
using namespace std;
//假设16位的整型
// -32768 , +32767
const char MAX_INT[] = "32767";
const char MIN_INT[] = "32768";
const int MAX_STRLEN = 5;
bool my_atoi(const char *str, int &res)
{
if(str == NULL)
{
cout << "Invalid pointer" << endl;
return false;
}
int index = 0;
if(str[index] == '-' ||... 阅读全帖 |
|
d****n 发帖数: 1637 | 20 //in windows
#include
#include
using namespace std;
class A{
public :
int a;
int b;
};
int main()
{
cout<<&main<
int before=4;
cout<<"STACK &before="<<&before<
A objA;
void *p=malloc(1000);
cout<<"HEAP *p="<
cout<<"&objA.a"<<&(objA.a)<
cout<<"&objA.b"<<&(objA.b)<
cout<<"objA="<<&objA<
int after=4;
cout<<"STACK &after="<<&after<
return 0;
}
//output from windows wi... 阅读全帖 |
|
b****f 发帖数: 138 | 21 前面的评论没看,i不回溯的确过不了,这个暴力法都可以过,但貌似超时了,感觉现
在只能用KMP了
class Solution {
public:
bool isMultiple (string s){
int N=s.size();
if(N<3) return false;
int len=1;
int start=0;
for(int i=1;i
if(s[start]==s[i]){
start++;
if(start>=len) start=0;
}
else{
len++;
i=len-1;
start=0;
}
if(len>N/2) return false;
... 阅读全帖 |
|
b****f 发帖数: 138 | 22 前面的评论没看,i不回溯的确过不了,这个暴力法都可以过,但貌似超时了,感觉现
在只能用KMP了
class Solution {
public:
bool isMultiple (string s){
int N=s.size();
if(N<3) return false;
int len=1;
int start=0;
for(int i=1;i
if(s[start]==s[i]){
start++;
if(start>=len) start=0;
}
else{
len++;
i=len-1;
start=0;
}
if(len>N/2) return false;
... 阅读全帖 |
|
y*****i 发帖数: 141 | 23 照着Maxthon的编码解法写了一个:
#include
#include
#include
#include
using namespace std;
bool bool_vec_none(const vector & vec) {
for(auto elem : vec) {
if (elem) {
return false;
}
}
return true;
}
bool bool_vec_all(const vector & vec) {
for(auto elem : vec) {
if (!elem) {
return false;
}
}
return true;
}
string word_abbreviation(string target, vector dict)
{
if (t... 阅读全帖 |
|
b******i 发帖数: 914 | 24 大体思路就是记录下所有当前是反括号但是没有正括号pop出来的,和最后剩下的正括号
class Solution {
public:
string balance(string s) {
int n = s.length();
if(n == 0)
return 0;
vector stk;
vector to_remove(n, false);
for(int i = 0; i < n; i++) {
char c = s[i];
if(c == '(') {
stk.push_back(i);
} else {
if(stk.empty())
to_remove[i] = true;
else {
stk.p... 阅读全帖 |
|
g*****n 发帖数: 56 | 25 #include
using namespace std;
class A {
public:
A() {
cout << "A::A()" << endl;
}
~A() {
cout << "A::~A()" << endl; throw "A::exception";
}
};
class B {
public:
B() {
cout << "B::B()" << endl; throw "B::exception";
}
~B() {
cout << "B::~B()";
}
};
int main(int, char**) {
try {
cout << "Entering try...catch block" << endl;
A objectA;
B objectB;
cout << "Exiting try...catch block" << endl;
} catch (const char* ex) {
cout << ex << endl;
}
return 0;
}
output:
A::A();
B::B();
B::~B();
Abor |
|
r*******y 发帖数: 1081 | 26 The code below is OK
//4.cpp
#include
using namespace std;
class M{
int i;
public:
M(){cout << "default constructor" << endl;}
M(int ii):i(ii){cout << "constructor"<< endl;}
M(const M & mm):i(mm.i){cout << "copy constructor"<
};
int main(){
M mm = 1;
}
If I run the program, output is
constructor
But If I changed the copy constructor as
M(M & mm):i(mm.i){cout << "copy constructor"<
that is, the parameter is M & instead of const M&
N... 阅读全帖 |
|
f******5 发帖数: 11 | 27 Thanks so much.
The compiler I am using is Dev c++ 4.9.9.2
Can I use New operator inside LCS like follows? It shows more compiling time
error?
Or should I declare c[][] outside the helping function LCS or inside Main()
function?
I want to pass the arrayc[][] and b[][] at the end of LCS to another Helping
function called
Print_LCS.
From your point of view, where should I declare the c[][] and b[][]
If i use New operator as follows to declare and initialize c[][], c[][],
both c[][] and b[][] go ou... 阅读全帖 |
|
x******a 发帖数: 6336 | 28 为什么下面这个程序输出的时候多一个1出来:
enter the number of element
5
4 0 3 0 4 1
the maximum element is 4
0 0 3 4 4
程序在这
#include
#include
#include
using namespace std;
void myfunction(int i)
{
cout << i<<" ";
}
int main()
{
int N;
cout<< "enter the number of element" <
cin>> N;
int a[N];
srand((unsigned) time(0));
for(int i=0; i
a[i]=rand()%N;
cout<
cout<< endl;
cout<<"the maximum element is " << *max_element(a, a+N)... 阅读全帖 |
|
EM 发帖数: 715 | 29 在parent process里面建立一个{1,2,3}的set,然后在child process里面打印出来,编
译可以通过,但是为什么会有segmentation fault呢?读了大半天boost的东东也没搞
明白,请大侠指导一下
程序如下,编译如右:g++ -std=c++0x src.cc -lrt
#include
#include
#include
#include //std::system
#include
#include
#include
using namespace boost::interprocess;
//Main function. For parent process argc == 1, for child process argc == 2
int main(int argc, char **argv)
{
if(argc == 1... 阅读全帖 |
|
t**r 发帖数: 3428 | 30 你必須去改那個類,
for your reference:
#include
#include
struct A {
std::string s;
A() : s("test") {}
A(const A& o) : s(o.s) { std::cout << "move failed!n";}
A(A&& o) : s(std::move(o.s)) {}
};
A f(A a) {
return a;
}
struct B : A {
std::string s2;
int n;
// implicit move contructor B::(B&&)
// calls A's move constructor
// calls s2's move constructor
// and makes a bitwise copy of n
};
struct C : B {
~C() {}; // destructor prevents ... 阅读全帖 |
|
a****s 发帖数: 2060 | 31 转
#include
#include
#include
#include
void main()
{
int a=0,b=0,c=0;
int A=0,B=0,C=0;
int num=0;
while(1)
{
while(A+b==3 && B+a==1 && A+c==6 && C+a==4 && C+b==7)
{
cout<
cout<
cout<
cout<
cout<
cout<
return;
}
a=rand()%10;
b=rand()%10;
c=rand()%10;
A=rand()%10;
B=rand()%10;
C=rand()%10;
}
} |
|
s*****y 发帖数: 897 | 32 How about this?
print(matrix1, m, n, 0, m-1, 0, n-1);
void print(int matrix[][6], int m, int n, int up, int down, int left, int
right)
{
if ((up < 0) || (down >= m) || (left < 0) || (right >= n))
return;
if (up == down) {
for (int i = left; i <= right; i++)
cout << matrix[up][i] << " " ;
return;
}
if (left == right) {
for (int i = up; i <= down; i++)
cout << matrix[i][left] << " " ;
return;
}
//print left to ... 阅读全帖 |
|
d*******l 发帖数: 338 | 33 不是很麻烦吧。
#include
#include
#include
using namespace std;
#define MP make_pair
typedef pair PII;
typedef vector VI;
struct FindVertex
{
struct rect
{
int x1, y1, x2, y2;
rect(int a, int b, int c, int d) : x1(a), y1(b), x2(c), y2(d) {}
};
vector rects;
void add(int a, int b, int c, int d)
{
rect r(a, b, c, d);
rects.push_back(r);
}
void solve()
{
int i, j;
VI v;
... 阅读全帖 |
|
E*****7 发帖数: 128 | 34 Algorithm:
Store each input into std::vector v;
Compare its first (i=0) and last element(j=v.size()-1)
if (v[i] != v[j]) return false;
Compare its 2nd and 2nd to last element
if (v[i+1] != v[j-1]) return false;
until i meets j
======= C++ Code (efficient and O(N) =========
#include
#include
bool isPalindrome(const std::vector &v) {
size_t len = v.size();
if ( len < 2 )
return false;
int i = 0;
int j = len - 1;
while ( i < j ) {
... 阅读全帖 |
|
K*******i 发帖数: 399 | 35 const int M = 4;
const int N = 5;
// si: row of the upper left element of the sub matrix
// sj: col of the upper left elment of the sub matrix
// m: rows of the current sub matrix
// n: cols of the current sub matrix
void PrintMatrix(int A[M][N], int si, int sj, int m, int n)
{
if (m == 0 || n == 0)
return;
if (m == 1)
{
for (int j = 0; j < n; j++)
cout << A[si + 0][sj + j] << " ";
}
else if (n == 1)
{
for (int i = 0; i < m; i+... 阅读全帖 |
|
g*******s 发帖数: 2963 | 36 这个行不?constructor里我pass了container(这里是vector),因为我不知道没有
container该怎么写hasNext()? 如果假设原始iterator已经有hasNext()和next()了,
那这俩函数应该不用改吧?看成black box就行了,直接在peek里引入个temp变量就行
了。
#include
#include
using namespace std;
template
class Wrapper {
public:
Wrapper(vector &v,
typename vector::iterator it)
:_vec(v),_it(it),_tempIt(it){
_it = _vec.begin();
};
bool hasNext(){
_tempIt = _it;
return (++_tempIt)!=_vec.end();
};
... 阅读全帖 |
|
c**********e 发帖数: 2007 | 37 这个Strategy design pattern的例子为什么人为得弄得这么复杂?
#include
#include
#include
using namespace std;
class Strategy;
class TestBed
{
public:
enum StrategyType
{
Dummy, Left, Right, Center
};
TestBed()
{
strategy_ = NULL;
}
void setStrategy(int type, int width);
void doIt();
private:
Strategy *strategy_;
};
class Strategy
{
public:
Strategy(int width): width_(width){}
void format()
{
char line[80], wo... 阅读全帖 |
|
w****x 发帖数: 2483 | 38
//Spirally print M*N array
const int M = 5;
const int N = 3;
void SpiralPrint2(int A[M][N])
{
int nX = 0;
int nY = 0;
int nLenth = N;
int nHeight = M;
//nothing to say, completely swapped the row and col ++ operation
while (nLenth > 0 && nHeight > 0)
{
//set tarting point
int nIterX = nX;
int nIterY = nY;
//when length or height is equal to 1, needs special care
if (nLenth == 1)
{
for (int i = 0; i < nHeight... 阅读全帖 |
|
d**e 发帖数: 6098 | 39 ☆─────────────────────────────────────☆
oneid (Mobius) 于 (Fri Dec 28 19:12:49 2012, 美东) 提到:
比如careercup,glassdoor上每个公司那么多题,难道大家都做完了?
还是leetcode has OJ?比较容易发现code的问题?
但是大家觉得leetcode的题被问得几率比careercup上针对性的公司的题概率高么?不可
能把?
☆─────────────────────────────────────☆
luckynoob (菜鸟) 于 (Fri Dec 28 19:20:06 2012, 美东) 提到:
题目肯定是做不完的,重要的是实现一下一些重要方法吧,书还是要看的
☆─────────────────────────────────────☆
lolhaha (人生如棋,棋如人生) 于 (Fri Dec 28 19:21:35 2012, 美东) 提到:
careecup上的题你做得完吗?
那上面的很多回帖都有问题,没有正确答案
☆─────... 阅读全帖 |
|
s*w 发帖数: 729 | 40 要有人觉得有所帮助,给发个包子
【 以下文字转载自 Programming 讨论区 】
发信人: saw (句子熊), 信区: Programming
标 题: Re: 一道count frequency of all words的面试题 (转载)
发信站: BBS 未名空间站 (Mon Sep 23 00:28:12 2013, 美东)
又琢磨了两天,看了不少相关东西,终于搞定了,觉得自己对这个多线程的理解加强了
很多。思考比单纯的看人说原理更刻骨铭心。
这个问题我设计的用一个 producer 多个 consumer 的架构,上个版本的是两头用
condition_variable, 一个通知 producer 有空间了开始干活生产,另一个通知
consumer 有库存了开始消费。参见上篇里面的 wait 和 notify_all,notify_one 语
句。 这个思路对于单 producer 单 consumer 没问题,可是不适用于 多 consumer.
因为所有的 consumer 可能同时睡觉(没空存),同时醒来(有库存),结果只有一个
能抢占mutex(拿到库存),... 阅读全帖 |
|
c*******y 发帖数: 98 | 41 DP好难,我来贴个无脑的,不知道题目理解对了没有。。
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int findMaxAllOneTopLeftSquare(vector>& matrix)
{
int i, j;
int m = matrix.size(); if (!m) return 0;
int n = matrix[0].size(); if (!n) return 0;
int max = min(m, n);
for (i=0; i
for (j=0; j<=i; j++){
if (matri... 阅读全帖 |
|
f**********t 发帖数: 1001 | 42 // Given a nested list of integers, returns the sum of all integers in the
list
// weighted by their depth. For example, given the list {{1,1},2,{1,1}} the
// function should return 10 (four 1's at depth 2, one *2 at depth 1). Given
// the list {1,{4,{6}}} the function should return 27 (one 1 at depth 1, one
4
// at depth 2, and *one 6 at depth 3)
int getNum(string::iterator &i, string::iterator end) {
if (i == end) {
return 0;
}
bool neg = false;
if (*i == '-') {
neg = true;
... 阅读全帖 |
|
y******g 发帖数: 4 | 43 #include
#include
using namespace std;
void print(const vector &A) {
int count = 0;
cout << "[";
for(auto &i : A) {
if (count++) {
cout << ", ";
}
cout << i;
}
cout << "]" << endl;
};
void zigzag(vector &A) {
if (A.empty()) return;
for (size_t i = 1; i < A.size(); ++i) {
if (i&0x01) { // odd position element should be a local maximum
if (A[i] <= A[i-1]) {
swap(A[i-1],... 阅读全帖 |
|
b******i 发帖数: 914 | 44 贴个code,不过只test了几种case
/*
给一个list of string 和一个string s, 返回s是不是在list当中, s里可以包含一
个*代表一个任意字符。
*/
#include
#include
#include
using namespace std;
struct TrieNode {
TrieNode():is_word(false),children(26,nullptr) {}
bool is_word;
vector children;
};
class Solution {
public:
bool string_in_list(vector strings, string s) {
if(s.length() == 0 || strings.size() == 0)
return false;
// construct trie
... 阅读全帖 |
|
e**y 发帖数: 784 | 45 格式都坏了,凑合看吧
这是airbnb的电面题
// [123,456,[788,799,833],[[]],10,[]]
// 123->456->788(list)->[](list)->10->(list)
// 799
// 833
class nestedList {
private:
bool isNumber;
list l;
int val;
public:
nestedList(string, int&);
void print();
};
nestedList::nestedList(string s, int& index) {
if(index==s.length()) return;
// object is a number
if(isdigit(s[index])) {
size_t sz;
val=stoi(s.substr(index), &sz);
// cout << "index==" << index << endl;
// cout << "value==" << val << endl;
isNumber=true;... 阅读全帖 |
|
b*****l 发帖数: 9499 | 46 是啊,还是老样子。能麻烦你在你的机子上跑一下不?
$ g++ -fopenmp -c TestOMP.cpp
$ g++ -o TestOMP TestOMP.o -I. -g -O -fopenmp -lm
$ ./TestOMP
omp_get_dynamic: 1
omp_get_max_threads: 48
Fork!
Hello World from thread = 0 of 1
Master thread: number of threads = 1
Joint!
$ less TestOMP.cpp
#include
#include
using namespace std;
main () {
int nthreads;
omp_set_dynamic(1);
cout << "omp_get_dynamic: " << omp_get_dynamic() << endl;
cout << "omp_get_max_threads: " << omp_get_max_threads() << endl;
... 阅读全帖 |
|
e***a 发帖数: 18 | 47 should be:
A[0][0] = 2; A[0][1] = 4; A[0][2] = 1;
A[1][0] = 5; A[1][1] = 3; A[1][2] = 7;
A[2][0] = 0; A[2][1] = 3; A[2][2] = 2;
for ( int i = 0; i < num; i++)
cout << A[0][i] << ' ';
cout << endl;
for ( int i = 0; i < num; i++)
cout << A[1][i] << ' ';
cout << endl;
for ( int i = 0; i < num; i++)
cout << A[2][i] << ' ';
cout << endl; |
|
c**y 发帖数: 172 | 48 Anyone can explain why the second output is 0, rather than 1.
========================================
#include
#include
#include
using namespace std;
int main() {
const int x = 0;
int const *py = &x;
int *px = const_cast(&x);
cout << *px << endl; // output 0
*px = 1;
cout << x << endl; // output 0, I don't understand
cout << *px << endl; // output 1
cout << *py << endl; ... 阅读全帖 |
|
m****s 发帖数: 1481 | 49 还是有点问题,大侠帮忙看下下面的代码,为什么在main里释放内存出错。
用pass by reference:
void fun1(int& *a, double& *b, char& *c){
a=new int[20];
b=new double[20];
c=new char[10];
for (int i=0;i<20;i++){
a[i]=i;
b[i]=sqrt((double)a[i]);
}
c="0123456789";
}
int _tmain(int argc, _TCHAR* argv[])
{
int *a;
double *b;
char *c;
fun1(a,b,c);
cout<<"a & b are:"<
for(int i=0;i<20;i++)
cout<
cout<<"c is: "<
delete[] ... 阅读全帖 |
|
l*********s 发帖数: 5409 | 50 MSVC老是抱怨没有《右边是TEntry类型的重载,但是明明一开始就重载了啊,到底怎么
回事请大家看看。
(这段代码来自于Think in C++)
#include
#include |
|