分支嵌套

例题 一

B4260 GESP202503 时间跨越

1.读题
【问题条件】

2.思考
【建模算法】

3.编写调试

4.回顾

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int y, m, d, h, k;
    
    int y1,m1,d1,h1;
 
    cin >> y >> m >> d >> h >> k;
	
	//小时
	h += k;
	if (h >= 24) d += h / 24, h %= 24;
 
	//day 按月份分别处理,不同的月份,进位的大小不同。
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
    {
        if (d > 31) m += 1, d %= 31;
				    
    }
    else if (m == 2)
    {
        if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
        {
            if (d > 29)
                m += 1, d %= 29;
        }
        else
        {
            if (d > 28) m += 1, d %= 28;
        }
    }
    else // other months 30 days
    {
        if (d > 30) m += 1, d %= 30;
    }
 
	//月
    if (m > 12) y += 1, m %= 12;
 
    cout << y << " " << m << " " << d << " " << h << endl;
 
    return 0;
}

循环嵌套

例题二

B4002 GESP202406 平方之和

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n;
    cin >> n;
    while (n--) //for(int i=1;i<=n;i++)
    {
        int num;
        cin >> num;
 
		//全搜索找是否有解	
        bool flag = false; // char/ int/ long long/ double
        for (int x = 1; x * x <= num; x++) //减少循环次数的技巧 x*x
        {
            for (int y = 1; y * y <= num; y++)
            {
                if (x * x + y * y == num)
                {
                    // cout << x << " " << y << endl;
                    flag = true;
                    break;
                }
            }
            if (flag)
                break;
        }
        
        if (flag)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
 
    return 0;
}

练习1 B4259 GESP202503 等差矩阵

答案

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
   
    int n, m;
 
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            cout << i * j << " ";
        cout << endl;
    }
    return 0;
}

练习2

B4064 GESP202412 寻找数字

回家作业。

答案

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int t;
    cin >> t;
 
    while (t--) // 可以用for(int i = 0; i < t; i++)
    {
        int a;
 
        cin >> a;
 
        int ans = -1;
 
        for (int i = 1; i <= 100; i++) // 如果写i<=a循环太大了,或者写 i*i*i*i<=a
        {
            if (i * i * i * i == a)
            {
                ans = i;
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

例题三

B4065 GESP202412 数位和

#include <bits/stdc++.h>
using namespace std;
 
/*
1.读题
【问题条件】
数位和,还有一个是数据范围 10的12次方,超过21亿了,2*10的9次方;用long long
*/
 
int main()
{
    int n;
    int ans = 0;
 
    cin >> n;
 
    for (int i = 1; i <= n; i++)
    {
        long long num; 
        cin >> num;
 
        int sum = 0;
        
        while (num) //for(;num;)
        {
            int d= num%10;
            sum = sum+ d;
            num /= 10;
        }
 
        ans = max(ans, sum);
    }
 
    cout << ans << endl;
 
    return 0;
}

练习3.1

B4036 GESP202409 数位之和

答案

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n;
    cin >> n;
 
    while (n--)
    {
        int a;
        cin >> a;
        int sum = 0;
 
        while (a)
        {
            sum += a % 10;
            a /= 10;
        }
 
        if (sum % 7 == 0)
            cout << "Yes\n";
        else
            cout << "No\n";
    }
    return 0;
}
 

练习3.2 05-25

B4007 GESP202406 计数

答案

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n, k;
    cin >> n >> k;
    int sum = 0;
 
    for (int i = 1; i <= n; i++)
    {
        int num = i;
 
        while (num)
        {
            if (num % 10 == k)
                sum++;
            num /= 10;
        }
    }
 
    cout << sum << endl;
    return 0;
}

例题四

B3923 GESP202312 小杨做题

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int a, b, m, n;
    int ans = 0;
    cin >> a >> b >> m >> n;
 
    ans += a;
    ans += b;
 
    for (int i = 3; i <= n; i++)
    {
        int t = a + b;
        ans += t;
 
        a = b;
        b = t;
 
        if (t >= m)
            break;
    }
 
    cout << ans << endl;
    
    return 0;
}

例题五

B3866 GESP202309 数字黑洞

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n;
    cin >> n;
    int ans = 0;
 
    while (n != 495) 
    {
        int a, b, c;
        c = n % 10, b = (n / 10) % 10, a = (n / 100) % 10;
 
        int h1, h2, h3; // big to small
        h1 = max(a, max(b, c));
        h3 = min(a, min(b, c));
        h2 = a + b + c - h1 - h3;
 
        int hn, ln;
        hn = h1 * 100 + h2 * 10 + h3;
        ln = h3 * 100 + h2 * 10 + h1;
        n = hn - ln;
        ans++;
    }
 
    cout << ans;
    return 0;
}

总结

循环嵌套

多为数位问题,取数位的方法。

1 单独

数位代码
个位n % 10
十位n / 10 % 10
百位n / 100 % 10
千位n / 1000 % 10

2 循环

while(n)
{
	d=n%10;
	//处理d
	n/=10;
}
 

下一课:真题第二轮