目录
1 数对
时间限制:1000
内存限制:65536
给定2到15个不同的正整数,你的任务是计算这些数里面有多少个数对满足:数对中一个数是另一个数的两倍。 比如给定1 4 3 2 9 7 18 22,得到的答案是3,因为2是1的两倍,4是2个两倍,18是9的两倍。
输入
一行,给出2到15个两两不同且小于100的正整数。最后用0表示输入结束。
输出
一个整数,即有多少个数对满足其中一个数是另一个数的两倍。
样例输入
1 4 3 2 9 7 18 22 0
样例输出
3
解析:根据题目要求,进行循环查找,查找完满足数对中一个数是另一个数的两倍这个条件后直接输出,上code!
不准直接抄!!!
#include <iostream>
using namespace std;
int a[20], t[105];
int main(){
int i = 0, x;
cin >> x;
while (1)
{
if (x == 0)
break;//返回
a[i++] = x;
t[x]++;
cin >> x;
}
int ans = 0;//答案
for (int j = 0; j < i; j++)
if (t[a[j] * 2] > 0)
ans++;//如果满足,答案自增
cout << ans;//输出
return 0;
}
2 井和绳子
时间限制:1000
内存限制:65536
有A, B, C, D, E五家人共用一口井,已知井深不超过k米。A, B, C, D, E的绳长各不相同,而且厘米表示的绳长一定是整数。
从井口放下绳索正好达到水面时:
(a)需要A家的绳n1条接上B家的绳1条
(b)需要B家的绳n2条接上C家的绳1条
(c)需要C家的绳n3条接上D家的绳1条
(d)需要D家的绳n4条接上E家的绳1条
(e)需要E家的绳n5条接上A家的绳1条
问井深和各家绳长。
输入
输入只有1行。包括空格分开的6个整数。 第一个整数k(1 <= k <= 20),代表井的最大深度(单位:米)。 接下来是5个正整数n1, n2, n3, n4, n5。这五个整数的含义见上面的题目描述。
输出
输出只有1行。 如果找到了可行解,就输出6个整数,用空格分开,分别代表井的深度和A, B, C, D, E的绳长(单位都是厘米)。 如果有多组可行解,输出井的深度最小的那组解。 如果不存在可行解,就输出一行: not found。
样例输入
10 2 3 4 5 6
样例输出
721 265 191 148 129 76
解析:if判断,看代码。
不准直接抄!!!
#include <iostream>
using namespace std;
int main() {
int n[6], l, a, b, c, d, e, len;
cin >> l;
l *= 100;
for (int i = 1; i <= 5; i++)
cin >> n[i];
int flag = 0;
for (len = 1; len <= l; len++)
{
for (a = 1; a <= len; a++)
{
b = len - n[1] * a;
c = len - n[2] * b;
d = len - n[3] * c;
e = len - n[4] * d;
if (a == b || a == c || a == d || a == e || b == c || b == d || b == e ||
c == d || c == e || d == e || a <= 0 || b <= 0 || c <= 0 || d <= 0 ||
e <= 0)
continue;
if (e * n[5] + a == len)
{
printf("%d %d %d %d %d %d\n", len, a, b, c, d, e);
return 0;
}
}
}
cout << "no found";
return 0;
}
3 爬楼
时间限制:1000
内存限制:65536
已知楼梯的数量,可以每次走2级或者3级,求不同的走法数
例如:楼梯一共有7级,一共3种方法:2 2 3或者 2 3 2 或者 3 2 2。
输入
输入包含若干行,每行包含一个正整数N,代表楼梯级数,1 <= N <= 50。 最后一行为0,表示测试结束。
输出
不同的走法数,每一行输入对应一行输出。
样例输入
7
0
样例输出
3
解析:do_while循环解决,上code!
不准直接抄!!!
#include<iostream>
using namespace std;
long long a[55];
int main() {
a[1] = 0;
a[2] = 1;
a[3] = 1;
for(int i = 4; i <= 50; i++)
a[i] = a[i - 2] + a[i - 3];
int n;
do
{
cin >> n;
if(n)
cout << a[n] << endl;
}
while(n);
return 0;
}
4 表达式求值
时间限制:1000
内存限制:65536
输入一个布尔表达式,请你输出它的真假值。
比如:( V | V ) & F & ( F | V )
V表示true,F表示false,&表示与,|表示或,!表示非。
上式的结果是F。
输入
输入包含多行,每行一个布尔表达式,表达式中可以有空格,总长度不超过1000
输出
对每行输入,如果表达式为真,输出"V",否则出来"F"
样例输入
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))
样例输出
F
V
V
解析:while循环,详情看代码
不准直接抄!!!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
using namespace std;
char calculate(char x, char y, char oper)
{ // 计算 x oper y
bool a = (x == 'V'), b = (y == 'V'), ans;
if (oper == '|')ans = (a || b);
else if (oper == '&')ans = (a && b);
return ans ? 'V' : 'F';
}
char reverse(char x)
{
if (x == 'V')
return 'F';
return 'V';
}
int main() {
string in;
int i, j, len;
while (getline(cin, in))
{
stack< char > Oper, num; //oper保存运算符,num保存运算结果
queue< char > s; // s就是前缀表达式
len = in.length();
i = len;
in = " " + in;
while (i > 0)
{ // 从右往左,中缀表达式转 前缀表达式
if (in[i] == ' ')
i--; continue;
else if (isalpha(in[i]))
s.push(in[i--]);
else if (in[i] == '!') //最高级的运算,直接进入表达式
s.push(in[i--]);
else
{
if (in[i] == '&' || in[i] == '|' || in[i] == ')') //低级运算,进栈
Oper.push(in[i--]);
else if (in[i] == '(')
{ //一个括号结束,弹出中间的所有运算符
while (Oper.top() != ')')
{
s.push(Oper.top());
Oper.pop();
}
Oper.pop(); i--;
}
}
}
while (!Oper.empty()) //栈中剩下的运算符
s.push(Oper.top()), Oper.pop();
while (!s.empty())
{ //计算前缀表达式
char ch = s.front(); s.pop();
if (isalpha(ch))
num.push(ch);
else Oper.push(ch);
if (!num.empty() && !Oper.empty() && Oper.top() == '!')
{ //单目运算符‘!’;
char x = num.top();
num.pop(); Oper.pop();
num.push(reverse(x));
}
else if (num.size() >= 2 && !Oper.empty())
{ //双目运算符
char oper = Oper.top(), x, y;
Oper.pop();
x = num.top(); num.pop();
y = num.top(); num.pop();
num.push(calculate(x, y, oper));
}
}
cout << num.top() << endl;
}
}
5 数列
时间限制:1000
内存限制:65536
用以下方式构造数列: 数列的第一个和第二个数都为1,接下来每个数都等于前面2个数之和。
给出一个正整数a,要求数列中第a个数对1000取模的结果是多少。
输入
第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a(1 <= a <= 1000000)。
输出
n行,每行输出对应一个输入。输出应是一个正整数,为数列中第a个数对1000取模得到的结果。
样例输入
4
5
2
19
1
样例输出
5
1
181
1
解析:按题目条件用for和数组解决,上code!
不准直接抄!!!
#include <iostream>
using namespace std;
int a[1000010]; //数组往大开
int main(){
a[1] = 1;
a[2] = 1;
for (int i = 3; i <= 1000000; i++)
a[i] = (a[i - 1] + a[i - 2]) % 1000;
int n, t;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> t;
cout << a[t] << endl;
}
return 0;
}
作者制作不易,喜欢就点个赞,关注一下吧!