数据类型
1.sizeof关键字
计算其数据类型占用内存的大小
sizeof(int);
sizeof(hello);
2.静态变量
- 方法1: #define 不用分号
- 方法2 : 在局部变量前面加 const
// ------------ 常量 -------------
// 方法一: #define 不用分号
#include <iostream>
using namespace std ;
#define Day 7
int main(){
// Day = 10 ; // 无法修改否则报错
cout << "一周共有:" << Day << endl;
// 方法2 : 在局部变量前面加 const
const int month = 12 ;
cout << "一月共有:" << month << endl;
cout << "其数据大小为:" << sizeof(month) << endl;
system("pause");
}
// ------------ end -------------
// ------------ 表示符号 -------------
// 1. 区分大小写
// 2. 第一个字符只能是字母或者下划线
3.精度问题

单精度 float. 4字节
双精度 double 8 字节
都默认输出小数 : 6位有效数字 但是 统计占用内存空间不一样
科学计数法
float f2 =3e2; // 3*10 ^ 2 ; float f3 =3e-2; // 3*0.1 ^ 2 ; cout << "3e2:" << f2 << endl << "3e-2:" << f3 << endl ; // 3e2:300 // 3e-2:0.03
char 类型
char ch ="a" ; // 不可以用双引号 因为他代表的是一个单词,不是字符; cout << (int)ch << endl ; // 求其ascii // a -97 // A - 65
bool布尔类型
bool flag = true ; // 大小为 : 1
#####4. 三目运算符号
( a>b ? a:b ) =100;
#####5. 水仙花数
#include <iostream>
using namespace std;
int main ()
{
int n=100;
int ans;
do
{
int a,b,c;
a = n%10 ;
b = n/10%10;
c = n/10/10%10;
ans = a*a*a+b*b*b+c*c*c ;
if(ans == n && ans <1000)
cout << "the result is : " << ans << endl;
n++;
}while( n<1000);
return 0;
}

6.乘法表
/*
7的倍数 %7 = 0
个位有7 %10 =7
十位有7 /10 = 7
用于敲桌子游戏
打印乘法表
*/
#include <iostream>
using namespace std;
int main ()
{
int ans[10][10];
int j=0;
for (int i = 1 ; i < 10 ; i++) // 控制行树
{
for( j=1 ; j <= i ; j++) // 控制列数
{
ans[i][j] = i*j ;
cout << j << "x" << i << "=" << ans[i][j] << " " ;
}
cout<< endl ;
}
}
7.元素逆置
/*
20020101
元素逆置
start = 0 ; 角标
end = sizeof(arr);
使用中间变量temp
使temp = arr[start] ;
start = arr[end];
end = temp ;
start++ ;
end -- ;
if(start == end)
break;
*/
#include <iostream>
using namespace std;
int main ()
{
int n=5;
int arr[n];
// cin >> n ; // 注意一开始就要声明好空间大小
for(int i=0 ; i<n ; i++ )
cin >> arr[i];
for(int i=0 ; i<n ; i++ )
cout << arr[i] << " ";
cout << endl ;
int start = 0;
int end = sizeof(arr) / sizeof(arr[0])-1; // 注意循环队列中的end最后角标为:该类型数组的个数再减去1,因为从0开始
while(start < end){
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
for(int i=0 ; i<n ; i++ )
cout << arr[i] << " ";
cout << endl;
}
8.冒泡排序
/*
经过前面的交换问题,冒泡就是挺的交换,得到最右端有一个最大值
即: 将最后一位 不用再比较
*/
#include <iostream>
using namespace std;
int main ()
{
int arr[6]={6,5,4,3,2,1};
for(int i = 0; i<6 ; i++)
{
cout << arr[i] << " " ;
}
cout << endl ;
for(int i = 0 ; i < 6-1 ; i++) { // 外层控制每轮交换次数
for(int j = 0; j < 6-i-1 ; j++) // 内层控制每轮的前后比较问题 ---为了快捷运算
// 需要记得 外层最多循环 n-1 内部则是 n-i-1
{
if( arr[j] > arr[j+1] )
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]= temp ;
}
}
}
cout << "after:" << endl;
for(int i = 0; i<6 ; i++)
{
cout << arr[i] << " " ;
}
}
charpter3 排序算法
3.1 插入排序


/*
错误版本,第一次做插值排序。难点不懂怎么将所有的数据后移一位,一位如果采用向上类型:
0 1 2 3 4 5
即 a[i] = a[i+1] ; i++;
0 = 1
1 = 2
2 = 3
3 = 4
4 = 5
那么需要注意设置一个 5 = 0
感觉自己写了一个交换排序
看了答案--自我反思
*/
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
// 插入排序
int main(){
int n ;
int a[1000];
cin >> n ;
for(int i =0; i < n ; i++){
cin >> a[i] ;
}
int temp ; // 用来接受不了下一个数字
for (int i = 0 ; i < n ; i++ )
{
temp = a[i+1];
for(int j = 0; j <= i ; j++)
{
if(temp<a[j])
{
a[i+1] = a[j] ;
a[j] = temp ;
break;
}
}
for(int j = 0; j < n ; j++)
{
cout << a[j] << " ";
}
cout << endl ;
}
}
该状态下的错误答案: