4.6
1 do while循环,此循环与while循环的不同之处在于该循环先循环一次在进行条件判断,while循环先进行条件判断在进行循环;特别注意的一个点,do while循环后有分号
#include<iostream>
using namespace std;
int main()
{
int num=0;
do
{
cout<<num<<endl;
num++;
}while(num<10);
system("pause");
return 0;
}
2 水仙花数案例 一个三位数的每个位上的数的三次方求和后等于该数
水仙花数关键找出三个不同位上的数;个位数 取模于10;十位数 除以10变两位数后取模于10;百位数除以100;
#include<iostream>
using namespace std;
int main()
{
//水仙花数案例
//先找出所有的三位数
int num=100;
int a=0;
int b=0;
int c=0;
do
{
//判断水仙花数
a=num%10;//个位数
b=num/10%10;//十位数
c=num/100;//百位数
if(a*a*a+b*b*b+c*c*c==num)//幂次方的表示
{
cout<<num<<endl;
}
num++;
}while(num<1000);
system("pause");
return 0;
}
3 for循环语句,首先是for循环的格式;其次是for循环的执行顺序问题;
表达式 for(创建变量;条件判断;循环)for循环结构清晰使用度高;
#include<iostream>
using namespace std;
int main()
{
for(int a=0;a<10;a++)
{
cout<<a<<endl;
}
system("pause");
return 0;
}
4 敲桌子案例
#include<iostream>
using namespace std;
int main()
{
//敲桌子
//先打印1-99的数
for(int a=1;a<100;a++)
{
//找出7的倍数,带有数字7的数,打印为敲桌子
if(a%7==0||a%10==7||a/10==7)
{
cout<<"敲桌子"<<endl;
}
else
{
cout<<a<<endl;
}
}
system("pause");
return 0;
}
5 嵌套循环,打印星图
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=10;i++)
{
for(int j=0;j<=10;j++)
{
cout<<"* ";
}
cout<<endl;
}
system("pause");
return 0;
}
6 乘法口诀表案例
#include<iostream>
using namespace std;
int main()
{
//乘法口诀表
for(i=1;i<10;i++)
{
for(int j=1;j<=i;j++)
{
cout<<j<<"*"<<i<<"="<<j*i<<"\t"
}
cout<<endl;
}
system("pause");
return 0;
}
7 break语句 continue语句 goto语句
break语句为结束当前的循环;continue语句为跳过该条循环继续执行以下的循环;goto语句为跳到指定的标志处的循环不执行中间被调过的循环;
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=20;i++)
{
if(i==10)
{
break;
}
else
{
cout<<i<<endl;
}
}
for(int j=1;j<=20;j++)
{
if(j%2==0)
{
continue;
}
else
{
cout<<j<<endl;
}
}
cout<<"asd\te"<<endl;
goto FLAG;
cout<<"1\td"<<endl;
cout<<"ds\t432"<<endl;
FLAG:
cout<<"c\tv"<<endl;
system("pause");
return 0;
}
8 一维数组 首先为一维数组几种不同的定义方式,其次为一维数组的特点
一维数组的三种定义:数据类型 数组名称 [ 元素个数 ] ;数据类型 数组名称 [ 元素个数 ] ={ 各个元素 };数据类型 数组名称 [ ] ={ 各个元素};
一维数组特点 存在一块连续的内存空间,各个元素的数据类型相同;
#include<iostream>
using namespace std;
int main()
{
char str1[5];
str1[0]='a';
str1[1]='d';
str1[2]='c';
cout<<str1[1]<<endl;
cout<<str1[4]endl;
int str2[5]={0,1,1,4,7};
int str3[ ]={2,4,8,4};
cout<<str2[3]<<endl;
cout<<str3[1]<<endl;
system("pause");
return 0;
}
9 一维数组名称的应用
一维数组名可用于查看数组的所占内存空间,数组名是一常量不可赋值操作;一维数组名可用于查看数组中首元素的地址
遗留问题 字符型数组的首地址表示
#include<iostream>
using namespace std;
int main()
{
int arr1[] = { 1,2,3,4,5 };
char arr2[] = { 'a','b','c','f' };
cout << "arr1所占内存的大小为:" << sizeof(arr1) << endl;
cout << "arr2所占内存的大小为:" << sizeof(arr2) << endl;
cout << arr2[2] << endl;
//数组首地址的表示
cout << "arr1首地址为:" << arr1 << endl;//可加int强制为整型输出
cout << "arr1首地址为:" << &arr1[0] << endl;
//cout << &arr2[0] << endl; 字符型数组的首地址表示
system("pause");
return 0;
}
10 一维数组案例---五只小猪称体重
#include<iostream>
using namespace std;
int main()
{
int arr[]={256,261,262,262,275};
int max=0;
int num=0;
while(num<5)
{
if(arr[num]>max)
{
max=arr[num];
}
num++;
}
cout<<max<<endl;
system("pause");
return 0;
}
11 一维数组案例——元素逆置
#include<iostream>
using namespace std;
int main()
{
//创建数组
int arr[] = { 1,5,3,6,7 };
int len = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < len; i++)
{
cout << arr[i] << endl;
}
cout << "******" << endl;
//元素逆置
int start = 0;
int end = len - 1;
while(start<end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
for (int i = 0; i < len; i++)
{
cout << arr[i] << endl; ;
}
//cout << "逆置后的数组:" << endl;
system("pause");
return 0;
}
12简单算法——冒泡排序
冒泡排序的两个关键核心,其一为排序的轮数,排序的轮数为数组的元素个数减一;其二为每轮排序的次数;
#include<iostream>
using namespace std;
int main()
{
//创建一个数组
int arr[] = { 2,1,5,4,65,76,87,9 };
int len = sizeof(arr) / sizeof(arr[0]);
//排序前的数组
for (int i = 0; i < len; i++)
{
cout << arr[i]<<" ";
}
cout << endl;
//冒泡排序
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
//排序后的数组
for (int i = 0; i < len; i++)
{
cout << arr[i] <<" ";
}
cout << endl;
system("pause");
return 0;
}