- 博客(19)
- 资源 (1)
- 收藏
- 关注
原创 含有无符号类型的表达式
#include "stdafx.h"#include#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ for (unsigned u = 10; u >= 0; --u){ cout << u << endl; } return 0;}上述代码有问题,变量u永远也不会小于0
2017-02-25 13:48:02 239
原创 与旧代码的接口(C++中C_str()的用法)
#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ string s("hello world"); char *str = s; cout << *str; const char *str1= s.c_str(); cout << endl << *str1; return 0;}不能用st
2017-02-24 16:59:15 351
原创 字符串
#include#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ const char ca[] = { 'h', 'e', 'l', 'l', 'o' }; const char *cp = ca; while (*cp){ cout << *cp << endl; ++c
2017-02-24 14:58:09 151
原创 比较字符串
#include "stdafx.h"#include#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ string s1 = "A string example"; string s2 = "A different string"; cout << (s2 <s1) << end
2017-02-24 14:48:59 205
原创 素数筛法1163
#include "stdafx.h"#include#includeusing namespace std;int prime[10000];int primeSize=0;bool marked[10001] = { false };void init(){ for (int i = 2; i <= 10000; i++) { if (marked[i] == true)
2017-02-20 14:54:15 171
原创 素数测试1047
#include "stdafx.h"#include#includeusing namespace std;bool judge(int x){ if (x <= 1) return false; int bound = (int)sqrt(x) + 1; for (int i = 2; i <bound; i++){ if (x%i == 0){ return fals
2017-02-20 11:17:41 238
原创 C++版本百鸡问题1045
#include "stdafx.h"#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ int n; while (cin>>n) { for (int x = 0; x <= 100; x++){ for (int y = 0; y <= 100-x; y++){
2017-02-19 13:21:48 504
原创 C++版本查找(找X问题)1052
#include "stdafx.h"#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ int buf[200]; int n; int i; while (cin >> n){ for (i = 0; i < n; i++){ cin >> buf[i]; } int
2017-02-19 11:47:52 444
原创 string::size_type类型
#include "stdafx.h"#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ string line; string::size_type i=0; while (getline(cin, line)){ if (line.size() > 10){ cout
2017-02-18 18:55:02 366
原创 string的empty和size操作
#include "stdafx.h"#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ string line; while (getline(cin, line)){ if (!line.empty()) cout << line << endl; } return
2017-02-18 18:31:36 805
原创 使用getline读取一整行
#include "stdafx.h"#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ string line; while (getline(cin, line)) cout << line; return 0;}输出结果为:
2017-02-18 18:15:16 881
原创 C++(定义和初始化string对象)
#include "stdafx.h"#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ string s1; string s2; string s3 = "hiya"; string s4(10, 'c'); string s5(s3 + s4); cout << s1 <
2017-02-18 17:49:15 4796
原创 算法导论第十五章15.2-4
算法导论第15章368页,当思考一个动态规划问题时,我们应该弄清楚所涉及的子问题与子问题的依赖关系,问题的子问题图准确的表达了这些信息。那么根据此图可以来回答题目15.2-4.输入链长度为n那么矩阵数目为n-1,所以子问题图一共包含n-1个定点,一共包含1+2+3+......+n-1=1/2(n^2-n)条边,这些边连接原问题顶点和比其更小的子问题的顶点
2017-02-08 17:07:03 1311 1
原创 算法导论第15章15.2-5
第九行 for 循环执行j-i次,即为l-1次,调用m[i,j] 2(l-1)次,加上外面两层循环一共1/3(n^3-n)
2017-02-08 16:47:43 1759
原创 算法导论第15章15.2-2
MATRIX-CHAIN-ORDER得到的表s中存储的数值就是子问题中的分割点k。MATRIX-CHAIN-MULTIPLY(A,s,i,j) if i=j return 0 else k=s[i,j] return (MATRIX-CHAIN-MUTIPLY(A,s,i,k)+MATR
2017-02-07 10:46:34 910
原创 算法导论第15章15.2-1
找了一段C实现的矩阵链乘法#include "stdafx.h"#include#include#define N 1000int m[N][N]; //m[i][j]表示从第i个矩阵乘到第j个矩阵所需的最小代价int s[N][N]; //s[i][j]表示最优值m[i][j]对应的分割点int p[N]; //p[]代表矩阵链void MATRIX_
2017-02-06 17:47:57 2586
原创 算法导论第15章习题15.1-4
定义全局变量k存储子函数计算最大收益时的变量i#include "stdafx.h"#include using namespace std;int k=0;int Max(int a, int b){return a>b ? a : b;}int MEMOIZED_CUT_ROD_AUX(int p[], int n, int r[],int &k){
2017-02-05 12:59:45 1396
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人