目录
连接(ccf模拟赛)
题目描述
给出 n,m 问有多少有序数对 (a,b) 满足 1≤a≤n,1≤b≤m,a×b+a+b=conc(a,b) 其中 conc(a,b) 表示将 b 的十进制表示拼接在 a 的十进制表示之后所形成的数,a,b 不能包含前导零。
输入格式
第一行包含一个正整数 T,表示数据组数。
接下来 T 行,每行包含两个正整数 n,m 含义如上。
输出格式
共输出 T 行,每组数据输出一行一个正整数,表示答案。
样例输入
5
123 456
111 222
314 159
104 8576
233 3333
样例输出
246
222
628
312
699
数据范围
对前 40% 的数据,满足 1≤n,m≤1000
对所有数据,满足 1≤T≤10,1≤n,m≤10e9。
代码
需要代码解析,下方评论数字1
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll number(int n){
int ans=0;
while(n){
n/=10;
ans++;
}
return ans-1;
}
int main(){
ll T,n,m;
cin>>T;
while(T){
T--;
ll ans=0;
cin>>n>>m;
ans=number(m);
cout<<ans*n<<endl;
}
return 0;
}
装铅笔(ccf模拟赛)
题目描述
小 A 要为他的美术课准备许多盒彩色铅笔,可是他每天只能得到买一支彩铅的零花钱,所以要凑成一盒很不容易啊。彩铅有 n 种颜色(编号 1 到 n),一盒彩铅由每种颜色的彩铅各一支组成。一开始,小 A 没有任何彩铅,在接下来的 m 天中,小 A 会每天买一支某种颜色的彩铅。如果一天他发现目前他的零散的彩铅能凑成一盒了,那他会立即把它们装一盒。
小 A 希望你帮他计算这 m 天中,有哪些天他能装一盒彩铅。
输入格式
第一行两个整数 n,m 表示颜色数和天数。
第二行 m 个整数 a1,a2,⋯,am,表示每天小 A 买的彩铅颜色。
输出格式
一行 m 个数字 0 或 1,无需空格分隔。第 i 个数为 1 当且仅当小 A 在第 i 天成功地装了一盒彩铅。
样例输入
3 9
2 2 1 1 3 1 3 3 2
样例输出
000010101
数据范围
对于前 20% 的数据,n≤2。
对于前 50% 的数据,n≤100。
对于 100% 的数据,1≤n,m≤10e5,1≤ai≤n。
时间限制:1s
,空间限制:256MB
。
代码
需要代码解析,下方评论数字2
#include<iostream>
#include<set>
using namespace std;
const int N=1e5+5;
int a[N];
set<int> b;
int main(){
int m,n,ans;
cin>>m>>n;
for(int i=0;i<n;i++){
cin>>ans;
a[ans]++;
if(b.find(ans)==b.end()){
b.insert(ans);
a[ans]--;
if(b.size()==m){
cout<<1;
b.clear();
for(int i=1;i<=m;i++){
if(a[i]){
a[i]--;
b.insert(i);
}
}
}
else cout<<0;
}else{
cout<<0;
}
}
return 0;
}
链表合并(pat甲级真题)
给定两个单链表 L1=a1→a2→…→an−1→an 和 L2=b1→b2→…→bm−1→bm。
如果 n≥2m,你的任务是将较短的那个链表逆序,然后将之并入较长的链表,得到形如 a1→a2→bm→a3→a4→bm−1…的结果。
例如给定两个链表分别为 6→7 和 1→2→3→4→5,你应该输出 1→2→7→3→4→6→5。
补充
本题中可能包含不在两个单链表中的节点,这些节点无需考虑。
输入格式
输入首先在第一行中给出两个链表 L1 和 L2 的头结点的地址,以及正整数 N,即给定的结点总数。
一个结点的地址是一个 5 位数的非负整数(可能包含前导 0),空地址 NULL
用 −1 表示。
随后 N 行,每行按以下格式给出一个结点的信息:
Address Data Next
其中 Address
是结点的地址,Data
是不超过 10e5 的正整数,Next
是下一个结点的地址。
题目保证没有空链表,并且较长的链表至少是较短链表的两倍长。
输出格式
按顺序输出结果链表,每个结点占一行,格式与输入相同。
数据范围
1≤N≤10e5
时间限制
0.4s
输入样例:
00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1
输出样例:
01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1
代码
需要代码解析,下方评论数字3
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int address;
int data;
int next;
};
int cmp(node i,node j){
return i.address<j.address;
}
const int N=1e5+5;
node a[N],b[N],c[N];
int main(){
int p,q,n,P=0,Q=0;
cin>>p>>q>>n;
for(int i=0;i<n;i++){
scanf("%d %d %d",&a[i].address,&a[i].data,&a[i].next);
}
sort(a,a+n,cmp);
while(p!=-1){
int l=0,r=n-1;
int mid=l+r>>1;
while(l<=r){
if(a[mid].address<p) l=mid+1;
else if(a[mid].address>p) r=mid-1;
else{
p=a[mid].next;
b[P].address=a[mid].address;
b[P].data=a[mid].data;
b[P].next=a[mid].next;
P++;
break;
}
mid=l+r>>1;
}
}
while(q!=-1){
int l=0,r=n-1;
int mid=l+r>>1;
while(l<=r){
if(a[mid].address<q) l=mid+1;
else if(a[mid].address>q) r=mid-1;
else{
q=a[mid].next;
c[Q].address=a[mid].address;
c[Q].data=a[mid].data;
c[Q].next=a[mid].next;
Q++;
break;
}
mid=l+r>>1;
}
}
if(Q>P){
for(int i=0,j=P-1;i<Q;i++){
if(i%2==1&&j>=0){
if(b[j].address!=-1)printf("%0.5d %d %0.5d\n",c[i].address,c[i].data,b[j].address);
else printf("%0.5d %d %d\n",c[i].address,c[i].data,b[j].address);
if(c[i].next!=-1) printf("%0.5d %d %0.5d\n",b[j].address,b[j].data,c[i].next);
else printf("%0.5d %d %d\n",b[j].address,b[j].data,c[i].next);
j--;
}else{
if(c[i].next!=-1) printf("%0.5d %d %0.5d\n",c[i].address,c[i].data,c[i].next);
else printf("%0.5d %d %d\n",c[i].address,c[i].data,c[i].next);
}
}
}else{
for(int i=0,j=Q-1;i<P;i++){
if(i%2==1&&j>=0){
if(c[j].address!=-1)printf("%0.5d %d %0.5d\n",b[i].address,b[i].data,c[j].address);
else printf("%0.5d %d %d\n",b[i].address,b[i].data,c[j].address);
if(b[i].next!=-1)printf("%0.5d %d %0.5d\n",c[j].address,c[j].data,b[i].next);
else printf("%0.5d %d %d\n",c[j].address,c[j].data,b[i].next);
j--;
}else{
if(b[i].next!=-1) printf("%0.5d %d %0.5d\n",b[i].address,b[i].data,b[i].next);
else printf("%0.5d %d %d\n",b[i].address,b[i].data,b[i].next);
}
}
}
return 0;
}