编程基础03

循环逻辑语句

  • while

    先判断再执行 判断次数比执行次数多一次

    1
    2
    3
    4
    5
    int i = 0;
    while(i···循环条件){
    ···
    i++;
    }
  • for

    1
    2
    3
    for(设置变量;循环边界条件;i++){
    //括号当中的语句可以灵活摘出放在循环体内,要注意放置位置的逻辑顺序
    }
  • do···while

    先执行再判断 执行次数与判断次数相等

    1
    2
    3
    4
    do{

    }
    while();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;

int main()
{
// while 判断次数比执行次数多一次
// 1.循环变量的初始化
//int i = 1;
//while (i <= 10) // 2.循环的判断条件
//{
// cout << i << endl;
// // 3.循环变量的变化
// i++;
//}

// for循环
// 灵活用法 -> 需要思考逻辑问题
for (int i = 1; i <= 10; i++)
{
cout << i << endl;
}

// do...while...判断次数与执行次数相等
// 猜数字
int pw = 7, ans;
do
{
cin >> ans;
} while (pw == ans);

// goto语句 了解即可

return 0;
}

作业

1304收费

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;

int main()
{
double w;
double ans;
cin >> w;
if (w <= 20) ans = w * 1.68;
else ans = w * 1.98;
printf("%.2lf", ans);
return 0;
}

1320分段函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;

int main()
{
/*
* y=−x+2.5;0≤x<5
* y=2−1.5(x−3)(x−3);5≤x<10
* y=x/2−1.5;10≤x<20
*/

double x, y;
cin >> x;
if (x >= 0 && x < 5)
{
y = -x + 2.5;
}
else if (x >= 5 && x < 10)
{
y = 2 - 1.5 * (x - 3) * (x - 3);
}
else
{
y = x / 2 - 1.5;
}
printf("%.3lf", y);
return 0;
}

课堂练习

LG1011上学迟到(优化版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
//#include<cmath>
using namespace std;
int main() {
int s, v;
cin >> s >> v;
int hour, minute;
//delta方便整型的向上取整,整型性能更高
int delta = (s % v == 0) ? 0 : 1;//三目运算符
//int timeAll = ceil(s / v) + 10;
int timeAll = s / v + 10 + delta;
int m = 480 -timeAll;
/*if (m>=0) {
hour = m / 60;
minute = m % 60;
printf("%02d:%02d", hour, minute );
}
else {
m = 24 * 60 + m;
hour = m / 60;
minute = m % 60;
printf("%02d:%02d", hour, minute);
}*/
if (m<0) {
m = 24 * 60 + m;
}
hour = m / 60;
minute = m % 60;
printf("%02d:%02d", hour, minute);
return 0;
}
  1. 全部化成最小单位minute进行计算 double型变量 使用ceil向上取整 if判断中有重复代码块

  2. 设置一个delta 运用三目运算符做整型的向上取整 性能更高 优化if语句

1317买笔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
using namespace std;

/*
* 想买尽量多的笔:全部购买4元的
* 不想有剩余钱:笔的数量不变,买更贵的笔
*/

int main()
{
int price_6 = 0, price_5 = 0, price_4 = 0;
int money;
cin >> money;
price_4 = money / 4;
int r = money % 4;
if (r == 1)
{
price_4 -= 1;
price_5 += 1;
}
else if (r == 2)
{
price_4 -= 1;
price_6 += 1;
}
else if(r == 3)
{
price_4 -= 2;
price_5 += 1;
price_6 += 1;
}

cout << price_6 << " " << price_5 << " " << price_4 << endl;
return 0;
}

1316简单计算器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
using namespace std;

int main()
{
int a, b;
char c; // '' 字符 "" 字符串
cin >> a >> b >> c;
//scanf("%d %d %c", &a, &b, &c); // 34 56 空格读入问题
if (c == '+')
{
cout << a + b << endl;
}
else if (c == '-')
{
cout << a - b << endl;
}
else if (c == '*')
{
cout << a * b << endl;
}
else if (c == '/')
{
if (b == 0)
{
cout << "Divided by zero!" << endl;
return 0;
}
cout << a / b << endl;
}
else
{
cout << "Invalid operator!" << endl;
}
return 0;
}


编程基础03
http://yjmanman.github.io/2024/09/10/03/
作者
YuJia
发布于
2024年9月10日
许可协议