因为51cto不允许博文超过8万字符。我将把这些练习放到接下来的博客中,但是它们其实是连着的。

  1. #pragma warning(disable:4786)
  2. #include <iostream>
  3. #include <sstream>
  4. #include <set>
  5. #include <algorithm>
  6. #include <map>
  7. #include <cmath>
  8. #include <vector>
  9. #include <queue>
  10. #include <deque>
  11. #include <IOMANIP>
  12. #include <stack>
  13. #include <list>
  14. using namespace std;
  15. #define is_year(year) ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) ? 1 : 0
  16. int month_day[13][2] = {
  17. {0,0},
  18. {31,31},
  19. {28,29},
  20. {31,31},
  21. {30,30},
  22. {31,31},
  23. {30,30},
  24. {31,31},
  25. {31,31},
  26. {30,30},
  27. {31,31},
  28. {30,30},
  29. {31,31}
  30. };
  31. class date
  32. {
  33. public:
  34. int year;
  35. int month;
  36. int day;
  37. date(){};
  38. date(int year,int month,int day);
  39. void display();
  40. void nextday();
  41. };
  42. date::date(int year,int month,int day)
  43. {
  44. this->year = year;
  45. this->month = month;
  46. this->day = day;
  47. }
  48. void date::display()
  49. {
  50. cout<<this->year<<'-'<<this->month<<'-'<<this->day<<endl;
  51. }
  52. void date::nextday()
  53. {
  54. ++day;
  55. if(month_day[month][is_year(year)] < day)
  56. {
  57. day = 1;
  58. ++month;
  59. if(month > 12)
  60. {
  61. month = 1;
  62. year++;
  63. }
  64. }
  65. }
  66. int days[5001][13][32];
  67. void main41()
  68. {
  69. date temp(0,1,1);
  70. int day_count = 0;
  71. while(temp.year < 5001)
  72. {
  73. days[temp.year][temp.month][temp.day] = day_count++;
  74. temp.nextday();
  75. //      cout<<temp.year<<' '<<temp.month<<' '<<temp.day<<' '<<day_count<<endl;
  76. }
  77. int y,m,d;
  78. while(cin>>y>>m>>d)
  79. {
  80. cout<<days[y][m][d]<<endl;
  81. }
  82. }
  83. int qixi[50001];
  84. void main44()
  85. {
  86. int i,j,num;
  87. for(i=0; i<=50000; ++i)
  88. qixi[i] = 1;
  89. for(i=2; i<=50000; ++i)
  90. for(j=2; i*j <= 50000; ++j)
  91. qixi[i*j] += i;
  92. while(cin>>num)
  93. {
  94. cout<<qixi[num]<<endl;
  95. }
  96. }
  97. //因子筛选
  98. /*for(int i=0; i<=500000; ++i)
  99. qixi[i] = 1;
  100. for(int i=2; i<=500000; ++i)
  101. for(int j=2; j*i<=500000; ++j)
  102. qixi[i*j] += i;
  103. */
  104. //素数筛选
  105. bool bPrime[1001];          //必须得单独赋值
  106. void main45()
  107. {
  108. int i,j;
  109. bPrime[0] = bPrime[1] = false;
  110. for(i=2; i<=1000; ++i) bPrime[i] = true;
  111. for(i=2; i<=1000; ++i)
  112. {
  113. if(bPrime[i])
  114. {
  115. for(j=i*i; j<=1000; j+=i)
  116. {
  117. //  cout<<j<<endl;
  118. bPrime[j] = false;
  119. }
  120. }
  121. }
  122. int num;
  123. while(cin>>num)
  124. {
  125. if(bPrime[num]) cout<<"yes"<<endl;
  126. else cout<<"no"<<endl;
  127. }
  128. }
  129. void main46()
  130. {
  131. char ch[10];
  132. cin.get(ch,10);
  133. cout<<ch;
  134. getchar();
  135. cin.get(ch,10);
  136. cout<<ch;
  137. }
  138. void main47()
  139. {
  140. string s("-122");
  141. istringstream iss(s);   //使用istringstream可以对输入进行格式化输入,不管是整数还是负数
  142. int a;
  143. iss>>a;
  144. cout<<a;
  145. vector<int> v;
  146. v.push_back(3);
  147. v.push_back(4);
  148. }
  149. #include <algorithm>//必须要有这个头文件。。。。。
  150. int main48(int argc, char*argv[])
  151. {
  152. std::string str = "song";
  153. reverse(str.begin(), str.end());
  154. return 0;
  155. }
  156. void main49()
  157. {
  158. char aa[100];
  159. int a;
  160. /*
  161. while(cin>>a)
  162. {
  163. itoa(a,aa,10);
  164. cout<<aa<<endl;
  165. memset(aa,'0',sizeof(aa));
  166. }
  167. */
  168. while(cin>>hex>>a)
  169. {
  170. sprintf(aa,"%b",a);
  171. cout<<aa<<endl;
  172. }
  173. }
  174. void main50()
  175. {
  176. int a,sum = 0,max = -100;
  177. while(cin>>a && a != -1000)
  178. {
  179. sum += a;
  180. if(sum > max)
  181. max = sum;
  182. if(sum < 0)
  183. sum = 0;
  184. }
  185. cout<<max<<endl;
  186. }
  187. int gcd(int a,int b)
  188. {
  189. if(b == 0)
  190. return a;
  191. else return gcd(b,a%b);
  192. }
  193. void  main51()
  194. {
  195. int a,b;
  196. while(cin>>a>>b)
  197. {
  198. cout<<gcd(a,b)<<endl;
  199. }
  200. }
  201. //求N!阶乘中乘5的次数
  202. void main52()
  203. {
  204. int cnt = 0,n;
  205. cin>>n;
  206. for(int i=1; i<=n; ++i)
  207. {
  208. int j = i;
  209. while(j % 5 == 0)
  210. {
  211. ++cnt;
  212. j /= 5;
  213. }
  214. }
  215. cout<<cnt<<endl;
  216. }
  217. void main53()
  218. {
  219. int cnt=0;
  220. int n;
  221. cin>>n;
  222. while(n)
  223. {
  224. cnt += n/5;
  225. n /= 5;
  226. }
  227. cout<<cnt<<endl;
  228. }
  229. //并查集的操作
  230. #define N 1000
  231. int tree[N];
  232. int findRoot(int x)
  233. {
  234. int ret = x;
  235. if(tree[x] != -1)
  236. {
  237. ret = findRoot(tree[x]);
  238. tree[x] = ret;
  239. }
  240. return ret;
  241. }
  242. void main54()
  243. {
  244. int n,m;
  245. while(cin>>n && n != 0)
  246. {
  247. cin>>m;
  248. for(int i=0; i<N; ++i)  tree[i] = -1;
  249. while(m--)
  250. {
  251. int a,b;
  252. cin>>a>>b;
  253. a = findRoot(a);
  254. b = findRoot(b);
  255. if(a != b)
  256. tree[a] = b;
  257. }
  258. int ans = 0;
  259. for(i=1; i<=n; ++i)
  260. if(tree[i] == -1) ++ans;
  261. cout<<--ans<<endl;
  262. }
  263. }
  264. int sum[N];  //只有当tree[i]中的值为真时候才有效
  265. //涉及到求并查集中的元素个数
  266. void main55()
  267. {
  268. int n;
  269. while(cin>>n)
  270. {
  271. for(int i=0; i<N; ++i)
  272. {
  273. tree[i] = -1;
  274. sum[i] = 1;
  275. }
  276. while(n--)
  277. {
  278. int a,b;
  279. cin>>a>>b;
  280. a = findRoot(a);
  281. b = findRoot(b);
  282. if(a != b)
  283. {
  284. tree[a] = b;
  285. sum[b] += sum[a];
  286. }
  287. }
  288. int ans = 0;
  289. for(i=1; i<=N; ++i)
  290. {
  291. if(tree[i] == -1 && sum[i] > ans)
  292. ans = sum[i];
  293. }
  294. cout<<ans<<endl;
  295. }
  296. }
  297. struct Edge1
  298. {
  299. int a,b,cost;
  300. bool operator < (struct Edge1 e)
  301. {
  302. return cost < e.cost;
  303. }
  304. }edge[N];
  305. void main56()
  306. {
  307. int n;
  308. while(cin>>n)
  309. {
  310. for(int i=1; i<=n*(n-1)/2; ++i)
  311. cin>>edge[i].a>>edge[i].b>>edge[i].cost;
  312. sort(edge+1,edge+1+n*(n-1)/2);
  313. for(i=0; i<N; ++i)
  314. tree[i] = -1;
  315. int ans = 0;
  316. for(i=1; i<=n*(n-1)/2; ++i)
  317. {
  318. int a = edge[i].a;
  319. int b = edge[i].b;
  320. a = findRoot(a);
  321. b = findRoot(b);
  322. if(a != b)
  323. {
  324. tree[a] = b;
  325. ans += edge[i].cost;
  326. }
  327. }
  328. cout<<ans<<endl;
  329. }
  330. }
  331. struct Edge2
  332. {
  333. int a,b;
  334. double cost;
  335. bool operator < (const Edge2 &e) const
  336. {
  337. return cost < e.cost;
  338. }
  339. }edge2[N];
  340. struct Point
  341. {
  342. double x,y;
  343. double getDistance(Point p)
  344. {
  345. return sqrt(pow(abs(x-p.x),2)+pow(abs(y-p.y),2));
  346. }
  347. }list1[N];
  348. void main57()
  349. {
  350. int n;
  351. while(cin>>n)
  352. {
  353. for(int i=1; i<=n; ++i)
  354. cin>>list1[i].x>>list1[i].y;
  355. int size = 0;
  356. for(i=1; i<=n; ++i)
  357. {
  358. for(int j=i+1; j<=n; ++j)
  359. {
  360. edge2[size].a = i;
  361. edge2[size].b = j;
  362. edge2[size].cost = list1[i].getDistance(list1[j]);
  363. ++size;
  364. }
  365. }
  366. sort(edge2+1,edge2+size);
  367. for(i=0; i<=N; ++i)
  368. tree[i] = -1;
  369. double ans = 0;
  370. for(i=0; i<size; ++i)
  371. {
  372. int a = findRoot(edge2[i].a);
  373. int b = findRoot(edge2[i].b);
  374. if(a != b)
  375. {
  376. tree[a] = b;
  377. ans += edge2[i].cost;
  378. }
  379. }
  380. printf("%.2lf\n",ans);
  381. //  cout<<setprecision(2)<<ans<<endl;
  382. }
  383. }
  384. int ans[N][N];  //利用一个矩阵保存图,边达不到则为-1
  385. void main58()
  386. {
  387. int n,m;
  388. while(cin>>n>>m && n && m)
  389. {
  390. for(int i=1; i<=n; ++i)
  391. {
  392. for(int j=1; j<=n; ++j)
  393. ans[i][j] = -1;
  394. ans[i][i] = 0;
  395. }
  396. while(m--)
  397. {
  398. int a,b,c;
  399. cin>>a>>b>>c;
  400. ans[a][b] = ans[b][a] = c;   //此式中为无向图
  401. }
  402. for(int k=1; k<=n; ++k)
  403. {
  404. for(i=1; i<=n; ++i)
  405. for(int j=1; j<=n; ++j)
  406. if(ans[i][k]==-1 || ans[k][j] == -1)
  407. continue;
  408. else if(ans[i][j] == -1 || ans[i][k] + ans[k][j] < ans[i][j])
  409. ans[i][j] = ans[i][k] + ans[k][j];
  410. }
  411. cout<<ans[1][n]<<endl;
  412. }
  413. }
  414. struct E
  415. {
  416. int node;
  417. int cost;
  418. };
  419. vector<E> vEdge[N];
  420. bool mark[N];
  421. int dist[N];
  422. void main59()
  423. {
  424. int n,m;
  425. while(cin>>n>>m && n && m)
  426. {
  427. for(int i=0; i<N; ++i) vEdge[i].clear();
  428. while(m--)
  429. {
  430. int a,b,c;
  431. cin>>a>>b>>c;
  432. E tmp;
  433. tmp.cost = c;
  434. tmp.node = b;
  435. vEdge[a].push_back(tmp);
  436. tmp.node = a;
  437. vEdge[b].push_back(tmp);
  438. }
  439. for(i=0; i<N; ++i)
  440. {
  441. dist[i] = -1;
  442. mark[i] = false;
  443. }
  444. dist[1] = 0;
  445. mark[1] = true;
  446. int newP = 1;   //初始节点
  447. for(i=1; i<n; ++i)
  448. {
  449. for(int j=0; j<vEdge[i].size(); ++j)
  450. {
  451. int t = vEdge[newP][j].node;
  452. int c = vEdge[newP][j].cost;
  453. if(mark[t]) continue;
  454. if(dist[t] == -1 || dist[newP] + c < dist[t])
  455. dist[t] = dist[newP] + c;
  456. }
  457. int min = 123123123;
  458. for(j=1; j<=n; ++j)
  459. {
  460. if(mark[i] || dist[j] == -1) continue;
  461. if(dist[j]  < min)
  462. {
  463. min = dist[j];
  464. newP = j;
  465. }
  466. }
  467. mark[newP] = true;
  468. }
  469. cout<<dist[n]<<endl;
  470. }
  471. }
  472. //字符串替换函数
  473. void replace(char str[],char key[],char swap[])
  474. {
  475. int l1,l2,l3,i,j,flag;
  476. char tmp[100];
  477. l1 = strlen(str);
  478. l2 = strlen(key);
  479. l3 = strlen(swap);
  480. for(i=0; i<=l1-l2; ++i)
  481. {
  482. flag = 1;
  483. for(j=0; j<l2; ++j)
  484. if(str[i+j] != key[j])
  485. {
  486. flag = 0;
  487. break;
  488. }
  489. if(flag)
  490. {
  491. strcpy(tmp,str);
  492. strcpy(tmp+i,swap);
  493. strcpy(tmp+i+l3,str+l2+i);
  494. strcpy(str,tmp);
  495. i += l3-1;          //不要忘了for循环中有++i操作
  496. l1 = strlen(str);
  497. }
  498. }
  499. }
  500. void main60()
  501. {
  502. char str[100] = "rei wreant treo replace re";
  503. char key[100] = "re";
  504. char swap[100] = "no";
  505. replace(str,key,swap);
  506. cout<<str<<endl;
  507. }
  508. int BFMatch(char *s,char *p)
  509. {
  510. int i=0,j;
  511. while(i<=strlen(s)-strlen(p))
  512. {
  513. j=0;
  514. while(s[i] == p[j] && j<strlen(p))
  515. {
  516. ++i;
  517. ++j;
  518. }
  519. if(j == strlen(p))
  520. return i-j;
  521. i = i-j+1;
  522. }
  523. return -1;
  524. }
  525. void main61()
  526. {
  527. char s1[100] = "i need to find a str in this";
  528. char s2[100] = "str";
  529. cout<<BFMatch(s1,s2)<<endl;
  530. }
  531. void getnext(char *p,int *next)
  532. {
  533. int j=0,k=-1;
  534. next[j] = k;
  535. while(j<strlen(p)-1)
  536. {
  537. if(k==-1 || p[j] == p[k])
  538. {
  539. ++k;
  540. ++j;
  541. next[j] = k;
  542. }else
  543. k = next[k];
  544. }
  545. }
  546. int KMPMatch(char *s,char *p)
  547. {
  548. int next[N];
  549. int i=0,j=0;       //i用来遍历s主串,j用来遍历模式串
  550. getnext(p,next);
  551. while(i<strlen(s))
  552. {
  553. if(j==-1 || p[j] == s[i])
  554. {
  555. ++j;
  556. ++i;
  557. }else
  558. j = next[j];
  559. if(j == strlen(p))
  560. return i-j;
  561. }
  562. return -1;
  563. }
  564. void main62()
  565. {
  566. char ch[100] = "asbabcababa";
  567. char c[100] = "aba";
  568. //  copy(next,next+100,ostream_iterator<int>(cout," "));
  569. cout<<KMPMatch(ch,c)<<endl;
  570. }
  571. struct Edge3
  572. {
  573. int node,cost,c;
  574. };
  575. vector<Edge3> vE[N];
  576. int cost1[N];
  577. void main63()
  578. {
  579. int n,m;
  580. int S,T;
  581. while(cin>>n>>m && n && m)
  582. {
  583. for(int i=0; i<N; ++i) vE[i].clear();
  584. while(m--)
  585. {
  586. int a,b,c,cost;
  587. cin>>a>>b>>c>>cost;
  588. Edge3 tmp;
  589. tmp.c = c;
  590. tmp.cost = cost;
  591. tmp.node = b;
  592. vE[a].push_back(tmp);
  593. tmp.node = a;
  594. vE[b].push_back(tmp);
  595. }
  596. cin>>S>>T;
  597. for(i=0; i<N; ++i)
  598. {
  599. mark[i] = false;
  600. dist[i] = -1;
  601. cost1[i] = 0;
  602. }
  603. int newP = S;
  604. mark[newP] = true;
  605. dist[newP] = 0;
  606. for(i=1; i<n; ++i)
  607. {
  608. for(int j=0; j<vE[newP].size(); ++j)
  609. {
  610. int t = vE[newP][j].node;
  611. int c = vE[newP][j].c;
  612. int co = vE[newP][j].cost;
  613. if(!mark[t])
  614. {
  615. if(dist[t] == -1 || dist[newP] + c < dist[t] || (dist[newP] + c == dist[t] && cost1[newP] + co < cost1[t]))
  616. {
  617. dist[t] = c+dist[newP];
  618. cost1[t] = co + cost1[newP];
  619. }
  620. }
  621. }
  622. int min = 123123123;
  623. for(j=1; j<=n; ++j)
  624. {
  625. if(!mark[j] && dist[j] != -1)
  626. {
  627. if(dist[j] < min)
  628. {
  629. newP = j;
  630. min = dist[j];
  631. }
  632. }
  633. }
  634. mark[newP] = true;
  635. }
  636. cout<<dist[T]<<"  "<<cost1[T]<<endl;
  637. }
  638. }
  639. vector<int> iv[N];      //拓扑排序适合使用链表表示法
  640. queue<int>  q;
  641. void main64()
  642. {
  643. int inDegree[N];
  644. int n,m;
  645. while(cin>>n>>m && n && m)
  646. {
  647. for(int i=0; i<N; ++i)
  648. {
  649. iv[i].clear();
  650. inDegree[i] = 0;
  651. }
  652. while(m--)
  653. {
  654. int a,b;
  655. cin>>a>>b;
  656. ++inDegree[b];
  657. iv[a].push_back(b);         //有向树
  658. }
  659. while(!q.empty()) q.pop();      //清空
  660. for(i=0; i<n; ++i)
  661. if(inDegree[i] == 0)
  662. q.push(i);
  663. int cnt = 0;
  664. while(!q.empty())
  665. {
  666. int newP = q.front();
  667. q.pop();
  668. ++cnt;
  669. for(i=0; i<iv[newP].size(); ++i)
  670. {
  671. int k=iv[newP][i];
  672. if(--inDegree[k] == 0)
  673. {
  674. q.push(k);
  675. }
  676. }
  677. }
  678. if(cnt == n)
  679. cout<<"yes"<<endl;
  680. else cout<<"no"<<endl;
  681. }
  682. }

