NO.30十六届蓝桥杯备战|C++输入输出|单组测试用例|多组测试用例|isalpha|逗号表达式(C++)

OJ题⽬输⼊情况汇总

  • 单组测试用例:程序运行一次,只处理一组数据
  • 多组测试用例:程序运行一次,会处理多组数据
    • 测试数据组数已知(输入)
    • 测试数据组数未知
    • 特殊值结束测试数据

单组测试⽤例

B2009 计算 (a+b)/c 的值 - 洛谷
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int a, b, c;
    cin >> a >> b >> c;
    cout << (a + b) / c << endl;
    
    return 0;
}
B2081 与 7 无关的数 - 洛谷
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int sum = 0;
    for (int i = 1; i <= n; i++)
    {
        if ((i % 7 != 0) && (i % 10 != 7) && (i / 10 != 7))
        {
            sum += i * i;
        }
    }
    cout << sum << endl;
    
    return 0;
}

多组测试用例

测试数据组数已知

多组输入a+b
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    int a, b;
    while (n--)
    {
        cin >> a >> b;
        cout << a + b << endl;
    }
    
    return 0;
}
B2064 斐波那契数列 - 洛谷
c++ 复制代码
#include <iostream>
using namespace std;

int n;

int main()
{
    cin >> n;
    int a = 0;
    while (n--)
    {
        cin >> a;

        int i = 1, j = 1, k = 1;
        while (a >= 3)
        {
            k = i + j;
            i = j;
            j = k;
            a--;
        }
        cout << k << endl;
    }

    return 0;
}
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

const int N = 40;
int a[N] = {0, 1, 1};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int i = 0;
    for (i = 3; i <= 30; i++)
    {
        a[i] = a[i-1] + a[i-2];        
    }
    int n;
    cin >> n;
    int k;
    while (n--)
    {
        cin >> k;
        cout << a[k] << endl;
    }
    
    return 0;
}
B3769 [语言月赛202305] 制糊串 - 洛谷
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s, t;
    cin >> s >> t;
    int q;
    cin >> q;
    int l1, r1, l2, r2;
    while (q--)
    {
        cin >> l1 >> r1 >> l2 >> r2;
        string s1 = s.substr(l1 - 1, r1 - l1 + 1);
        string t1 = t.substr(l2 - 1, r2 - l2 + 1);
        if (s1 < t1)
            cout << "yifusuyi" <<endl;
        else if (s1 > t1)
            cout << "erfusuer" << endl;
        else
            cout << "ovo" << endl;
    }

    
    return 0;
}

题⽬中说"有q次询问",意思是程序要处理q组测试数据,(也就是对应 q 次循环),我们

要针对每次询问,给出⼀个结果。

其实就是之前的单组测试变成了 q 组测试,在之前的代码上套⼀层 while 循环即可。当有 q 次询问的时候, while(q--) 是⾮常⽅便的⽅式。然后就按照单组输⼊的⽅式处理每组输⼊的数据就好了。

测试数据组数未知

多组输入a+b 2
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int a, b;
    while (cin >> a >> b)
    {
        cout << a + b << endl;
    }
    
    return 0;
}

cin >> a; 会返回⼀个流对象的引⽤,即 cin 本⾝。在C++中,流对象 cin 可以被⽤作布尔值来检查流的状态。如果流的状态良好(即没有发⽣错误),流对象的布尔值为true 。如果发⽣错误(如遇到输⼊结束符或类型不匹配),布尔值为 false 。

在 while (cin >> a >> b) 语句中,循环的条件部分检查 cin 流的状态。如果流成功读取到2个值, cin >> a >> b 返回的流对象 cin 将被转换为 true ,循环将继续。如果读取失败(例如遇到输⼊结束符或⽆法读取到2个值), cin >> a >> b 返回的流对象 cin 将被转换为 false ,循环将停⽌。

数字三角形
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    while (cin >> n)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                cout << j << " ";
            }
            cout << endl;
        }
    }
    
    return 0;
}
定位查找
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    int a;
    while (cin >> n)
    {
        vector<int> s(n);
        for (auto &x : s)
        {
            cin >> x;
        }
        cin >> a;
        int i = 0;
        for (i = 0; i < n; i++)
        {
            if (a == s[i])
            {
                cout << i << endl;
                break;
            }

        }
        if (i == n)
            cout << "No" << endl;
    }
    
    return 0;
}

特殊值结束测试数据

字符统计
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int l, d, o;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int ch = 0;
    while ((ch = getchar()) != '?')
    {
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
            l++;
        else if (ch >= '0' && ch <= '9')
            d++;
        else
            o++;
    }
    cout << "Letters=" << l << endl;
    cout << "Digits=" << d << endl;
    cout << "Others=" << o << endl;

    return 0;
}
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int l, d, o;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int ch = 0;
    while ((ch = getchar()) != '?')
    {
        if (islower(ch) || isupper(ch))
            l++;
        else if (isdigit(ch))
            d++;
        else
            o++;
    }
    cout << "Letters=" << l << endl;
    cout << "Digits=" << d << endl;
    cout << "Others=" << o << endl;

    return 0;
}
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int l, d, o;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int ch = 0;
    while ((ch = getchar()) != '?')
    {
        if (isalpha(ch))
            l++;
        else if (isdigit(ch))
            d++;
        else
            o++;
    }
    cout << "Letters=" << l << endl;
    cout << "Digits=" << d << endl;
    cout << "Others=" << o << endl;

    return 0;
}
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int l, d, o;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    getline(cin, s);
    s.pop_back();

	for (auto e : s)
	{
        if (isalpha(e))
            l++;
        else if (isdigit(e))
            d++;
        else
            o++;
	}
    cout << "Letters=" << l << endl;
    cout << "Digits=" << d << endl;
    cout << "Others=" << o << endl;

    return 0;
}
多组数据a+b 3
c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int a, b;
    while(cin >> a >> b, a || b)
    {
        cout << a + b << endl;
    }
    
    
    return 0;
}

逗号表达式:逗号隔开的一串表达式

特点:

  1. 从左向右依次计算
  2. 整个表达式的结果,是最后一个表达式结果
相关推荐
Dream it possible!4 小时前
LeetCode 热题 100_字符串解码(71_394_中等_C++)(栈)
c++·算法·leetcode
My Li.5 小时前
c++的介绍
开发语言·c++
Alan-Xia6 小时前
使用jest测试用例之入门篇
前端·javascript·学习·测试用例
天才测试猿6 小时前
功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
邪恶的贝利亚7 小时前
C++之序列容器(vector,list,dueqe)
开发语言·c++
原来是猿7 小时前
蓝桥备赛(13)- 链表和 list(上)
开发语言·数据结构·c++·算法·链表·list
成功助力英语中国话7 小时前
SDK编程,MFC编程,WTL编程之间的关系
c++·mfc
仟濹7 小时前
【算法 C/C++】二维差分
c语言·c++·算法
总斯霖8 小时前
题解:士兵排列
数据结构·c++·算法
稳兽龙8 小时前
P4268 [USACO18FEB] Directory Traversal G
c++·算法·换根dp