目录
A - 火柴棒等式
给你 n根火柴棍,你可以拼出多少个形如 "A+B=C" 的等式?等式中的 A、B、C 是用火柴棍拼出的整数(若该数非零,则最高位不能是 0)。
用火柴棍拼数字 0-9 的拼法如图所示
注意:
-
加号与等号各自需要两根火柴棍
-
如果 A 不等于B,则 A+B=C 与 B+A=C视为不同的等式(A、B、C≥0)
-
n根火柴棍必须全部用上
输入格式
输入一个整数 n(n≤24)。
输出格式
输出能拼成的不同等式的数目。
Sample Input 5
Sample Output 0
解题思路:
确定循环次数:因为A,B,C火柴数之和最多为20根,0+1111=1111,所以i,j最大为1111.
代码如下:
#include <bits/stdc++.h>
using namespace std;
int m(int n)
{
int a[10]={6,2,5,5,4,5,6,3,7,6};
if (n==0)//0应当单独列出来,否则会没有返回值
return 6;
int sum=0;
while (n)
{
sum+=a[n%10];
n=n/10;
}
return sum;
}
int main()
{
int n;
cin>>n;
n=n-4;//+和=会用掉4根火柴
int ans=0;
for (int i=0;i<=1111;i++)
{
for (int j=0;j<=1111;j++)
{
if (m(i)+m(j)+m(i+j)==n)
ans++;
}
}
printf ("%d",ans);
return 0;
}
B - 砝码称重
设有 1g,2g,3g,5g,10g,20g1g,2g,3g,5g,10g,20g 的砝码各若干枚(其总重小于等于 10001000),现在求问这些砝码能称出多少不同的重量。
输入格式
一行六个整数,分别代表 1g砝码的个数,分别代表 2g 砝码的个数,分别代表 3g 砝码的个数,分别代表 5g 砝码的个数,分别代表 10g砝码的个数,分别代表 20g砝码的个数。
输出格式
输出一行,格式为 Total=
n。
(n表示用这些砝码能称出的不同重量的个数,但不包括一个砝码也不用的情况)
Sample Input 1 1 0 0 0 0
Sample Output Total=3
解题思路:
暴力枚举
#include <bits/stdc++.h>
int flag[1001]={0};//用flag来标记
using namespace std;
int main()
{
int x1,x2,x3,x5,x10,x20;
cin>>x1>>x2>>x3>>x5>>x10>>x20;
for (int i1=0;i1<=x1;i1++)
for (int i2=0;i2<=x2;i2++)
for (int i3=0;i3<=x3;i3++)
for (int i5=0;i5<=x5;i5++)
for (int i10=0;i10<=x10;i10++)
for (int i20=0;i20<=x20;i20++)
{
int sum=i1+i2*2+i3*3+i5*5+i10*10+i20*20;
flag[sum]=1;//满足条件置为1
}
int ans=0;//ans代表数量
for (int i=1;i<=1000;i++)
{
if (flag[i]==1) ans++;
}
printf ("Total=%d",ans);
return 0;
}
C - 4 values whose sum is 0
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) belongs to A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2的28次方 ) that belong respectively to A, B, C and D .
Output
Output should be printed on a single line.
Example
Input: 6 -45 22 42 -16 -41 -27 56 30 -36 53 -37 77 -36 30 -75 -46 26 -38 -10 62 -32 -54 -6 45 Output: 5
解题思路:
因为n的范围是4000,n的4次方时间复杂度会超时,所以采用二分法,两两一组,最后找出满足a+b=-c-d的数。
利用stl中的函数
upper_bound(a,a+n,num):找出当前数组中第一个大于num的数,并返回下标地址。
lower_bound (a,a+n,num):找出当前数组中第一个大于等于num的数,并返回下标地址。
两者相减得到当前数组中等于num的个数。
#include <bits/stdc++.h>
using namespace std;
#define N 4500
int a[N],b[N],c[N],d[N];
int num1 [N*N],num2 [N*N];
int main()
{
int n;
scanf ("%d",&n);
int cnt=0;
for (int i=0;i<n;i++)
scanf ("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
num1[cnt]=a[i]+b[j];//注意cnt的值
num2[cnt++]=c[i]+d[j];//此时++
}
}
sort(num1,num1+cnt);//先排序
sort(num2,num2+cnt);
int ans=0;
for (int i=0;i<cnt;i++)
{
int tmp=num2[i];
ans+=upper_bound(num1,num1+cnt,-tmp)-lower_bound (num1,num1+cnt,-tmp);
}
printf ("%d",ans);
return 0;
}
E - Yet Another Card Deck
You have a card deck of nn cards, numbered from top to bottom, i. e. the top card has index 11 and bottom card — index nn. Each card has its color: the ii-th card has color a_iai.
You should process qq queries. The jj-th query is described by integer t_jtj. For each query you should:
- find the highest card in the deck with color t_jtj, i. e. the card with minimum index;
- print the position of the card you found;
- take the card and place it on top of the deck.
Input
The first line contains two integers nn and qq (2 \le n \le 3 \cdot 10^52≤n≤3⋅105; 1 \le q \le 3 \cdot 10^51≤q≤3⋅105) — the number of cards in the deck and the number of queries.
The second line contains nn integers a_1, a_2, \dots, a_na1,a2,…,an (1 \le a_i \le 501≤ai≤50) — the colors of cards.
The third line contains qq integers t_1, t_2, \dots, t_qt1,t2,…,tq (1 \le t_j \le 501≤tj≤50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
Input
7 5 2 1 1 4 3 3 1 3 2 1 1 4
Output
5 2 3 1 5
Note
Description of the sample:
- the deck is [2,1,1,4,3,3,1] and the first card with color t1 =3 has position 5;
- the deck is [3,2,1,1,4,3,1] and the first card with color t2 =2 has position 2;
- the deck is [2,3,1,1,4,3,1] and the first card with color t3 =1 has position 3;
- the deck is [1,2,3,1,4,3,1] and the first card with color t4 =1 has position 1;
- the deck is [1,2,3,1,4,3,1] and the first card with color t5 =4 has position 5.
解题思路:
枚举
题意理解:
第一行输入n(代表牌的个数)和q(代表q次查找),
第二行输入n个数,代表牌的颜色;
第三行输入q个数,t1, t2,.., tq (1< tq< 50)-查询颜色。它保证查询只询问在牌组中出现的颜色。
需要找到与查询颜色相同的卡片;打印你找到的卡片的位置;取卡片并把它放在牌组的顶部。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,q;
scanf ("%d%d",&n,&q);
int a[n+3];
int t[n+3];
for (int i=1;i<=n;i++) cin>>a[i];//输入n张牌的颜色
for (int i=1;i<=q;i++) cin>>t[i];//输入查找的牌的颜色
for (int i=1;i<=q;i++)
{
for (int j=1;j<=n;j++)
{
if (a[j]==t[i])//查找
{
cout<<j;//输出这个牌的位置
for (int m=j;m>=2;m--)//从第二张牌到第j张牌
{
a[m]=a[m-1];//依次后移
}
a[1]=t[i];//将查找到的这张牌放在顶部
break;
}
}
cout<<' ';
}
return 0;
}