From 1dfa86844cf6b1db1898358730feb270e49f0278 Mon Sep 17 00:00:00 2001 From: flykhan Date: Wed, 21 Jun 2023 14:30:56 +0800 Subject: [PATCH] day7 coding --- day7/d1.cpp | 14 ++++++++++++++ day7/d10.cpp | 20 ++++++++++++++++++++ day7/d11.cpp | 17 +++++++++++++++++ day7/d12.cpp | 21 +++++++++++++++++++++ day7/d13.cpp | 31 +++++++++++++++++++++++++++++++ day7/d14.cpp | 25 +++++++++++++++++++++++++ day7/d15.cpp | 22 ++++++++++++++++++++++ day7/d16.cpp | 28 ++++++++++++++++++++++++++++ day7/d17.cpp | 28 ++++++++++++++++++++++++++++ day7/d18.cpp | 37 +++++++++++++++++++++++++++++++++++++ day7/d2.cpp | 16 ++++++++++++++++ day7/d3.cpp | 21 +++++++++++++++++++++ day7/d4.cpp | 26 ++++++++++++++++++++++++++ day7/d5.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ day7/d6.cpp | 30 ++++++++++++++++++++++++++++++ day7/d7.cpp | 24 ++++++++++++++++++++++++ day7/d8.cpp | 21 +++++++++++++++++++++ day7/d9.cpp | 13 +++++++++++++ 18 files changed, 442 insertions(+) create mode 100644 day7/d1.cpp create mode 100644 day7/d10.cpp create mode 100644 day7/d11.cpp create mode 100644 day7/d12.cpp create mode 100644 day7/d13.cpp create mode 100644 day7/d14.cpp create mode 100644 day7/d15.cpp create mode 100644 day7/d16.cpp create mode 100644 day7/d17.cpp create mode 100644 day7/d18.cpp create mode 100644 day7/d2.cpp create mode 100644 day7/d3.cpp create mode 100644 day7/d4.cpp create mode 100644 day7/d5.cpp create mode 100644 day7/d6.cpp create mode 100644 day7/d7.cpp create mode 100644 day7/d8.cpp create mode 100644 day7/d9.cpp diff --git a/day7/d1.cpp b/day7/d1.cpp new file mode 100644 index 0000000..955dac6 --- /dev/null +++ b/day7/d1.cpp @@ -0,0 +1,14 @@ +// 测试单分支结构 +#include + +using namespace std; + +int main() +{ + cout << "请输入一个数: "; + int n; + cin >> n; + if (n % 2 == 0) + cout << n << "666" << endl; + return 0; +} \ No newline at end of file diff --git a/day7/d10.cpp b/day7/d10.cpp new file mode 100644 index 0000000..380acbf --- /dev/null +++ b/day7/d10.cpp @@ -0,0 +1,20 @@ +#include + +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; +} \ No newline at end of file diff --git a/day7/d11.cpp b/day7/d11.cpp new file mode 100644 index 0000000..648c3ec --- /dev/null +++ b/day7/d11.cpp @@ -0,0 +1,17 @@ +#include + +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; +} \ No newline at end of file diff --git a/day7/d12.cpp b/day7/d12.cpp new file mode 100644 index 0000000..9a7d3c0 --- /dev/null +++ b/day7/d12.cpp @@ -0,0 +1,21 @@ +#include + +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; +} \ No newline at end of file diff --git a/day7/d13.cpp b/day7/d13.cpp new file mode 100644 index 0000000..b625223 --- /dev/null +++ b/day7/d13.cpp @@ -0,0 +1,31 @@ +// 输入一个正整数,输出它的二进制表示 +#include + +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; +} \ No newline at end of file diff --git a/day7/d14.cpp b/day7/d14.cpp new file mode 100644 index 0000000..ad6d3aa --- /dev/null +++ b/day7/d14.cpp @@ -0,0 +1,25 @@ +#include + +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; +} \ No newline at end of file diff --git a/day7/d15.cpp b/day7/d15.cpp new file mode 100644 index 0000000..6b21c37 --- /dev/null +++ b/day7/d15.cpp @@ -0,0 +1,22 @@ +#include + +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; +} \ No newline at end of file diff --git a/day7/d16.cpp b/day7/d16.cpp new file mode 100644 index 0000000..18d53ba --- /dev/null +++ b/day7/d16.cpp @@ -0,0 +1,28 @@ +#include + +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; +} \ No newline at end of file diff --git a/day7/d17.cpp b/day7/d17.cpp new file mode 100644 index 0000000..a16bf7f --- /dev/null +++ b/day7/d17.cpp @@ -0,0 +1,28 @@ +#include + +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; +} \ No newline at end of file diff --git a/day7/d18.cpp b/day7/d18.cpp new file mode 100644 index 0000000..9ef91f4 --- /dev/null +++ b/day7/d18.cpp @@ -0,0 +1,37 @@ +#include + +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; +} \ No newline at end of file diff --git a/day7/d2.cpp b/day7/d2.cpp new file mode 100644 index 0000000..42eab32 --- /dev/null +++ b/day7/d2.cpp @@ -0,0 +1,16 @@ +// 测试双分支结构 +#include + +using namespace std; + +int main() +{ + cout << "请输入一个数: "; + int n; + cin >> n; + if (n % 2 == 0) + cout << n << "是偶数" << endl; + else + cout << n << "是奇数" << endl; + return 0; +} \ No newline at end of file diff --git a/day7/d3.cpp b/day7/d3.cpp new file mode 100644 index 0000000..8c8ea60 --- /dev/null +++ b/day7/d3.cpp @@ -0,0 +1,21 @@ +// 测试多分支结构 +#include +#include // 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; + } +} \ No newline at end of file diff --git a/day7/d4.cpp b/day7/d4.cpp new file mode 100644 index 0000000..3a590f1 --- /dev/null +++ b/day7/d4.cpp @@ -0,0 +1,26 @@ +// 嵌套分支 +#include + +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; +} \ No newline at end of file diff --git a/day7/d5.cpp b/day7/d5.cpp new file mode 100644 index 0000000..a35a307 --- /dev/null +++ b/day7/d5.cpp @@ -0,0 +1,48 @@ +// 测试switch分支 +#include +#include + +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; +} \ No newline at end of file diff --git a/day7/d6.cpp b/day7/d6.cpp new file mode 100644 index 0000000..aada8e3 --- /dev/null +++ b/day7/d6.cpp @@ -0,0 +1,30 @@ +// 输入0,1,2三个数值,分别代表玩家的角色,0代表`亚索`、1代表`盲僧`、2代表`提莫`,输出“您选择的角色为 xxx”。 +#include + +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; +} \ No newline at end of file diff --git a/day7/d7.cpp b/day7/d7.cpp new file mode 100644 index 0000000..e4f139c --- /dev/null +++ b/day7/d7.cpp @@ -0,0 +1,24 @@ +// for循环测试: 使用位运算符实现以下条件:判断一个数的二进制表示中有多少个 1 +#include +#include + +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; +} \ No newline at end of file diff --git a/day7/d8.cpp b/day7/d8.cpp new file mode 100644 index 0000000..2018b85 --- /dev/null +++ b/day7/d8.cpp @@ -0,0 +1,21 @@ +// 输入一个数,输出小于这个数所有偶数 +#include + +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; +} \ No newline at end of file diff --git a/day7/d9.cpp b/day7/d9.cpp new file mode 100644 index 0000000..9b495f5 --- /dev/null +++ b/day7/d9.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + int i = 1, total = 0; + for (; i <= 100; i++) + total += i; + + cout << "1~100的和为: " << total << endl; + return 0; +} \ No newline at end of file