m****n 发帖数: 145 | 1 恭喜lz先,感觉这个Design题是典型的Strategy模式的应用来实现控制反转。我C++不熟,用C#写了一
下代码,欢迎大家批评指正,然后给几个包子。呵呵,本人新ID,穷呀。。
(如果还要扩展的话,还可以用工厂模式来构建具体的Strategy类,对于命令行模式可能是overkill了,呵呵)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FileProcessor
{
class Program
{
static void Main(string[] args)
{
if (args.Count() != 3)
{
System.Console.WriteLine("The correct format should be:\n
Program filename operation_type output_for |
|
c**********e 发帖数: 2007 | 2 #include
using namespace std;
const int size = 5;
void print(int *ptr)
{
cout << ptr[0] << endl;
}
void main()
{
int *b = new int(size);
print(b);
}
~ |
|
c**********e 发帖数: 2007 | 3 The following code is from a book. But it does not work.
I have not been able to make it work. Anybody look at it?
________________________________
#include
#include
using namespace std;
int maxSub(int a[], int n, int& i, int& j) {
int t=a[0], vmax = a[0];
int tmin = min(0, t);
for(int k=1; k
{
t+=a[k];
if(t-tmin > vmax) { vmax = t-tmin; j=k; }
if(t < tmin) { tmin =t; i=(k+1
}
return vmax;
}
int main() { |
|
P***y 发帖数: 2885 | 4 问在堆上分配内存的问题。问详细过程,小弟一问三不知。只知道用new operator.有个
问题是如果Constructor失败怎么办,会不会有内存泄漏什么的。后来才知道调用new在
堆上分配的时候,先调用operator new去拿到一块raw memory,然后在此内存上调用Con
structor,但问题是这个时候Constructor throw exception,然后就Fail了。。。。
问题就在这儿,由于前面用operator new分配了一块的raw memory,这时候有没有内存泄
漏???
回来以后自己找了个程序来调试,故意让它Constructor不成功。。。但是依然没有看出
那块事先分配的raw memory是怎样回收回去的。。。
各位有空的话帮我看看,我把我自己的源程序给贴上来。。。
#include "stdafx.h"
#include
using namespace std;
///// class Counted
template
class Counted {
public:
clas |
|
c*******d 发帖数: 255 | 5 贴一下递归的程序:
#include
using namespace std;
void printspiral(char *a, int colsize, int m, int n, int x0, int y0)
{
// a is the matrix, colsize is its original column size
// m is #rows, n is #cols in the submatrix
// x0 and y0 are the offsets of the first element of the submatrix
// check if m and n are positive
if (m<=0 || n<=0) {
cout << "zero or negative dimensions" << endl;
return;
}
|
|
B*****t 发帖数: 335 | 6 There are 3 memory storage types for c++ to allocate memory for an object.
1. stack memory for object inside a function.
2. static storage for namespace-scope objects and local static objects
3. heap storage for dynamically-allocated objects.
If the stack in you problem refers to the first type of memory storage,
there is only one method that you could do this, which is defining a
function, allocating memory for an object inside a function, and using it.
You cannot use it outside of a function. |
|
c*******d 发帖数: 255 | 7 36 = 2^2*3^2
两个2分给三个人,有C(4,2)种分法
C(4,2)*C(4,2) = 36种情况
所以程序可以这么写:
#include
#include
using namespace std;
int main()
{
unsigned int k1, k2, k3, s1, s2, s3, n1, n2, n3;
unsigned int count = 0;
unsigned int i, j, p, q;
for(i=1;i<=4;i++) {
for(j=i+1;j<=4; j++) {
k1 = i-1;
k2 = j-(i+1);
k3 = 4-j;
for(p=1; p<=4; p++) {
for(q=p+1; q<=4; q++) {
s1 = p-1;
s2 = q-(p+1);
s3 = 4-q;
n1 = pow(2,k1)*pow(3,s1 |
|
t******e 发帖数: 1293 | 8 测试了前面8个都没有问题, O(n)时间和空间,可以改为O(1)空间
#include
#include
#include |
|
b******n 发帖数: 823 | 9 写了个用string的
#include
#include
int main(int argc, char* argv[])
{
using namespace std;
cout << 1 << endl;
string s1, s2;
s1.push_back('1');
s1.push_back('1');
int loopcount = 0;
while(loopcount++ < 10)
{
cout << s1 << endl;
s1.push_back('a');
int i;
for (i=0; i
{
int dupsize = 1;
while(s1[i] == s1[i+1])
{
dupsize++;
i++ |
|
t******e 发帖数: 1293 | 10 #include
using namespace std;
int f(int n, int k)
{
if (n == 1)
return 0;
return (f(n-1, k) + k)%n;
}
int main()
{
for (int i = 1; i < 12; i++)
cout << f(i, 5) << endl;
} |
|
o***e 发帖数: 497 | 11 #include
using namespace std;
int reverse(const int input, int &output)
{
if (input < INT_MIN || input > INT_MAX) {
output = 0;
return -1;
}
cout <<"input:" << input << endl;
int absolute = abs(input);
int ispositive = (input < 0) ? -1 : 1;
while ( absolute > 10) {
int cur = output + (absolute % 10);
absolute /= 10;
if((ispositive == 1) && ( (cur == INT_MAX / 10 && absolute > INT_MAX
% 10) || (cur > INT_MAX / 10)) ) {
|
|
f*******5 发帖数: 52 | 12 .c文件里可以调用STL,需要using namespace std,用g++ |
|
t***e 发帖数: 446 | 13 what about this?
#include
#include
using namespace std;
unsigned int CountWord(const char* ch)
{
unsigned int cnt=0;
bool flag=true; /*check for consecutive spaces*/
if (ch==NULL)
return cnt;
for (size_t i=0; ch[i]!='\0'; ++i)
{
if (ch[i]==' ' && flag==false)
{++cnt;flag=true;}
if (ch[i]!=' ')
flag=false;
}
if (flag==false)
++cnt;
return cnt;
}
int main()
{
cout< |
|
l*******y 发帖数: 1498 | 14 If you inherit a base class as virtual, only one subobject of that class
will ever appear as a base class. 下面这个例子:
#include
#include
using namespace std;
class MBase {
public:
int i_base;
virtual char* vf() const = 0;
virtual ~MBase() {}
};
class D1 : virtual public MBase {
public:
char* vf() const { return "D1"; }
};
class D2 : virtual public MBase {
public:
char* vf() const { return "D2"; }
};
// MUST explicitly disambiguate vf():
class MI : public D1, public D2 { |
|
I**********s 发帖数: 441 | 15 第一题的完整代码如下.
http://www.informatik.uni-ulm.de/acm/Locals/2003/html/judge.html
Problem H, solution 4. O(n).
版主给个包子吧.
#include
#include
using namespace std;
void getMax(int hist[], stack * s, int right, int & max) {
int i = s->top();
int height = hist[i];
s->pop();
int left = (s->size() > 0) ? s->top() : -1;
int area = height * (right - left);
if (area > max) max = area;
}
void doHist(int hist[], int len) {
stack * s = new stack;
int i, top_v, max = |
|
I**********s 发帖数: 441 | 16 好吧, 试试这个 (可以有元素是0):
#include
#include
using namespace std;
int DEBUG = 0;
void getMax(int hist[], stack * s, int newHeight, int right, int & max,
int & start) {
int height, left = 0, area;
while (s->size() > 0 && hist[s->top()] > newHeight) {
height = hist[s->top()];
s->pop();
left = (s->size() > 0) ? s->top() : start;
while (s->size() > 0 && hist[s->top()] == height) {
s->pop();
left = (s->size() > 0) ? s->top() : start;
}
area = h |
|
l******o 发帖数: 144 | 17 随便写了一个,懒得去测试了。
#include
#include
#include
#include
#include
using namespace std;
template
struct Segment
{
T left;
T right;
};
template
struct LeftEndComp
{
bool operator () (const Segment& one, const Segment& two) const
{
return one.left < two.left || (!(two.left < one.left) && one.right <
two.right);
}
};
template
T coverage(const vector >& segments)
{
if(se |
|
p**********s 发帖数: 115 | 18 我来解typedef那题:
#include
using namespace std;
void staticFun(int num) {
cout << "static function" << endl;
}
class foo{
public:
void classFun(int num) {
cout << "function in a class" << endl;
}
};
typedef void (*myStaticFun) (int);
typedef void (foo::*myClassFun) (int);
int main()
{
myStaticFun testStaticFun = staticFun;
testStaticFun(6);
foo foo1;
myClassFun testClassFun = &foo::classFun;
(foo1.*testClassFun)(6);
return 0;
} |
|
i**********e 发帖数: 1145 | 19 这题我有做过,请问lz申的是加州的游戏公司吗?
我的方法很简单,实现也比较简单写,比较容易写成bug free。
首先把拼图放在2D array的中间,然后每次加入新的拼图在有可能的四方形地区里进行
搜索。
以下是我写的代码。
#include
using namespace std;
void rotate(char* tile)
{
char first = tile[0];
strcpy(tile, tile + 1);
tile[3] = first;
tile[4] = '\0';
}
const int MAP_SZ = 1024;
char map[MAP_SZ][MAP_SZ];
void printMap(int x1, int y1, int x2, int y2)
{
for (int j = y1; j <= y2; j++)
{
for (int i = x1; i <= x2; i++)
{
cout << map[i][j];
|
|
c********g 发帖数: 13 | 20 这个应该可以
#define nums 8
#include
using namespace std;
int main()
{
int max[10];
int pre[10];
int num[100]={50,23,1,67,30,40,45,49};
max[0]=num[0];
pre[0]=0;
for(int i=1;i
{
int temp=0;
max[i]=num[i];
pre[i]=i;
for(int j=0;j
{
if(num[i]>num[j])
{
if(max[j]>=temp)
{
temp=max[j];
|
|
l*********y 发帖数: 142 | 21 贴一个按svncheckout的idea实现的c++. Tested.
#include
using namespace std;
int GasStation(int* d, int* g, int size)
{
int i = 0;
int* c = new int[2*size];
for (i = 0; i < 2 * size; i++) {
if (i < size) {
c[i] = g[i] - d[i];
} else {
c[i] = c[i-size];
}
}
int result = -INT_MAX;
int sum = 0;
int start = 0, j = 0;
for (i = 0; i < 2* size; i++) {
sum = sum + c[i];
if (sum >= 0) {
j++;
|
|
b******v 发帖数: 1493 | 22 #include
#include
#include
#include
using namespace std;
string findbin(unsigned int x)
{
string result;
char tmp[2];
tmp[1]='\0';
if(x==0)
return string("0");
while(x!=0) {
int r = x%2;
tmp[0] = r+48;
result = string(tmp) + result;
x = (x-r)/2;
}
return result;
}
int find012(string& s, set& result)
{
|
|
b******v 发帖数: 1493 | 23 改进了一下,不用queue,全用set,减少了重复运算
#include
#include
#include
using namespace std;
string findbin(unsigned int x)
{
string result;
char tmp[2];
tmp[1]='\0';
if(x==0)
return string("0");
while(x!=0) {
int r = x%2;
tmp[0] = r+48;
result = string(tmp) + result;
x = (x-r)/2;
}
return result;
}
int find012(string& s, set& re |
|
B*****t 发帖数: 335 | 24 写了个程序, x=8327168时, 我的老破机器0秒就可以出结果了
#include
#include
#include
#include
using namespace std;
int x = 8327168, k;
int d[1000];
char buf[100000];
inline bool check(int val, int ix) {
int i;
for(i=0; i<=ix; i++){
if(val%2==1&&d[i]==2) return false;
val >>=1;
}
return true;
}
void dfsFind21Str(int ix, int tot) {
if(tot>x) return;
if(ix==k) {
int i, reminder = 0;
i = 0;
reminder = x;
while(remind |
|
a***9 发帖数: 364 | 25 呵呵,也写了一个,也是0秒
#include
#include
using namespace std;
int counter= 1;
int end = 0;
void bin21(string str, int start)
{
if(start >= end)
return;
if (start < 0) start = 0;
while((str[start] == '0') && (start < end)) start++;
while((str[start+1] != '0') && (start < end)) start++;
if((str[start +1] == '0') && (start < end))
{
if(str[start] == '1')
{
str[start] = '0';
str[start + 1] = '2';
++counter; |
|
t****t 发帖数: 6806 | 26 你这个思路跟我的类似, 不过我是从大向小搜索的, 不需要reverse string, 但是没有
利用最后一位是0,2/1的性质
顺手写了一个
#include
#include
using namespace std;
void FindAll210(int num, char res[], int index, int weight)
{
if (weight==0 && num==0) {
cout<
return;
}
if (num>(2*weight-1)*2) return;
res[index]='0';
FindAll210(num, res, index+1, weight>>1);
num-=weight;
if (num<0) return;
res[index]='1';
FindAll210(num, res, index+1, weight>>1);
num-=weight;
if (n |
|
B*****t 发帖数: 335 | 27 代码才是硬道理! 贴个我的code,可能有bug,欢迎指出。另外,DFS没有优化。
#include
#include
using namespace std;
const int N = 10000, MaxTimeValue = 100000;
class Job {
public:
int st, ed, money;
}nd[N];
int n, maxV = 0x80000000;
bool cmp(const Job &a, const Job &b) {
return a.st
}
void dfs(int ix, int endTime, int tot) {
if(ix==n) {
if(tot>maxV) maxV = tot;
return;
}
if(nd[ix].st>=endTime) dfs(ix+1, max(nd[ix].ed,
endTime), tot+nd[ix].mone |
|
B*****t 发帖数: 335 | 28 不解释了, 给你个code参考一下就明白了
#include
using namespace std;
int main() {
const int ANumNotInArray = 0x7fffffff;
class Node {
public:
int x, cnt;
}d[3];
int m[] = {3, 2, 6, 3, 2, 1, 6, 3, 6, 2, 7};
int n, i, j;
n = sizeof(m)/sizeof(int);
bool found = false;
for(i=0; i<3; i++) {
d[i].x = ANumNotInArray;
d[i].cnt = 0;
}
for(i=0; i
for(j=0; j<3; j++) {
if(m[i]==d[j].x) {
d[j].cnt++; f |
|
B*****t 发帖数: 335 | 29 又改了一下,你看看还有没有bug
#include
using namespace std;
int main() {
const int ANumNotInArray = 0x7fffffff;
class Node {
public:
int x, cnt;
}d[3];
int m[] = {3,3,3,7,2,2,6,1,6,2,6};
int n, i, j, ix;
n = sizeof(m)/sizeof(int);
bool found = false;
for(i=0; i<3; i++) {
d[i].x = ANumNotInArray;
d[i].cnt = 0;
}
for(i=0; i
for(j=0; j<3; j++) {
if(m[i]==d[j].x) {
d[j].cnt++; found = true |
|
h**6 发帖数: 4160 | 30 写了个程序,执行时间半秒到一秒之间,结果是2998002,大家可以试一试。
#include
#include
#include
using namespace std;
const int N = 1000000;
const int M = 1000000;
inline void InsertHeapSet(int* a, int i, int* b, int j, priority_queue
int, pair > >& Q, set >& S)
{
Q.push(pair >(a[i]+b[j], pair(i, j)));
S.insert(pair(i, j));
}
void main()
{
int* a = new int [N];
int* b = new int [N];
for(int i=0; i
{
|
|
s**9 发帖数: 207 | 31 http://people.csail.mit.edu/bdean/6.046/dp/ is a good resource for DP.
For this problem, DP complexity is O(n**2 10**k) where k is the number of
digits.
Let the array be a1,...,an
Define p(i,j)=1 if the sum of a subset of a1,...,ai is j. It holds that
p(i,j)=1 if p(i-1,j)=1 or p(i-1, j-ai)=1.
#include
using namespace std;
bool subsum(int a[], int n){
int sum=0;
int minI=0;
for(int i=0;i
sum+=a[i];
minI=(a[minI]>a[i]?i:minI);
}
if(sum%2==1)
|
|
s**9 发帖数: 207 | 32 solution based on back tracking:
#include
using namespace std;
const int N=10;
const int M=10;
//sort a[N] first;
int a[N]={0,0, 2, 3, 4,5,5,8,9,10};
int level=0;
int order[N];
int sum=0;
void comb(int i){
order[level++]=a[i];
sum+=a[i];
if(sum>M){
for(int j=0;j
cout<
cout<
}
if(level
for(int j=i+1;j
if(a[j]!=a[j-1])
comb(j);
}
}
sum-=a[i] |
|
h*****2 发帖数: 114 | 33 #include
using namespace std;
int main(void)
{
bool isOdd(false);
int A[] = {2,5,7,8,9};
int B[] = {1,3,4,7,9,10};
int m=sizeof(A)/sizeof(int), n=sizeof(B)/sizeof(int);
int Half_Place;
if((m+n)%2 == 1)
{
isOdd = true;
Half_Place = (m+n)/2+1;
}
else
Half_Place = (m+n)/2;
int i=0, m_temp(0), n_temp(0);
int Value;
int current_A, current_B;
while(i < Half_Place)
{
if(A[m_temp] < B[n_temp])
{
|
|
h***9 发帖数: 45 | 34 //This seems to work
//The problem seems to be in vec4.reserve(10).
#include
#include
#include
#include
using namespace std;
int main(){
vector vec4 (10,0); // myvector: 10 10 10 10 10 10 10 10
// vec4.reserve(10);
fill_n(vec4.begin(), 10, 1);
copy(vec4.begin(), vec4.end(), ostream_iterator (cout, " "));
cout << endl;
return 0;
} |
|
y****n 发帖数: 579 | 35 你搞java的时候能通过设置不写import吗? |
|
g*******y 发帖数: 2114 | 36 这个相当于java的 package吧(不懂c++)
java里理论上可以不写package名字的
虽然实际中基本上都得用package管理名字 |
|
h**6 发帖数: 4160 | 37 我看很多职业码工,习惯每次在string和vector前面都加上std:: |
|
s***f 发帖数: 226 | 38 You can just write a simple script so that whenever you want to create a new
.cpp file, it will have automatically written #include / using already for
you. |
|
s***f 发帖数: 226 | 39 or you can have a template and so a save as for each .cpp file.
There are too many ways to do it. |
|
b***u 发帖数: 12010 | 40 我通常每个函数内都敲一次,怕全局写通过不了code review |
|
c**y 发帖数: 172 | 41 Below is my code. However, (101,2)^2 looks the simplest.
#include
#include
#include
using namespace std;
int countRect(int x, int y) {
if ((x == 0) || (y == 0)) {
return 0;
} else if ((x == 1) && (y > 1)) {
return y;
} else if ((x > 1) && (y == 1)) {
return x;
} else {
return (x * y);
}
}
int countAllRect(int n, int m) {
if ((n <= 0) || (m <= 0)) {
return 0;
} else if ((n == 1) && (m > 1)) {
|
|
i**********e 发帖数: 1145 | 42 我以前贴过,但不知道为什么找不回那个帖子了。
#include
#include
#include
using namespace std;
const int TAB_SPACE = 4;
void outputJSon(istream &in, int indentLevel) {
bool firstChar = true;
bool inBracket = false;
while (in) {
char c = in.get();
if (firstChar) {
cout << endl << setw(indentLevel) << c;
firstChar = false;
}
else {
cout << c;
}
if (c == '{') {
outputJSon(in, indentLevel+TAB_SPACE);
c = in.get();
assert(c == '... 阅读全帖 |
|
b********e 发帖数: 53 | 43 #ifndef MERGE_SORTED_ARRAY_H
#define MERGE_SORTED_ARRAY_H
#include
using namespace std;
/* problem statement:
* Given an array of n elements and an integer k where k
* Elements {a[0].....a[k] and a[k+1].....a[n] are already sorted. Give an
* algorithm to sort in O(n) time and O(1) space.
*/
class MergeSortedArray
{
public:
// [begin, end), inflection is the beginning of the second sorted
beginning
void sort(int * begin, int * end, int * inflection)
{
if (begin > |
|
i*****e 发帖数: 113 | 44 批判一下我的程序
假设数组是[0..k-1], [k..n-1],这样看起来舒服一些
第一个是递归的形式,尾递归所以空间没问题,第二个改写成了循环
考虑最差情况,k=1,这时候时间复杂度是O(n)
using namespace std;
void sort_recurse(int a[], int n, int k)
{
int ll = k, lr = n - k;
if (ll == 0 || lr == 0) {
return;
}
if (ll == lr) {
for (int i = 0; i < ll; ++i)
swap(a[i], a[k+i]);
return;
} else if (ll < lr) {
for (int i = 0; i < ll; ++i)
swap(a[i], a[n-k+i]);
return sort_recurse(a, n - ll, k);
} else { |
|
c**********e 发帖数: 2007 | 45 #include
using namespace std;
void main()
{
char *c1="xyz";
cout << c1 << endl;
char *c2=0;
cout << c2 << endl;
char *c3="abc";
cout << c3 << endl;
} |
|
d**e 发帖数: 6098 | 46 刚写了一下,出来的结果是对的,不知你是不是某一步写错了。
#include
#include
#include
using namespace std;
void genknuth(int m, int n)
{
for(int i = 0; i < n; i++)
{
if(rand() % (n-i) < m)
{
cout << i << endl;
m--;
}
}
}
int main(int argc, char * argv[])
{
srand(time(0));
genknuth(15, 20);
return 0;
} |
|
c**********e 发帖数: 2007 | 47 #include
using namespace std;
class Base {
protected:
virtual void output(std::ostream& os) { os << "Base\n"; }
};
class Derived : private Base {
friend std::ostream& operator<<(std::ostream&, Derived&);
};
std::ostream& operator<<(std::ostream& os, Derived& d) {
d.output(os);
}
int main() {
Base b; Derived d;
std::cout << b << d;
}
For which one of the following reasons is the code above INVALID?
a) Base::output is defined for Base, but operator<< is defined for Derived.
b) |
|
c**********e 发帖数: 2007 | 48 extern const int MaxElements=10;
Which one of the following statements about the declaration in the sample
area above is true?
a) It defines a global constant variable that can be referenced from other
modules.
b) It references the global constant variable MaxElements declared in
another module.
c) It initializes a variable declared in another module.
d) It retrieves the global constant variable MaxElements declared in another
module.
e) It is only legal within an unnamed namespace.
(9_33 a32_48 |
|
t****t 发帖数: 6806 | 49 const makes namespace scope names internal linkage unless extern is
specified. (3.5 clause 3)
and |
|
x***y 发帖数: 633 | 50 In your statement, there are several mistakes that are common for most
people.
First of all, as gg() is defined in A, it can only see void f(int) in A, so
that's why the private comes here. If you want void f(int) in B can be
called by gg(), you need to define virtual void f(int) as virtual, which is
called NVI idiom(please check it yourself).
State again: each function signature must be unqiue in a scope, either in a
class or in a namespace. For the inheritence relationship, there is also
name |
|