/*三只小猪称体重*/
#include <iostream>
using namespace std;
int main()
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
cout << "请输入a猪的体重" << endl;
cin >> num1;
cout << "请输入b猪的体重" << endl;
cin >> num2;
cout << "请输入c猪的体重" << endl;
cin >> num3;
if (num1 > num2)
{
if (num1 > num3)
{
cout << "小猪a最重" << endl;
}
else
{
cout << "小猪c最重" << endl;
}
}
else if (num1 < num2)
{
if (num2 > num3)
{
cout << "小猪b最重" << endl;
}
else
{
cout << "小猪c最重" << endl;
}
}
system("pause");
return 0;
}
2
/*猜数字*/
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
//1.生成随机数
//需要添加随机数种子,作用利用当前系统的时间生成随机数,
//防止每次的随机数都一样
srand((unsigned int)time(NULL));
int num = rand() % 100 + 1;//生成1-100的随机数
int a = 0;
while (1)
{
cout << "请猜一个数字" << endl;
cin >> a;
if (a > num)
{
cout << "猜大了" << endl;
}
else if (a < num)
{
cout << "猜小了" << endl;
}
else
{
cout << "猜对了" << endl;
break;
}
}
system("pause");
return 0;
}
3
/*水仙花数*/
水仙花数是指一个3位数,它的每个位的数字的三次方之和等于他本身
例如 153
1**3+5**3+3**3=153
取模获得个位:153%10=3
获取十位;153/10=15, 15%10=5
获取百位;153/100=1
#include <iostream>
using namespace std;
int main()
{
//1.先打印所有的三位数
int num = 100;
do
{
//2.从所有三位数中找到水仙花数
int a = 0;//个位
int b = 0;//十位
int c = 0;//百位
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);
}
4
/*敲桌子*/
#include <iostream>
using namespace std;
int main()
{
//先找出1-100的数
for (int i = 1; i < 100; i++)
{
//7的倍数,十位是7,个位是7的特殊数字,输出敲桌子
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
5
/*乘法口诀表*/
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 9; i++)//列数小于当前行数
{
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << i * j << " ";
}
cout << endl;
}
system("pause");
return 0;
}
6
//案例1:五只小猪称体重
#include <iostream>
using namespace std;
int main()
{
int max = 0;
int arr[5] = { 300,350,200,400,250 };
for (int i = 0; i < 5; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
cout << "最胖的那只猪是"<<max <<"kg"<< endl;
}
7
/*1.元素逆置*/
#include <iostream>
using namespace std;
int main()
{
int arr[5] = { 1,3,2,5,4 };
cout << "数组逆置前:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
//2、实现逆置
//2.1、记录起始下标位置
//2.2、记录结束下标位置
//起始与结束下标位置互换
//2.3、起始++,结束——
//2.4循环执行知道起始>=结束位置
int start = 0;
int end = sizeof(arr) / sizeof(arr[0]) - 1;
while (start < end)
{
int temp =arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
cout << "数组逆置后:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
}
8
/*2.冒泡排序*/
#include <iostream>
using namespace std;
int main()
{
//利用冒泡排序实现升序排列
int arr[9] = { 4, 2, 8, 0, 5, 7, 1, 3, 9 };
cout << "排序前" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
//开始冒泡排序
//总共排序轮数为 元素个数-1
for (int i = 0; i < 9 - 1; i++)
{
//内层循环对比 次数=元素个数-当前轮数-1
for (int j = 0; j < 9 - i - 1; j++)
{
//如果第一个数字,比第二个数字大,交换两个数字
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "排序后的结果" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
9
/*指针和函数*/
#include <iostream>
using namespace std;
void swap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "swap a=" << a << endl;
cout << "swap b=" << b << endl;
}
void swap02(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
cout << "*p1=" << *p1 << endl;
cout << "*p2=" << *p2 << endl;
}
int main()
{
int a = 10;
int b = 20;
swap01(a, b);//值传递
swap02(&a, &b);//地址传递
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
return 0;
}
10
/*函数和指针*/
#include <iostream>
using namespace std;
//冒泡排序函数
void bubbleSort(int *arr,int len )
{
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;
}
}
}
}
void printArray(int *arr,int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int arr[10] = { 4, 3, 6, 9, 1, 2, 10, 8, 7, 5 };
int len = sizeof(arr) / sizeof(arr[0]);//数组长度
bubbleSort(arr, len);
printArray(arr, len);
system("pause");
return 0;
}
11
/*结构体嵌套结构体*/
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
int age;
int sc;
};
struct teacher
{
int id;
string name;
int age;
struct student stu;
};
int main()
{
//创建老师
teacher t;
t.id = 10000l;
t.name = "老王";
t.age = 50;
t.stu.name = "小王";
t.stu.age = 20;
t.stu.sc = 60;
cout << " 老师姓名: " << t.name << " 老师编号: " << t.id << " 老师年龄: " << t.age
<< " 老师辅导的学神姓名: " << t.stu.name << " 学生年龄:" << t.stu.age
<<" 学生分数: " << t.stu.sc << endl;
system("pause");
return 0;
}
12
/*结构体案例1*/
#include <iostream>
#include <string>
#include <Ctime>
using namespace std;
struct Student
{
string sName;
int score;
};
struct Teacher
{
string tName;
struct Student sArray[5];
};
//给老师和学生赋值的函数
void allocateSpace(struct Teacher tArray[], int len)
{
string nameSeed = "ABCDE";
//给老师赋值
for (int i = 0; i < len; i++)
{
tArray[i].tName = "Teacher_";
tArray[i].tName += nameSeed[i];
for (int j = 0; j < 5; j++)
{
tArray[i].sArray[j].sName = "Student_";
tArray[i].sArray[j].sName += nameSeed[j];
int random = rand() % 61 + 40;//40~100
tArray[i].sArray[j].score = random;
}
}
}
//打印所有信息
void printInfo(struct Teacher tArray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "老师的姓名: " << tArray[i].tName << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t学生姓名:" << tArray[i].sArray[j].sName <<
" 考试分数:" << tArray[i].sArray[j].score << endl;
}
}
}
int main()
{
//随机种子
srand((unsigned int)time(NULL));
struct Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
//打印所有老师及所带学生的信息
printInfo(tArray, len);
}