day7 coding

This commit is contained in:
flykhan 2023-06-21 14:30:56 +08:00
parent 690d495f38
commit 1dfa86844c
18 changed files with 442 additions and 0 deletions

14
day7/d1.cpp Normal file
View File

@ -0,0 +1,14 @@
// 测试单分支结构
#include <iostream>
using namespace std;
int main()
{
cout << "请输入一个数: ";
int n;
cin >> n;
if (n % 2 == 0)
cout << n << "666" << endl;
return 0;
}

20
day7/d10.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <iostream>
using namespace std;
int main()
{
int i = 1, total1 = 0, total2 = 0;
for (; i <= 100; i++)
{
if (i % 2 == 0)
total1 += i;
else
total2 += i;
}
cout << "1~100的偶数和为: " << total1 << endl;
cout << "1~100的奇数和为: " << total2 << endl;
return 0;
}

17
day7/d11.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
using namespace std;
int main()
{
int i = 1, total = 0;
while (i <= 100)
{
if (i % 3 == 0)
total += i;
i++;
}
cout << "1~100的3的倍数和为: " << total << endl;
return 0;
}

21
day7/d12.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <iostream>
using namespace std;
int main()
{
char human_readable[] = "BKMGT"; // 人类可读的单位: 字节、千字节、兆字节、吉字节、太字节
int n;
int i = 0;
cout << "请输入一个数:";
cin >> n;
while (n >= 1024) // 原理: 除以 1024 直到小于 1024
{
n /= 1024;
i++;
}
cout << n << human_readable[i] << endl;
return 0;
}

31
day7/d13.cpp Normal file
View File

@ -0,0 +1,31 @@
// 输入一个正整数,输出它的二进制表示
#include <iostream>
using namespace std;
int main()
{
char arr[32] = "";
int len = 0;
int n;
cout << "请输入一个数:";
cin >> n;
// 逐位取出
while (n)
{
if (n & 1)
arr[len++] = '1';
else
arr[len++] = '0';
n >>= 1;
}
// 逆序输出
while (len)
cout << arr[--len];
cout << endl;
return 0;
}

25
day7/d14.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
using namespace std;
int main()
{
char arr[32] = "";
int len = 0;
int n;
cout << "请输入一个正整数: ";
cin >> n;
while (n)
{
arr[len++] = n % 2 + 48; //// 将数字0或1转成 字符'0'或'1'; 48是'0'的ASCII码值
n /= 2;
}
while (len)
cout << arr[--len];
cout << endl;
return 0;
}

22
day7/d15.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "输入一个正整数: ";
cin >> n;
int isStartShow = 0;
for (int i = 31; i >= 0; i--)
{
if ((n >> i) & 1) // 从最高位开始判断: 如果为 0 则不显示
isStartShow = 1;
if (isStartShow)
cout << ((n >> i) & 1);
}
cout << endl;
return 0;
}

28
day7/d16.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <iostream>
using namespace std;
int main()
{
string name, pwd; // 用户名和密码
int n = 3; // 剩余尝试次数
while (n--)
{
cout << "请输入用户名: ";
cin >> name;
cout << "请输入密码: ";
cin >> pwd;
if (name == "fly" && pwd == "123")
{
cout << "登录成功" << endl;
break;
}
else if (n == 0)
break;
else
cout << "用户名或密码错误, 你还有" << n << "次机会" << endl;
}
return 0;
}

28
day7/d17.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <iostream>
using namespace std;
int main()
{
int total = 0;
// 实现方法 1
int i = 100;
while (i--)
{
if (i % 5 == 0)
continue;
total += i;
}
// 实现方法 2
// for (int i = 1; i < 100; i++)
// {
// if (i % 5 == 0)
// continue;
// total += i;
// }
cout << "100以内除了5的倍数之外的数之和为: " << total << endl;
return 0;
}

