补题:
L1-2 倒数第N个字符串 (15分)
https://pintia.cn/problem-sets/1302616295384633344/problems/1302616526494978049
这个题好像以前做过也没得全分
第一种做法:
从后往前数字符:
l=3时 zzz是n=1,所以n得先减1,然后再用z减,再入栈
比赛时没绕过那个n-1的弯,所以就得了3分。。。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int l,n,i,x,k=0;
char c[10];
scanf("%d%d",&l,&n);
n=n-1;
for(i=0;i<l;i++)
{
x=n%26;
c[k++]='z'-x;
n=n/26;
}
for(i=k-1;i>=0;i--)
printf("%c",c[i]);
return 0;
}
第二种做法:
从前往后数字符:
全部变成z的次数-倒着数的次数=正着数的次数(正着数从0开始)
就是a+正着数的次数
然后再入栈。。。
这是在scdn里边看到的思路
用到了pow函数(头文件是 math.h)
pow ( double x,double y) 就是x的y次方(不用pow也可以 ,跟循环乘一样)
注:sqrt函数(求根函数)(头文件是 math.h)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int l,n,i,k=0;
int s;
char c[10];
scanf("%d%d",&l,&n);
s=pow(26,l)-n;
for(i=0;i<l;i++)
{
int x=s%26;
c[k++]='a'+x;
s=s/26;
}
for(i=k-1;i>=0;i--)
{
printf("%c",c[i]);
}
return 0;
}
L1-8 猜数字 (20分)
https://pintia.cn/problem-sets/1302616295384633344/problems/1302616526494978055
这个题的话,主要就是怎么查找跟目标数字最接近的数
先是快排
然后二分查找。。。
在二分查找里,一开始的时候是找不到一样的数字就返回-1
关键就在找不到时,规定左边界>右边界 返回-1 而这里恰好就是 x[l] > key > x[r]
所以在这里只需要判断 x[l] 和 x[r] 谁更接近目标数字 key 就可以了
所以我当时为什么没有想到。。。。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int x[10005];
char c[10004][10];
void f1(int l,int r)
{
int i=l,j=r,key=x[i];
char keyc[10];
strcpy(keyc,c[i]);
if(i<j)
{
while(i<j)
{
while(i<j&&x[j]>=key) j--;
x[i]=x[j];
strcpy(c[i],c[j]);
while(i<j&&x[i]<=key) i++;
x[j]=x[i];
strcpy(c[j],c[i]);
}
x[i]=key;
strcpy(c[i],keyc);
f1(l,i-1);
f1(i+1,r);
}
}
int f2(int l,int r,int k)
{
int key=(l+r)/2;
if(l<=r)
{
if(x[key]==k) return key;
else if(x[key]>k) return f2(l,key-1,k);
else return f2(key+1,r,k);
}
else
{
if(x[l]-k>k-x[r])return r;
else return l;
}
}
int main()
{
int n,i,s=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s%d",c[i],&x[i]);
s=s+x[i];
}
f1(1,n);
s=s/n/2;
int m=f2(1,n,s);
printf("%d %s\n",s,c[m]);
return 0;
}
L2-3 名人堂与代金券 (25分)
https://pintia.cn/problem-sets/1302616295384633344/problems/1302616526494978058
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char nam[16];
int data;
} st[10005];
void f1(int l,int r)
{
int i=l,j=r;
struct node key=st[i];
if(i<j)
{
while(i<j)
{
while(i<j&&(st[j].data<key.data||(st[j].data==key.data&&strcmp(st[j].nam,key.nam)>=0))) j--;
st[i]=st[j];
while(i<j&&(st[i].data>key.data||(st[i].data==key.data&&strcmp(st[i].nam,key.nam)<=0))) i++;
st[j]=st[i];
}
st[i]=key;
f1(l,i-1);
f1(i+1,r);
}
}
int main()
{
int n,g,k,i,sum=0;
scanf("%d%d%d",&n,&g,&k);
for(i=1; i<=n; i++)
{
scanf("%s%d",st[i].nam,&st[i].data);
if(st[i].data>=g&&st[i].data<=100) sum=sum+50;
else
{
if(st[i].data<g&&st[i].data>=60) sum=sum+20;
}
}
f1(1,n);
printf("%d\n",sum);
printf("%d %s %d\n",1,st[1].nam,st[1].data);
int t=1,cnt=1,f=st[1].data;
i=2;
while(1)
{
if(f==st[i].data)
{
cnt++;
printf("%d %s %d\n",t,st[i].nam,st[i].data);
i++;
}
else
{
t=cnt+t;
cnt=1;
f=st[i].data;
if(t>k) return 0;
printf("%d %s %d\n",t,st[i].nam,st[i].data);
i++;
}
}
return 0;
}
L2-1 分而治之 (25分) //有空补
https://pintia.cn/problem-sets/1302616295384633344/problems/1302616526494978056
L1-1 天梯赛座位分配 (20分) //有空补
https://pintia.cn/problem-sets/1302616295384633344/problems/1302616526494978048