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;
}
逗号表达式:逗号隔开的一串表达式
特点:
- 从左向右依次计算
- 整个表达式的结果,是最后一个表达式结果