C++ Code Snippet (1)
C++ File Operation
1
2 void FileHandle(string name,string outfile)
3 {
4 ifstream in(name.c_str());
5 ofstream out(outfile.c_str());
6
7 if(in) {
8
9 out << in.rdbuf();
10
11 }
12 in.close();
13 out.close();
14 }
2 void FileHandle(string name,string outfile)
3 {
4 ifstream in(name.c_str());
5 ofstream out(outfile.c_str());
6
7 if(in) {
8
9 out << in.rdbuf();
10
11 }
12 in.close();
13 out.close();
14 }
------------------------------------
时间
#include<time.h>
time_t t = time(NULL);
cout << ctime(&t);
// Thu Oct 07 12:16:08 2010
-------------------------------------
字符串
#include<string>
string str;
assign(string);
append();
c_str();
find_first_of();
substr(int start,int len);
--------------------------------------
目录操作
#include<io.h>
_finddata_t data;
long handle = _findfirst(“”,&data);
-----------------------------------------
打印矩阵
n = 4
1 4 5 16
2 3 6 15
9 8 7 14
10 11 12 13
View Code
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int n;
7 cin >> n;
8 int **d;
9 d = new int * [n];
10 for (int i=0;i<n;i++) {
11 d[i] = new int[n];
12 }
13 int count = 1;
14 for (int i = 1;i<=n;i++) {
15 if (i%2 == 1) {
16 for (int j=0;j<i;j++) {
17 d[i-1][j] = count++;
18 }
19 for (int j=i-2;j>=0;j--) {
20 d[j][i-1] = count++;
21 }
22 }
23 else {
24 for (int j=0;j<i;j++) {
25 d[j][i-1] = count++;
26 }
27 for (int j=i-2;j>=0;j--) {
28 d[i-1][j] = count++;
29 }
30 }
31 }
32
33 for (int i=0;i<n;i++) {
34 for (int j=0;j<n;j++) {
35 cout << d[i][j] << "\t";
36 }
37 cout << endl;
38 }
39
40 cin >> n;
41 return 0;
42 }
2 using namespace std;
3
4 int main()
5 {
6 int n;
7 cin >> n;
8 int **d;
9 d = new int * [n];
10 for (int i=0;i<n;i++) {
11 d[i] = new int[n];
12 }
13 int count = 1;
14 for (int i = 1;i<=n;i++) {
15 if (i%2 == 1) {
16 for (int j=0;j<i;j++) {
17 d[i-1][j] = count++;
18 }
19 for (int j=i-2;j>=0;j--) {
20 d[j][i-1] = count++;
21 }
22 }
23 else {
24 for (int j=0;j<i;j++) {
25 d[j][i-1] = count++;
26 }
27 for (int j=i-2;j>=0;j--) {
28 d[i-1][j] = count++;
29 }
30 }
31 }
32
33 for (int i=0;i<n;i++) {
34 for (int j=0;j<n;j++) {
35 cout << d[i][j] << "\t";
36 }
37 cout << endl;
38 }
39
40 cin >> n;
41 return 0;
42 }
方法二
1 int compute(int i,int j)
2 {
3 int max = i>j?i:j;
4 if (max%2)
5 return max*max -max +1 -(i-j);
6 else
7 return max*max -max +1 +(i-j);
8 }
9
10 void printMatrix2(int n)
11 {
12 for(int i=1;i<=n;i++)
13 {
14 for (int j=1;j<=n;j++)
15 {
16 printf("%2d ", compute(i,j));
17 }
18 printf("\n");
19 }
20
21 }
22
23 int main(int argc, char* argv[])
24 {
25 printMatrix2(6);
26 return 0;
27 }
2 {
3 int max = i>j?i:j;
4 if (max%2)
5 return max*max -max +1 -(i-j);
6 else
7 return max*max -max +1 +(i-j);
8 }
9
10 void printMatrix2(int n)
11 {
12 for(int i=1;i<=n;i++)
13 {
14 for (int j=1;j<=n;j++)
15 {
16 printf("%2d ", compute(i,j));
17 }
18 printf("\n");
19 }
20
21 }
22
23 int main(int argc, char* argv[])
24 {
25 printMatrix2(6);
26 return 0;
27 }
------------------------------------------
最长对称子串
abcdedcbx
bcdedcb
View Code
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
int max = 1;
int r = 0;
int cur = 1;
for (int i=0;i<str.length()-1;i++) {
cur = 1;
for(int j = 1;j<=str.length()/2+1;j++) {
if(i-j<0||i+j>=str.length()||(str[i-j] != str[i+j])){
if(cur > max) {
max = cur;
r = i - j +1;
}
break;
}else {
cur += 2;
}
}
if (str[i] == str[i+1]) {
cur = 2;
for(int j = 1;j<=str.length()/2+1;j++) {
if(i-j<0||i+1+j>=str.length()||(str[i-j] != str[i+j])){
if(cur > max) {
max = cur;
r = i - j +1;
}
break;
}else{
cur += 2;
}
}
}
}
cout << "length : " << max <<endl;
cout << str.substr(r,max) << endl;
system("pause");
return 0;
}
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
int max = 1;
int r = 0;
int cur = 1;
for (int i=0;i<str.length()-1;i++) {
cur = 1;
for(int j = 1;j<=str.length()/2+1;j++) {
if(i-j<0||i+j>=str.length()||(str[i-j] != str[i+j])){
if(cur > max) {
max = cur;
r = i - j +1;
}
break;
}else {
cur += 2;
}
}
if (str[i] == str[i+1]) {
cur = 2;
for(int j = 1;j<=str.length()/2+1;j++) {
if(i-j<0||i+1+j>=str.length()||(str[i-j] != str[i+j])){
if(cur > max) {
max = cur;
r = i - j +1;
}
break;
}else{
cur += 2;
}
}
}
}
cout << "length : " << max <<endl;
cout << str.substr(r,max) << endl;
system("pause");
return 0;
}
-----------------------------------------
有一个人站在电影院门口卖票,票价50,一开始手上没有找零的钱,
现在有两种人来买票,A拿着100元的钱,人数为m(m<20),B拿着50元的钱,
人数为n(n<20)。卖票的人必须用从B类人中那里得来钱找给A,所以卖票
的顺序是有限制的。
要求写一个程序打印出所有的买票序列:
例如:m =2 ,n = 3;
BABAB
BBAAB
BBBAA
BBABA
现在有两种人来买票,A拿着100元的钱,人数为m(m<20),B拿着50元的钱,
人数为n(n<20)。卖票的人必须用从B类人中那里得来钱找给A,所以卖票
的顺序是有限制的。
要求写一个程序打印出所有的买票序列:
例如:m =2 ,n = 3;
BABAB
BBAAB
BBBAA
BBABA
View Code
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 char* next(char *s,int n,int m);
6
7 int main()
8 {
9 int n = 5, m = 4;
10 char* s = "BABABABAB";
11
12 cout << s << endl;
13 s = next(s,n,m);
14 while(s != "") {
15 cout << s << endl;
16 s = next(s,n,m);
17 }
18
19 system("pause");
20 return 0;
21 }
22
23 char* next(char *s,int n,int m)
24 {
25 char* ns = (char*)malloc(sizeof(char)*(m+n+1));
26 int nc = 0 , mc = 0;
27 int p = n+m-1;
28
29 while(s[p] == 'A' && p>=0) {
30 p --;
31 mc ++;
32 }
33 while(s[p] == 'B' && p>=0) {
34 p --;
35 nc ++;
36 }
37 if( p < 0 )
38 return "";
39 for(int i=0;i<p;i++)
40 ns[i] = s[i];
41 ns[p] = 'B';
42 ns[p+1] = 'A';
43 p += 2;
44 int x = (n+1-nc) - (m-mc);
45
46 while(p<m+n) {
47 if(x > 0 && mc >= 0) {
48 ns[p++] = 'A';
49 x --;
50 mc --;
51 }else{
52 ns[p++] = 'B';
53 x ++;
54 }
55
56 }
57 ns[n+m] = '\0';
58 return ns;
59 }
2 #include <string>
3 using namespace std;
4
5 char* next(char *s,int n,int m);
6
7 int main()
8 {
9 int n = 5, m = 4;
10 char* s = "BABABABAB";
11
12 cout << s << endl;
13 s = next(s,n,m);
14 while(s != "") {
15 cout << s << endl;
16 s = next(s,n,m);
17 }
18
19 system("pause");
20 return 0;
21 }
22
23 char* next(char *s,int n,int m)
24 {
25 char* ns = (char*)malloc(sizeof(char)*(m+n+1));
26 int nc = 0 , mc = 0;
27 int p = n+m-1;
28
29 while(s[p] == 'A' && p>=0) {
30 p --;
31 mc ++;
32 }
33 while(s[p] == 'B' && p>=0) {
34 p --;
35 nc ++;
36 }
37 if( p < 0 )
38 return "";
39 for(int i=0;i<p;i++)
40 ns[i] = s[i];
41 ns[p] = 'B';
42 ns[p+1] = 'A';
43 p += 2;
44 int x = (n+1-nc) - (m-mc);
45
46 while(p<m+n) {
47 if(x > 0 && mc >= 0) {
48 ns[p++] = 'A';
49 x --;
50 mc --;
51 }else{
52 ns[p++] = 'B';
53 x ++;
54 }
55
56 }
57 ns[n+m] = '\0';
58 return ns;
59 }
=============================
变长整数编码解码
变长整数编码解码
1 #include<iostream>
2 using namespace std;
3
4 void vint_encode(char* buf,int num)
5 {
6 int i = 0;
7 while(num != 0) {
8 buf[i] = num % (1 << ((i+1)*7));
9 num = num / (1 << ((i+1)*7));
10 if (num != 0)
11 buf[i] += 128;
12 i++;
13 }
14 buf[i] = '\0';
15
16 }
17
18 int vint_decode(const char* buf)
19 {
20 int sum = 0;
21 char c;
22 for (unsigned int i=0;i<strlen(buf);i++) {
23 c = buf[i] < 0 ?(128 +buf[i]):buf[i];
24 sum += c * (1 << (i*7));
25 }
26 return sum;
27
28 }
29
30
31
32 int main()
33 {
34
35 char* s = new char[20];
36 for (int i=128;i<10000;i*=2) {
37 vint_encode(s,i);
38 cout << vint_decode(s) << endl;
39 }
40
41
42 system("pause");
43 return 0;
44 }
2 using namespace std;
3
4 void vint_encode(char* buf,int num)
5 {
6 int i = 0;
7 while(num != 0) {
8 buf[i] = num % (1 << ((i+1)*7));
9 num = num / (1 << ((i+1)*7));
10 if (num != 0)
11 buf[i] += 128;
12 i++;
13 }
14 buf[i] = '\0';
15
16 }
17
18 int vint_decode(const char* buf)
19 {
20 int sum = 0;
21 char c;
22 for (unsigned int i=0;i<strlen(buf);i++) {
23 c = buf[i] < 0 ?(128 +buf[i]):buf[i];
24 sum += c * (1 << (i*7));
25 }
26 return sum;
27
28 }
29
30
31
32 int main()
33 {
34
35 char* s = new char[20];
36 for (int i=128;i<10000;i*=2) {
37 vint_encode(s,i);
38 cout << vint_decode(s) << endl;
39 }
40
41
42 system("pause");
43 return 0;
44 }
=============================
链表基本操作
链表基本操作
1 #include <iostream>
2 #include <iomanip>
3
4 using namespace std;
5
6 template <class T>
7 struct node
8 {
9 T data;
10 node *next;
11 };
12
13 template <class T>
14 class list
15 {
16 public:
17 list();
18 void Create();
19 bool Empty() const; //判断链表是否为空
20 void InsertLast(const T e);
21 void InsertFirst(const T e);
22 void DeleteFirst();
23 void DeleteLast();
24 void Display() const;
25 node<T>* GetNode(int i);
26 void Reverse();
27 bool Find(const T e);
28 ~list(); //销毁链表
29 private:
30 node<T> *head; //头节点
31 };
32
33 template <class T>
34 list<T>::list()
35 {
36 head=new node<T>;
37 head->next=NULL;
38 }
39
40 template <class T>
41 void list<T>::Create()
42 {
43 node<T> *p,*q;
44 p=head;
45 q=new node<T>;
46 cout<<"Input value( 'ctrl+z'): ";
47 while(cin>>q->data)
48 {
49 p->next=q;
50 p=q;
51 q=new node<T>;
52 }
53 p->next=NULL;
54 }
55
56 template <class T>
57 bool list<T>::Empty() const
58 {
59 return (head->next==NULL);
60 }
61
62 template <class T>
63 void list<T>::InsertFirst(const T e)
64 {
65 node<T> *p=new node<T>;
66 p->data=e;
67 p->next=head->next;
68 head->next=p;
69 }
70
71 template <class T>
72 void list<T>::InsertLast(const T e)
73 {
74 node<T> *p,*q;
75 p=head;
76 q=new node<T>;
77 q->data=e;
78 while(p->next)
79 {
80 p=p->next;
81 }
82 p->next=q;
83 q->next=NULL;
84 }
85
86 template <class T>
87 void list<T>::DeleteFirst()
88 {
89 head->next=head->next->next;
90 }
91
92 template <class T>
93 void list<T>::DeleteLast()
94 {
95 node<T> *p;
96 p=head;
97 while(p->next->next)
98 p=p->next;
99 p->next=NULL;
100 }
101
102 template <class T>
103 void list<T>::Display() const
104 {
105 node<T> *p;
106 p=head->next;
107 while(p)
108 {
109 cout<<p->data<<" ";
110 p=p->next;
111 }
112 }
113
114 template <class T>
115 node<T>* list<T>::GetNode(int i) //返回第i个节点
116 {
117 int k=0;
118 node<T> *p;
119 p=head;
120 while(p && k<i)
121 {
122 p=p->next;
123 ++k;
124 }
125 return p;
126 }
127
128 template <class T>
129 void list<T>:: Reverse()
130 {
131 if (!head->next ) {
132 return;
133 }
134 node<T> *p,*q,*r;
135 p= head->next;
136 q = p->next;
137 p->next = NULL;
138 while (q){
139 r = q->next;
140 q->next = p;
141 p = q;
142 q = r;
143 }
144 head->next = p;
145
146 }
147
148 template <class T>
149 bool list<T>::Find(const T e)
150 {
151 bool flag=false;
152 node<T> *p;
153 p=head->next;
154 while(p)
155 {
156 if(p->data==e)
157 {
158 flag=true;
159 break;
160 }
161 p=p->next;
162 }
163 return flag;
164 }
165
166 template <class T>
167 list<T>::~list()
168 {
169 node<T> *p;
170 while(head)
171 {
172 p=head->next;
173 delete head;
174 head=p;
175 }
176 }
177
178
179 int main()
180 {
181 list<int> ilist;
182 ilist.Create(); //创建链表
183 ilist.Display(); //输出链表
184 cout<<endl;
185 ilist.InsertFirst(123); //从头插入一个值
186 ilist.InsertLast(456); //从尾插入一个值
187 ilist.Display();
188 cout<<endl;
189 if(ilist.Find(123))
190 cout<<"123 在链表中"<<endl;
191 else
192 cout<<"123 不在链表中"<<endl;
193 ilist.DeleteFirst(); //从头删除一个值
194 ilist.DeleteLast(); //从尾删除一个值
195 ilist.Display();
196 cout<<endl;
197 cout << "=============="<<endl;
198 ilist.Reverse(); //逆序
199 ilist.Display();
200 cout<<endl;
201 system("pause");
202 return 0;
203 }
2 #include <iomanip>
3
4 using namespace std;
5
6 template <class T>
7 struct node
8 {
9 T data;
10 node *next;
11 };
12
13 template <class T>
14 class list
15 {
16 public:
17 list();
18 void Create();
19 bool Empty() const; //判断链表是否为空
20 void InsertLast(const T e);
21 void InsertFirst(const T e);
22 void DeleteFirst();
23 void DeleteLast();
24 void Display() const;
25 node<T>* GetNode(int i);
26 void Reverse();
27 bool Find(const T e);
28 ~list(); //销毁链表
29 private:
30 node<T> *head; //头节点
31 };
32
33 template <class T>
34 list<T>::list()
35 {
36 head=new node<T>;
37 head->next=NULL;
38 }
39
40 template <class T>
41 void list<T>::Create()
42 {
43 node<T> *p,*q;
44 p=head;
45 q=new node<T>;
46 cout<<"Input value( 'ctrl+z'): ";
47 while(cin>>q->data)
48 {
49 p->next=q;
50 p=q;
51 q=new node<T>;
52 }
53 p->next=NULL;
54 }
55
56 template <class T>
57 bool list<T>::Empty() const
58 {
59 return (head->next==NULL);
60 }
61
62 template <class T>
63 void list<T>::InsertFirst(const T e)
64 {
65 node<T> *p=new node<T>;
66 p->data=e;
67 p->next=head->next;
68 head->next=p;
69 }
70
71 template <class T>
72 void list<T>::InsertLast(const T e)
73 {
74 node<T> *p,*q;
75 p=head;
76 q=new node<T>;
77 q->data=e;
78 while(p->next)
79 {
80 p=p->next;
81 }
82 p->next=q;
83 q->next=NULL;
84 }
85
86 template <class T>
87 void list<T>::DeleteFirst()
88 {
89 head->next=head->next->next;
90 }
91
92 template <class T>
93 void list<T>::DeleteLast()
94 {
95 node<T> *p;
96 p=head;
97 while(p->next->next)
98 p=p->next;
99 p->next=NULL;
100 }
101
102 template <class T>
103 void list<T>::Display() const
104 {
105 node<T> *p;
106 p=head->next;
107 while(p)
108 {
109 cout<<p->data<<" ";
110 p=p->next;
111 }
112 }
113
114 template <class T>
115 node<T>* list<T>::GetNode(int i) //返回第i个节点
116 {
117 int k=0;
118 node<T> *p;
119 p=head;
120 while(p && k<i)
121 {
122 p=p->next;
123 ++k;
124 }
125 return p;
126 }
127
128 template <class T>
129 void list<T>:: Reverse()
130 {
131 if (!head->next ) {
132 return;
133 }
134 node<T> *p,*q,*r;
135 p= head->next;
136 q = p->next;
137 p->next = NULL;
138 while (q){
139 r = q->next;
140 q->next = p;
141 p = q;
142 q = r;
143 }
144 head->next = p;
145
146 }
147
148 template <class T>
149 bool list<T>::Find(const T e)
150 {
151 bool flag=false;
152 node<T> *p;
153 p=head->next;
154 while(p)
155 {
156 if(p->data==e)
157 {
158 flag=true;
159 break;
160 }
161 p=p->next;
162 }
163 return flag;
164 }
165
166 template <class T>
167 list<T>::~list()
168 {
169 node<T> *p;
170 while(head)
171 {
172 p=head->next;
173 delete head;
174 head=p;
175 }
176 }
177
178
179 int main()
180 {
181 list<int> ilist;
182 ilist.Create(); //创建链表
183 ilist.Display(); //输出链表
184 cout<<endl;
185 ilist.InsertFirst(123); //从头插入一个值
186 ilist.InsertLast(456); //从尾插入一个值
187 ilist.Display();
188 cout<<endl;
189 if(ilist.Find(123))
190 cout<<"123 在链表中"<<endl;
191 else
192 cout<<"123 不在链表中"<<endl;
193 ilist.DeleteFirst(); //从头删除一个值
194 ilist.DeleteLast(); //从尾删除一个值
195 ilist.Display();
196 cout<<endl;
197 cout << "=============="<<endl;
198 ilist.Reverse(); //逆序
199 ilist.Display();
200 cout<<endl;
201 system("pause");
202 return 0;
203 }