转载于:https://blog.51cto.com/saibro/1184326

c++ STL平常练习-1相关推荐

  1. c++ STL平常练习-3

     接上一篇: void main79() { hanoi(3,'a','b','c' ); printf("%I64d\n",hanoiIII(12)); } char maze2 ...

  2. [技术] OIer的C++标准库 : STL入门

    注: 本文主要摘取STL在OI中的常用技巧应用, 所以可能会重点说明容器部分和算法部分, 且不会讨论所有支持的函数/操作并主要讨论 C++11 前支持的特性. 如果需要详细完整的介绍请自行查阅标准文档 ...

  3. STL中用erase()方法遍历删除元素

    STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时 ...

  4. 从零开始_学_数据结构(五)——STL(map、set、list、vector)

    STL容器 前注: STL(标准模板库)是一个C++的软件库,也是C++标准程序库的一部分. 这些容器,应该都是STL里面的一个类. vector封装数组.list封装链表.map和set封装二叉树 ...

  5. 为什么auto_ptr智能指针不能作为STL标准容器的元素

    上个星期的博客shared_ptr源码剖析里其实遗漏了一个问题:为什么auto_ptr不可以作为STL标准容器的元素,而shared_ptr可以?  我在网上看了好多篇讲shared_ptr的文章里讲 ...

  6. STL源码学习之空间配置

    STL的空间配置器主要用于内存的分配.填充.释放等操作,在学习它之前,需要深入一点理解new和delete.由于new和delete分两段操作:内存配置和对象构建(析构),本文亦将按此进行分类,其中第 ...

  7. 一文搞懂 STL 中 deque 与 hashtab 的底层实现

    文章目录 一.模板特化 二.设计容器必须定义的型别 三.deque 四.心心念念的优先队列 五.hashtable的构造 一.模板特化 针对任何模板参数更进一步的条件限制所设计出来的一个特化版本,如: ...

  8. STL原理与构建——阅读笔记

    前情提要: 本文不对任何算法做过多深入的解释,算法都很基础,建议以下所有内容等基础算法都会了之后再食用更佳哦- 文章目录 一.介绍STL 1.1 概述 1.2 六大组件 二.迭代器 2.1 迭代器 2 ...

  9. STL之string类:知其然,需知其所以然

    目录 前言 一,构造及初始化 1.1constuct类函数 1.2构造函数及其模拟实现 1.3拷贝构造及其模拟实现 1.4赋值操作符 1.5string类的swap接口 二,迭代器 2.1初识迭代器即 ...

