// 自我学习.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include"student.h"
int main()
{
int a, b;
//char *c="sasd";
cin >> a >> b;
student stu_one(a,b, "lsasd");
stu_one.showInf();
return 0;
}
#include"stdafx.h"
#include"student.h"
void student::showInf()
{
cout << name << " " << age << " " << grade;
}
student::student(int a,int b,char *c)
{
age = a;
grade = b;
name = new char[strlen(c) + 1];
if (name != 0)
{
strcpy_s(name, strlen(c) + 1,c);
}
}
student::~student()
{
delete []name;
name = 0;
}
student::student(student & g)
{
age = g.age;
grade = g.grade;
name = new char[strlen(g.name) + 1];
if (name != 0)
{
strcpy_s(name, strlen(g.name) + 1, g.name);
}
}
#pragma once
#include<iostream>
using namespace std;
class student
{
private:
char *name;
int age;
int grade;
public:
void showInf();
student(int ,int ,char* );
student(student&);
~student();
};
1.字符串的几个注意点
char *name;
char *name[];
char name[];
输出或者赋值的话直接对name进行操作。
2.字符的赋值,各种类型的转化的总结
字符串指针的赋值 strcpy_s(char*,int ,const char *)三个参数
直接拷贝地址memcpy(a(void);b(void);int size)
3.总结字符串的操作
string s;
for (i = 0; s[i]; i++) if (s[i] == '.') break;
这个语句可以查看string里面的每一个char;
4.总结c里面的和C++不同的地方
5.一个很简单的问题
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int V,count,row=0;
cin >> V;
while(row*(row+1)/2<V)
row++;
cout << V+row-1 << endl;
return 0;
}
6.在编写程序的时候,要注意提示用户输入的内容。
7.一个经典的三元素比较法
(a>b)?((a>c)?a:c):((b>c)?b:c)
8.一句话的辗转相除法
int GCD(int a,int b)
{
return b==0?a:GCD(b,a%b);
}
方法一:利用递归思想。
- //从后往前选取,选定位置i后,再在前i-1个里面选取m-1个。
- //如 1 2 3 4 5 中选取 3 个
- //1、选取5后,再在前4个里面选取2个,而前4个里面选取2个又是一个子问题,递归即可。
- //2、如果不包含5,直接选定4,那么再在前3个里面选取2个,而前三个里面选取2个又是一个子问题,递归即可。
- //3、如果也不包含4,直接选取3,那么再在前2个里面选取2个,刚好只有两个。
- //纵向看,1、2、3刚好是一个for循环,初值为5,终值为m
- //横向看,该问题为一个前i-1个中选m-1的递归。
- void Combination(int arr[], int nLen, int m, int out[], int outLen)
- {
- if(m == 0)
- {
- for (int j = 0; j < outLen; j++)
- cout << out[j] << "\t";
- cout << endl;
- return;
- }
- for (int i = nLen; i >= m; --i) //从后往前依次选定一个
- {
- out[m-1] = arr[i-1]; //选定一个后
- Combination(arr,i-1,m-1,out,outLen); // 从前i-1个里面选取m-1个进行递归
- }
- }
- void PrintCombination(int arr[], int nLen, int m)
- {
- if(m > nLen)
- return;
- int* out = new int[m];
- Combination(arr,nLen,m,out,m);
- delete [] out;
- }
//从后往前选取,选定位置i后,再在前i-1个里面选取m-1个。
//如 1 2 3 4 5 中选取 3 个
//1、选取5后,再在前4个里面选取2个,而前4个里面选取2个又是一个子问题,递归即可。
//2、如果不包含5,直接选定4,那么再在前3个里面选取2个,而前三个里面选取2个又是一个子问题,递归即可。
//3、如果也不包含4,直接选取3,那么再在前2个里面选取2个,刚好只有两个。
//纵向看,1、2、3刚好是一个for循环,初值为5,终值为m
//横向看,该问题为一个前i-1个中选m-1的递归。
void Combination(int arr[], int nLen, int m, int out[], int outLen)
{
if(m == 0)
{
for (int j = 0; j < outLen; j++)
cout << out[j] << "\t";
cout << endl;
return;
}
for (int i = nLen; i >= m; --i) //从后往前依次选定一个
{
out[m-1] = arr[i-1]; //选定一个后
Combination(arr,i-1,m-1,out,outLen); // 从前i-1个里面选取m-1个进行递归
}
}
void PrintCombination(int arr[], int nLen, int m)
{
if(m > nLen)
return;
int* out = new int[m];
Combination(arr,nLen,m,out,m);
delete [] out;
}
方法二:二进制组合算法:
思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标
代表的数被选中,为0则没选中。
首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。
然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为“01”组合,同时将其左边的所有“1”全部移动到数组的最左端(只有第一位变为0才需要移动,否则其左边的1本来就在最左端,无需移动)。
当第一个“1”移动到数组的m-n的位置,即n个“1”全部移动到最右端时,就得到了最后一个组合。
例如求5中选3的组合:
1 1 1 0 0 //1,2,3
1 1 0 1 0 //1,2,4
1 0 1 1 0 //1,3,4
0 1 1 1 0 //2,3,4
1 1 0 0 1 //1,2,5
1 0 1 0 1 //1,3,5
0 1 1 0 1 //2,3,5
1 0 0 1 1 //1,4,5
0 1 0 1 1 //2,4,5
0 0 1 1 1 //3,4,5
代码如下:
- void Combine(int arr[], int n, int m)
- {
- if(m > n)
- return;
- int* pTable = new int[n]; //定义标记buf并将其前m个置1
- memset(pTable,0,sizeof(int)*n);
- for(int i = 0; i < m; ++i)
- pTable[i] = 1;
- bool bFind = false;
- do
- {
- for (int i = 0; i < n; i++) //打印当前组合
- {
- if(pTable[i])
- cout << arr[i] << "\t";
- }
- cout << endl;
- bFind = false;
- for(int i = 0; i < n-1; i++)
- {
- if(pTable[i]==1 && pTable[i+1]==0)
- {
- swap(pTable[i],pTable[i+1]); //调换10为01
- bFind = true;
- if(pTable[0] == 0) //如果第一位为0,则将第i位置之前的1移到最左边,如为1则第i位置之前的1就在最左边,无需移动
- {
- for (int k=0, j=0; k < i; k++) //O(n)复杂度使1在前0在后
- {
- if(pTable[k])
- {
- swap(pTable[k],pTable[j]);
- j++;
- }
- }
- }
- break;
- }
- }
- } while (bFind);
- delete [] pTable;
- }
void Combine(int arr[], int n, int m)
{
if(m > n)
return;
int* pTable = new int[n]; //定义标记buf并将其前m个置1
memset(pTable,0,sizeof(int)*n);
for(int i = 0; i < m; ++i)
pTable[i] = 1;
bool bFind = false;
do
{
for (int i = 0; i < n; i++) //打印当前组合
{
if(pTable[i])
cout << arr[i] << "\t";
}
cout << endl;
bFind = false;
for(int i = 0; i < n-1; i++)
{
if(pTable[i]==1 && pTable[i+1]==0)
{
swap(pTable[i],pTable[i+1]); //调换10为01
bFind = true;
if(pTable[0] == 0) //如果第一位为0,则将第i位置之前的1移到最左边,如为1则第i位置之前的1就在最左边,无需移动
{
for (int k=0, j=0; k < i; k++) //O(n)复杂度使1在前0在后
{
if(pTable[k])
{
swap(pTable[k],pTable[j]);
j++;
}
}
}
break;
}
}
} while (bFind);
delete [] pTable;
}
测试代码如下
- int main()
- {
- int arr[] = {1,2,3,4,5,6};
- Combine(arr,sizeof(arr)/sizeof(int),3);
- //PrintCombination(arr,sizeof(arr)/sizeof(int),3);
- }
int main()
{
int arr[] = {1,2,3,4,5,6};
Combine(arr,sizeof(arr)/sizeof(int),3);
//PrintCombination(arr,sizeof(arr)/sizeof(int),3);
}
方法一结果 方法二结果