37
day7/d18.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
using namespace std;
int main()
{
int total = 0;
// goto 语句的使用
// 实现方法 1
// int i = 100;
// flag:
// while (i--)
// {
// if (i % 5 == 0)
// goto flag;
// total += i;
// }
// 实现方法 2
int i = 0;
abc:
if (i == 100)
goto show;
i++;
if (i % 5 == 0)
goto abc;
total += i;
if (i < 100)
goto abc;
show:
cout << "100以内除了5的倍数之外的数之和为: " << total << endl;
return 0;
}

16
day7/d2.cpp Normal file
View File

@ -0,0 +1,16 @@
// 测试双分支结构
#include <iostream>
using namespace std;
int main()
{
cout << "请输入一个数: ";
int n;
cin >> n;
if (n % 2 == 0)
cout << n << "是偶数" << endl;
else
cout << n << "是奇数" << endl;
return 0;
}

21
day7/d3.cpp Normal file
View File

@ -0,0 +1,21 @@
// 测试多分支结构
#include <iostream>
#include <cmath> // c++ 数学库: 提供 abs() 函数
using namespace std;
int main()
{
cout << "请输入2个数: ";
int a, b;
cin >> a >> b;
if (a >= 0 && b >= 0)
cout << "a + b = " << a + b << endl;
else if (a < 0 && b < 0)
cout << "a * b = " << a * b << endl;
else
{
cout << "a 和 b 差值: " << abs(a - b) << endl;
}
}

26
day7/d4.cpp Normal file
View File

@ -0,0 +1,26 @@
// 嵌套分支
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入一个整数: ";
cout << "请输入一个整数: ";
cin >> n;
if (n % 3 == 0 || n % 5 == 0)
{
// n 能被 3 或 5 整除
if (n % 3 == 0)
cout << "Fizz";
if (n % 5 == 0)
cout << "Buzz";
cout << endl;
}
else
cout << n << endl;
return 0;
}

48
day7/d5.cpp Normal file
View File

@ -0,0 +1,48 @@
// 测试switch分支
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char const *argv[])
{
int n;
cout << "输入您的成绩:" << endl;
cin >> n;
switch (n)
{
case 100:
cout << "你是最优秀的 ";
case 99:
case 98:
case 97:
case 96:
case 95:
case 94:
case 93:
case 92:
case 91:
case 90:
cout << "A";
break;
case 89:
case 88:
case 87:
case 86:
case 85:
case 84:
case 83:
case 82:
case 81:
case 80:
cout << "B";
break;
default:
cout << "D";
break;
}
cout << endl;
return 0;
}

30
day7/d6.cpp Normal file
View File

@ -0,0 +1,30 @@
// 输入012三个数值分别代表玩家的角色0代表`亚索`、1代表`盲僧`、2代表`提莫`,输出“您选择的角色为 xxx”。
#include <iostream>
using namespace std;
int main()
{
short n;
cout << "请选择角色: 0-亚索, 1-盲僧, 2-提莫";
cin >> n;
cout << "您选择的角色为: ";
switch (n)
{
default:
cout << "选择错误";
break;
case 0:
cout << "亚索";
break;
case 1:
cout << "盲僧";
break;
case 2:
cout << "提莫";
break;
}
cout << endl;
return 0;
}

24
day7/d7.cpp Normal file
View File

@ -0,0 +1,24 @@
// for循环测试: 使用位运算符实现以下条件:判断一个数的二进制表示中有多少个 1
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n = 0;
cout << "请输入一个整数: ";
cin >> n;
int count = 0; // 统计 1 的个数
int temp = 0; // 临时变量
for (int i = 0; temp < n; i++)
{
temp = 1 << i;
if ((n & temp) == temp)
count++;
}
cout << n << " 的二进制表示中有 " << count << " 个 1" << endl;
return 0;
}

21
day7/d8.cpp Normal file
View File

@ -0,0 +1,21 @@
// 输入一个数,输出小于这个数所有偶数
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入一个数:";
cin >> n;
int i = 0;
while (i < n)
{
if (i % 2 == 0)
cout << i << "\t";
i++;
}
cout << endl;
return 0;
}

13
day7/d9.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
using namespace std;
int main()
{
int i = 1, total = 0;
for (; i <= 100; i++)
total += i;
cout << "1~100的和为: " << total << endl;
return 0;
}