最新文章

  1. 【Oracle Database】数据库用户管理
  2. 一行代码都不用写,教你如何快速搭建Github博客!!!
  3. phpStorm 2016.1 最新版激活方法
  4. 如何在IAR工程中创建和使用模板
  5. React Native的安装和初始化(android /ios)
  6. Ubuntu 16.04 下部署Node.js+MySQL微信小程序商城
  7. Ajax应用查询员工信息
  8. python文件合并_用Python 将两个文件的内容合并成一个新的文件.
  9. linux 显卡转码,ffmpeg用GPU转码
  10. Java 主流垃圾收集器
  11. 替换后的最长重复字符
  12. Intel 64/x86_64/IA-32/x86处理器 - SIMD指令集 - SSE扩展(6) - 逻辑指令 比较指令
  13. 分布式数据库的字符集
  14. vCenter上解决”此主机当前没有管理网络冗余“的警告
  15. OpenCV3.4.1 vs2015 自定义过程的图片拼接
  16. .Net MVC个人笔记
  17. uniapp开发微信小程序生成二维码海报
  18. Android Retrofit通过OkHttp设置Interceptor拦截器统一打印请求报文及返回报文
  19. 易语言之选取被选中超级列表框内容
  20. mysql查询计算机系信息_MYSQL查询1

热门文章

  1. C#关于电脑DPI改动显示的问题
  2. Windows 2003系统下桌面清理向导
  3. JVM调优:GC 参数
  4. Spring学习网址
  5. 为什么建议使用Linux?从“白嫖”到精通,只需要这几步
  6. Redis操作Set的相关API
  7. 索引方式:真的是用的B+Tree 吗?
  8. AQS.addWaiter
  9. MultipartResolver
  10. vue指令-单向和双向绑定