机试刷题
机试刷题记录
一、刷题
L1-007 念数字
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805136889593856?type=7&page=0
错误答案:
错误原因:格式不会
题解:
怎么按照正确格式输出?
我们可以这样,如果遇到负数,先输出负号+一个空格,后面的n个数,除了第一个只输出数字本身,后面的都先输出一个空格再输出数字。这样我们就符合格式了。
现在以-613为例,我们在main函数里先输出负号后,把-613变成613,然后进入dayin函数,显然,6,1,3这三个数的输出顺序为6、1、3。那我们就只要判断哪个数字是第一个就行了,由于在递归中,这个6就是递归的最后一层,而最后一层的输入参数肯定是1-10,所以我们只要判断输入参数大小,就可以知道哪个是最后一层了。
#include<stdio.h>
#include<math.h>
int x,y;
char str[10][5]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
void dayin(int num)
{
x=num/10;
y=num%10;
dayin(x);
printf("%s ",str[y]);
}
int main()
{
int num;
scanf("%d",&num);
if(num<0) printf("fu ");
dayin(num);
printf("%s ",str[y]);
return 0;
}
正确答案
#include<stdio.h>
#include<math.h>
char nums[10][5] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
void dayin(int num)
{
int s_num = num%10;
num = num/10;
if(num!=0)
{
dayin(num);
printf(" %s",nums[s_num]);
}
else{
printf("%s",nums[s_num]);
}
}
int main()
{
int num;
scanf("%d",&num);
if(num < 0) {
num = -num;
printf("fu ");
}
dayin(num);
return 0;
}
L1-017 到底有多二
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805121500692480?type=7&page=0
我们要读入一个·50位的数,所以我们要用字符串来存这个数
错误原因:用long int 来读取50位数字
#include<stdio.h>
#include<math.h>
#include<string.h>
char num[55];
int main()
{
scanf("%s",num);
int len = strlen(num);
int is_even = 0, is_negtive = 0, digit_count = len, two_count = 0;
if(num[0]=='-') is_negtive = 1, digit_count -= 1;
if((num[len-1]-'0')%2==0) is_even = 1;
int i;
for(i=0;i<len;++i)
{
if(num[i]=='2') two_count += 1;
}
double ans = (double)two_count/(double)digit_count;
if(is_negtive) ans *= 1.5;
if(is_even) ans *= 2;
ans *= 100;
printf("%.2f%%",ans);
return 0;
}
L1-015 跟奥巴马一起画方块
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805124398956544?type=7&page=0
错误原因:没有向上取整
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int i,j,x,y;
scanf("%d %c",&x,&y);
for(j=0;j<((x+1)/2);j++)
{
for(i=0;i<x;i++)
{
printf("%c",y);
}
printf("\n");
}
return 0;
}
L1-016 查验身份证(难度接近去年机试最后一题难度,考验代码能力)
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805122985476096?type=7&page=0
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
char ID[110][20];
char test[11]={'1','0','X','9','8','7','6','5','4','3','2'};
int weight[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int i,j,N=0,count=0;
scanf("%d",&N);
for(i=0;i<N;i++)
{
int data=0;
scanf("%s",ID[i]);
for(j=0;j<17;j++)
{
if((ID[i][j]<'0')||(ID[i][j]>'9'))
{
printf("%s\n",ID[i]);
break;
}
data=data+(ID[i][j]-'0')*weight[j];
}
if(j==17)
{
if(ID[i][j]==test[data%11])
{
count++;
}
else printf("%s\n",ID[i]);
}
}
if(count==N) printf("All passed");
return 0;
}
L1-018 大笨钟(难点都在读题)
题目链接https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805119944605696?type=7&page=0
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int i,j,hour=0,minute=0;
scanf("%d:%d",&hour,&minute);
if(hour<0||hour>24) printf("Error");
if(minute<0||minute>60) printf("Error");
if(hour>=0&&hour<12||(hour==12&&minute==0))
{
if(hour<10&&minute>10)
printf("Only 0%d:%d. Too early to Dang.",hour,minute);
else if(hour>10&&minute<10)
printf("Only %d:0%d. Too early to Dang.",hour,minute);
else if(hour<10&&minute<10)
printf("Only 0%d:0%d. Too early to Dang.",hour,minute);
else printf("Only %d:%d. Too early to Dang.",hour,minute);
}
for(i=0;i<12;i++)
{
if((hour-12)==i&&minute==0)
{
for(j=0;j<(hour-12);j++)
printf("Dang");
}
if((hour-12)==i&&minute!=0)
{
for(j=0;j<(hour-12+1);j++)
printf("Dang");
}
}
return 0;
}
L1-008 求整数段和
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805135224455168?type=7&page=0
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
int main()
{
int i,x,y,sum=0,count=0;
scanf("%d %d",&x,&y);
for(;x<=y;x++)
{
printf("%5d",x);
sum=sum+x;
count++;
if(count==5&&x!=y)
{
printf("\n");
count=0;
}
}
printf("\n");
printf("Sum = %d",sum);
return 0;
}
L1-056 猜数字
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805074646122496?type=7&page=0
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
int main()
{
int i,N,min=0,count=0,halfaverage=0,result=0;
char name[10005][10];
int num[10005];
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%s %d",name[i],&num[i]);
count=count+num[i];
}
halfaverage=(count/N)/2;
if(halfaverage>num[0])
{
min=halfaverage-num[0];
}
min=num[0]-halfaverage;
for(i=0;i<N;i++)
{
if(halfaverage>num[i])
{
if(min>(halfaverage-num[i]))
{
min=halfaverage-num[i];
result=i;
}
}
else
{
if(min>(num[i]-halfaverage))
{
min=num[i]-halfaverage;
result=i;
}
}
}
printf("%d %s",halfaverage,name[result]);
return 0;
}
L1-011 A-B(知识点很多,推荐做一下)
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805130426171392?type=7&page=0
解法1:(复杂度为O(n^2))
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
int main()
{
char x,A[10005],B[10005];
int i=0,j=0,count,lengthA,lengthB;
while((x=getchar())!='\n')
{
A[i]=x;
i++;
}
while((x=getchar())!='\n')
{
B[j]=x;
j++;
}
lengthA=strlen(A);
lengthB=strlen(B);
for(i=0;i<lengthA;i++)
{
count=0;
for(j=0;j<lengthB;j++)
{
if((A[i]==B[j]))
{
break;
}
count++;
}
if(count==lengthB)
{
printf("%c",A[i]);
}
}
return 0;
}
解法2:复杂度为O(256n)
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
int main()
{
char A[10005] = {0};
int is_delete[10005] = {0};
int i,lenA = 0,use[260]={0};
char ch;
while((ch = getchar())!='\n')
{
A[lenA] = ch;
lenA ++;
}
//B字符串
while((ch = getchar())!='\n')
{
if(use[ch]) continue;
for(i=0;i<lenA;++i)
{
if(A[i]==ch) is_delete[i] = 1;
}
}
for(i=0;i<lenA;++i)
if(!is_delete[i]) printf("%c",A[i]);
return 0;
}
L1-039 古风排版
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805091888906240?type=7&page=0
#include<stdio.h>
char output[100][1000];
char str[1005]={0};
int main()
{
int n,i,j;
scanf("%d",&n);
char ch;
int len= 0;
getchar(); //读换行符
while((ch=getchar())!='\n')
{
str[len] = ch;
len ++;
}
for(i=0;i<n;++i)
for(j=0;j<(len/n+1);++j)
output[i][j] = ' ';
int l=0;
for(i=0;i<len;++i)
{
output[i%n][i/n] = str[i];
if(i/n>l) l = i/n;
}
for(i=0;i<n;++i)
{
for(j=l;j>=0;--j)
{
printf("%c",output[i][j]);
}
printf("\n");
}
return 0;
}
L1-071 前世档案
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/1336215880692482054?type=7&page=0
#include<stdio.h>
int main()
{
int n,m,i,j;
char str[30];
scanf("%d %d",&n,&m);
for(i=0;i<m;++i)
{
scanf("%s",str);
int ans = 0,base = 1;
for(j=n-1;j>=0;j--)
{
int x;
if(str[j]=='y') x = 0;
else x = 1;
ans += x*base;
base *= 2;
}
printf("%d\n",ans+1);
}
return 0;
}
L1-043 阅览室(模拟好题)
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805087447138304?type=7&page=0
#include<stdio.h>
int main()
{
int i,N,number,x;
int num[1005]={0},hour[1005]={0},h=0,minute[1005]={0},m=0,count[1005]={0},sum,average;
char y,SE[1005];
scanf("%d",&N);
for(i=0;i<N;i++)
{
int j;
for(j=0;j<1000;++j) num[j] = 0;
x=1;
number=0;
average=0;
sum=0;
while(x!=0)
{
scanf("%d %c %d:%d",&x,&y,&h,&m);
SE[x]=y;
if((x!=0)&&(SE[x]=='S'))
{
num[x]=1;
hour[x]=h;
minute[x]=m;
}
if((x!=0)&&(SE[x]=='E'))
{
if(num[x]==1)
{
num[x]=0;
count[x]=(h-hour[x])*60+(m-minute[x]);
number++;
sum=sum+count[x];
}
}
}
if((number!=0)&&(sum%number!=0))
average=(int)(1.0*sum/number + 0.5);
else if((number!=0)&&(sum%number==0))
average=sum/number;
else average=0;
printf("%d %d\n",number,average);
}
return 0;
}
L1-054 福到了
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805076512587776?type=7&page=0
#include<stdio.h>
int main()
{
int i,j,N,count=0;
char x[105],y,z;
char zifu[105][105]={0};
scanf("%c %d",&y,&N);
getchar();
for(i=0;i<N;i++)
{
j=0;
while((z=getchar())!='\n')
{
if(z=='@') zifu[i][j]=1;
j++;
}
}
for(i=N-1;i>=0;i--)
for(j=0;j<N;j++)
if(zifu[i][j]==zifu[N-i-1][j]) count++;
if(count==N*N) printf("bu yong dao le\n");
for(i=N-1;i>=0;i--)
{
for(j=N-1;j>=0;j--)
{
if(zifu[i][j]==1) printf("%c",y);
else printf(" ");
}
printf("\n");
}
return 0;
}
L1-048 矩阵A乘以B
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805082313310208?type=7&page=0
#include<stdio.h>
int main()
{
int i,j,k,p,q,num,R,C,AR,AC,BR,BC;
int A[105][105],B[105][105],result[105][105],number[3];
char x,y[105];
for(i=0;i<2;i++)
{
scanf("%d %d",&R,&C);
if(i==0)
{
AR=R;AC=C;
}
if(i==1)
{
BR=R;BC=C;
}
for(j=0;j<R;j++)
{
for(k=0;k<C;k++)
{
if(i==0) scanf("%d",&A[j][k]);
if(i==1) scanf("%d",&B[j][k]);
}
}
}
if(AC==BR)
{
printf("%d %d\n",AR,BC);
for(j=0;j<AR;j++)
{
for(i=0;i<BC;i++)
{
num=0;
for(k=0;k<AC;k++)
{
num=num+A[j][k]*B[k][i];
}
if(i!=BC-1) printf("%d ",num);
else printf("%d",num);
}
printf("\n");
}
}
else printf("Error: %d != %d",AC,BR);
return 0;
}
L1-050 倒数第N个字符串(思维题)
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805080346181632?type=7&page=0
L1-019 谁先倒(考验代码量)
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805118568873984?type=7&page=0
L1-006 连续因子
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805138600869888?type=7&page=0
#include<stdio.h>
#include<math.h>
int main()
{
int i,j,x,N,length,from,to,L=0,F=0,T=0;
scanf("%d",&N);
for(i=2;i<(sqrt(N)+1);i++)
{
length=0;
from=0;
to=0;
x=N;
for(j=i;j<N;j++)
{
if((x%j)==0)
{
length++;
if(length==1)
{
from=j;
}
x=x/j;
}
else
{
to=j-1;
break;
}
}
if(length>L)
{
L=length;
F=from;
T=to;
}
}
if(L!=0)
{
printf("%d\n",L);
for(i=F;i<T;i++)
{
printf("%d*",i);
}
printf("%d",i);
}
else
{
printf("%d\n",L+1);
printf("%d",N);
}
return 0;
}
L1-009 N个数求和
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805133597065216?type=7&page=0
#include<stdio.h>
#include<math.h>
int MAXMIN(int m,int n)
{
int x;
if(m>=n)
{
while(n!=0)
{
x=m%n;
m=n;
n=x;
}
return m;
}
else MAXMIN(n,m);
}
int main()
{
int i,N,x,y,fenzi,fenmu,max,min;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d/%d",&x,&y);
if(i==0)
{
fenzi=x;
fenmu=y;
}
else
{
max=MAXMIN(y,fenmu);
min=y*fenmu/max;
fenzi=x*(min/y)+fenzi*(min/fenmu);
fenmu=min;
max=MAXMIN(abs(fenzi),fenmu);
if(max!=1)
{
fenzi=fenzi/max;
fenmu=fenmu/max;
}
}
while((max=MAXMIN(abs(fenzi),fenmu))!=1)
{
fenzi=fenzi/max;
fenmu=fenmu/max;
}
}
if((fenzi/fenmu)!=0)
{
if((fenzi%fenmu)!=0)
{
printf("%d ",fenzi/fenmu);
printf("%d/%d",fenzi-fenmu*(fenzi/fenmu),fenmu);
}
else printf("%d",fenzi/fenmu);
}
else
{
if((fenzi%fenmu)!=0)
printf("%d/%d",fenzi,fenmu);
else printf("0");
}
return 0;
}
L1-087 机工士姆斯塔迪奥
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/1518581903422062592?type=7&page=0
#include<stdio.h>
int main()
{
int i,j,N,M,Q,hang,lie,count_hang=0,count_lie=0,Ti,Ci,count=0;
int H[100005]={0},L[100005]={0};
scanf("%d %d %d",&N,&M,&Q);
for(i=0;i<Q;i++)
{
scanf("%d %d",&Ti,&Ci);
if((Ti==0)&&(H[Ci-1]==0))
{
count_hang++;
H[Ci-1]=1;
}
else if((Ti==1)&&(L[Ci-1]==0))
{
count_lie++;
L[Ci-1]=1;
}
}
count=M*N-count_hang*M-count_lie*N+count_hang*count_lie;
printf("%d",count);
return 0;
}
GPLT天梯赛:https://ac.nowcoder.com/acm/contest/30532
洛谷普及组:https://www.luogu.com.cn/training/478078#problems
B3927 [GESP202312 四级] 小杨的字典
题目链接:https://www.luogu.com.cn/problem/B3927
代码1:
#include<stdio.h>
#include<string.h>
//A,B对应的词
char A[105][15]={0},B[105][15]={0};
//要被翻译的句子
char str[1000];
int translate(int i,int n)
{
//先找一下这个单词最后面一个字母的位置
int last_letter = i;
while(str[last_letter + 1]>='a' && str[last_letter + 1]<='z') last_letter ++;
//然后查找匹配的单词并进行翻译
int j;
for(j=0;j<n;++j)
{
//长度不等直接跳过
if((last_letter-i+1) != strlen(A[j])) continue;
int k,flag=1;
for(k=i;k<=last_letter;++k)
{
if(str[k]!=A[j][k-i]) flag=0;
}
//flag = 1代表匹配成功 ,我们进行翻译
if(flag){
//输出对应的B单词,然后该函数返回
printf("%s",B[j]);
return last_letter;
}
}
//如果没有找到对应的单词直接输出UNK
printf("UNK");
return last_letter;
}
int main()
{
int n,i,j;
//输入
scanf("%d",&n);
for(i=0;i<n;++i)
{
scanf("%s",A[i]);
scanf("%s",B[i]);
}
scanf("%s",str);
int len = strlen(str);
//对要翻译的句子进行遍历
for(i=0;i<len;++i)
{
//如果是字符,则直接输出即可
if(str[i]<'a' || str[i]>'z') printf("%c",str[i]);
//如果是字母,则说明是单词,我们直接用一个函数来处理
else{
//这个函数会将从i开始的单词转化并翻译,翻译完之后返回这个单词最后一个字母的位置
i = translate(i,n);
}
}
return 0;
}
错误代码:
错误点1:输入的方式有误,我们输入的是不含空格的单词或者句子,没必要用getchar(),用scanf就行。洛谷用getchar()容易出现错误,这个地方也是你出错的点之一。
错误点2:如下图所示,初始化字符串是用0而不是'0',0在字符串里代表空字符,如果用'0'初始化的后果就是A[0] = '0'而剩下的都是空字符
错误点3:如下图所示,用这种方式更新i值会导致大循环结尾i又+1,错过了一个字符
所以应该像这样添加一个i--。
错误点4:word数组用完不初始化
#include<stdio.h>
int main()
{
int N=0,i,j,k,p,num=0,count=0,length=0;
char x,A[105][15]={'0'},B[105][15]={'0'};
char sen[1005]={'0'},word[1005]={'0'};
scanf("%d",&N);
getchar();
for(i=0;i<N;i++)
{
num=0;
j=0;
while((x=getchar())!='\n')
{
//num为0时,输入A
if((x!=' ')&&(num==0))
{
A[i][j]=x;
j++;
}
//为空格时
else if((x==' ')&&(num==0))
{
num++;
j=0;
}
//当num=1时,输入B
else
{
B[i][j]=x;
j++;
}
}
}
scanf("%s",sen);
length=strlen(sen);
for(i=0;i<length;i++)
{
if(sen[i]<'a'||sen[i]>'z')
{
printf("%c",sen[i]);
}
else
{
count=0;
j=0;
while(sen[i]>='a'&&sen[i]<='z'&&i<length)
{
word[j]=sen[i];
i++;
j++;
}
for(k=0;k<N;k++)
{
for(p=0;p<=j;p++)
{
if(word[p]!=A[k][p]) break;
}
if(p==j&&A[k][p+1]=='0')
{
printf("%s",B[k]);
count++;
}
}
if(count==0) printf("UNK");
}
}
return 0;
}
更正后:
#include<stdio.h>
#include<string.h>
int main()
{
int N=0,i,j,k,p,num=0,count=0,length=0;
char x,A[105][15]={0},B[105][15]={0};
char sen[1005]={0};
scanf("%d",&N);
for(i=0;i<N;++i)
{
scanf("%s",A[i]);
scanf("%s",B[i]);
}
scanf("%s",sen);
length=strlen(sen);
for(i=0;i<length;i++)
{
if(sen[i]<'a'||sen[i]>'z')
{
printf("%c",sen[i]);
}
else
{
char word[15] = {0};
count=0;
j=0;
while(sen[i]>='a'&&sen[i]<='z'&&i<length)
{
word[j]=sen[i];
i++;
j++;
}
i--;
for(k=0;k<N;k++)
{
for(p=0;p<=j;p++)
{
if(word[p]!=A[k][p]) break;
}
if(p==j+1&&A[k][p]==0)
{
printf("%s",B[k]);
count++;
}
}
if(count==0) printf("UNK");
}
}
return 0;
}
B3939 [GESP样题 四级] 绝对素数
题目链接:https://www.luogu.com.cn/problem/B3939
#include<stdio.h>
#include<math.h>
int Judge(int num)
{
int i,count=0;
for(i=2;i<num;i++)
{
if((num%i)==0)
{
count=1;
return 0;
}
}
return 1;
}
int main()
{
int i=0,A=0,B=0,x=0,y=0;
scanf("%d %d",&A,&B);
for(i=A;i<=B;i++)
{
x=Judge(i);
y=Judge(i/10+(i%10)*10);
if(x==1&&y==1) printf("%d\n",i);
}
return 0;
}
B3940 [GESP样题 四级] 填幻方
题目链接:https://www.luogu.com.cn/problem/B3940
#include<stdio.h>
int main()
{
int i=0,j=1,N=0,hang=0,lie=0,hang_old=0,lie_old=0,False=0;
int HF[25][25]={0};
scanf("%d",&N);
for(i=1;i<=(N*N);i++)
{
False=0;
if(i==1)
{
hang=0;
lie=N/2;
HF[hang][lie]=i;
}
if((i!=1)&&(hang-1)<0)
{
hang_old=hang;
hang=N-1;
if((lie+1)==N)
{
lie_old=lie;
lie=0;
}
else
{
lie_old=lie;
lie++;
}
if(HF[hang][lie]==0)
{
HF[hang][lie]=i;
}
else
{
hang=hang_old;
lie=lie_old;
False=1;
}
}
else if((i!=1)&&(hang-1)>=0)
{
hang_old=hang;
hang=hang-1;
if((lie+1)==N)
{
lie_old=lie;
lie=0;
}
else
{
lie_old=lie;
lie++;
}
if(HF[hang][lie]==0)
{
HF[hang][lie]=i;
}
else
{
hang=hang_old;
lie=lie_old;
False=1;
}
}
if(False==1)
{
hang_old=hang;
if((hang+1)==N)
{
hang=0;
}
else
{
hang=hang+1;
}
HF[hang][lie]=i;
}
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(j!=N-1) printf("%d ",HF[i][j]);
else printf("%d\n",HF[i][j]);
}
}
return 0;
}
B3850 [GESP202306 四级] 幸运数
题目链接:https://www.luogu.com.cn/problem/B3850
#include<stdio.h>
int Judge(long long int num)
{
int x=1,y=0,i=1;//x记录每位上的数,y记录转换后的每位数的和
int a=0,b=0;
while(num!=0)//num等于0说明,x已经取到最高位
{
x=num%10;
num=num/10;
if(i%2==0)//偶数位,直接加入总和
{
y=y+x;
i++;
}
else//奇数位,判断与7相乘后是否大于9,若大于9则将相乘后的各位相加(必定是个两位数),再循环判断是否大于9.....,之后加入总和
{
x=x*7;
while(x>9)
{
//x=x/10+x%10;//不确定是否不能这样相加
a=x%10;
b=x/10;
x=a+b;
}
y=y+x;
i++;
}
}
if(y%8==0) return 1;
else return 0;
}
int main()
{
int i=0,N=0,result=0;
long long int num=0;
scanf("%d",&N);
for(;i<N;i++)
{
scanf("%d",&num);
result=Judge(num);
if(result==1) printf("T\n");
else printf("F\n");
}
return 0;
}
B3870 [GESP202309 四级] 变长编码
题目链接:https://www.luogu.com.cn/problem/B3870
#include<stdio.h>
int tran(long long int num,int * num_bits)
{
int len=0;
while(num)
{
num_bits[len] = num%2;
num/=2;
len ++;
}
return len;
}
char int_to_char(int x)
{
if(x>9) return 'A'-10+x;
return '0'+x;
}
int main()
{
long long int num;
int num_bits[100] = {0};
scanf("%lld",&num);
if(num==0) printf("00");
int len = tran(num,num_bits),i,j;
while(len%7!=0) len++;
// printf("len = %d\n",len);
for(i=0;i<len;i+=7)
{
int sum = 0;
for(j=i+6;j>=i;--j)
{
sum*=2;
sum+=num_bits[j];
}
if(i+7<len) sum += 128;
printf("%c%c ",int_to_char(sum/16),int_to_char(sum%16));
}
return 0;
}
B3851 [GESP202306 四级] 图像压缩
题目链接:https://www.luogu.com.cn/problem/B3851
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
typedef struct img_grid{
int color;//代表颜色值
int num;//代表这个灰阶出现的数量
}grid;
//把十六进制字符转化成对应的数字
int tran_chars_to_int(char ch)
{
if(ch>='0' && ch <='9') return ch-'0';
else return ch-'A' + 10;
}
//把数字转化成对应的十六进制
char tran_int_to_char(int x)
{
if(x<10) return '0' + x;
else return 'A' + x -10;
}
//qsort的比较函数
int cmp(const void *a,const void *b)
{
//如果num相等,则按照从小到大对color进行排序
if((*(grid*)b).num == (*(grid*)a).num)
{
return (*(grid*)a).color - (*(grid*)b).color;
}
//num不相等,按照num从大到小进行排序
else{
return (*(grid*)b).num - (*(grid*)a).num;
}
}
//自己写了个绝对值函数,自己写的用着放心
int Abs(int x){
if(x<0) return -x;
return x;
}
int main()
{
grid grids[260];
int n,i,len,k,j;
char str[50][50]={0};
//先进行初始化
for(i=0;i<256;++i) grids[i].num = 0;
scanf("%d",&n);
for(i=0;i<n;++i)
{
scanf("%s",str[i]);
len = strlen(str[i]);
for(j=0;j<len;j+=2)
{
int x = tran_chars_to_int(str[i][j])*16 + tran_chars_to_int(str[i][j+1]);
grids[x].color = x;
grids[x].num += 1;
}
}
//以出现次数为第一优先级,灰阶大小为第二优先级排个序
qsort(grids,256,sizeof(grid),cmp);
for(i=0;i<16;++i)
{
//把前十六个对应的灰阶转化成十六进制形式输出
printf("%c%c",tran_int_to_char(grids[i].color/16),tran_int_to_char(grids[i].color%16));
}
printf("\n");
for(i=0;i<n;++i)
{
for(j=0;j<len;j+=2)
{
int x = tran_chars_to_int(str[i][j])*16 + tran_chars_to_int(str[i][j+1]);
int small_idx = 0; //代表最接近这个颜色的下标
//看一下 十六个灰阶和谁更近
for(k=0;k<16;++k)
{
if(Abs(x-grids[k].color) < Abs(x-grids[small_idx].color))
{
small_idx = k;
}
}
printf("%c",tran_int_to_char(small_idx));
}
printf("\n");
}
return 0;
}
B3869 [GESP202309 四级] 进制转换
题目链接:https://www.luogu.com.cn/problem/B3869
#include<stdio.h>
#include<string.h>
long long int translate(char x,int a,int b)
{
int i=0;
long long int num,di=1;
if(x>='0'&&x<='9')
{
num=x-'0';
}
else num=x-'A'+10;
for(;i<a;i++)
{
di=di*b;
}
return num*di;
}
int main()
{
int N=0,K=0,length=0;
long long int count=0;
int i=0,j=0;
char K_num[15];
scanf("%d",&N);
for(;i<N;i++)
{
for(;j<15;j++) K_num[j]=0;
count=0ll;
scanf("%d %s",&K,K_num);
length=strlen(K_num);
for(j=length-1;j>=0;j--)
{
count=count+translate(K_num[j],length-j-1,K);
}
printf("%lld\n",count);
}
return 0;
}
B3928 [GESP202312 四级] 田忌赛马
题目链接:https://www.luogu.com.cn/problem/B3928
#include<stdio.h>
#include<stdlib.h>
int My[50005] = {0}, Your[50005] = {0};
int cmp(const void *a,const void *b){
return (*(int*)a - *(int*)b);
}
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;++i) scanf("%d",&My[i]);
for(i=0;i<n;++i) scanf("%d",&Your[i]);
qsort(My,n,sizeof(int),cmp); //我们的马都按照速度排序一下
qsort(Your,n,sizeof(int),cmp); //对面的马也都按照速度排序一下
j = n-1;
int Win = 0;
for(i=n-1;i>=0;--i) //然后我们从我们这边最快的马开始,让它们依次挑战对面能赢得最快的马
{
while(My[i] < Your[j] && j>=0) j--;
if(j>=0) Win ++,j--;
}
printf("%d",Win);
return 0;
}
下面回归到基础题部分
L1-095 分寝室(限时20分钟)
https://pintia.cn/problem-sets/994805046380707840/exam/problems/1649748772841508870?type=7&page=0
#include<stdio.h>
#include<math.h>
int abs(int x)
{
if(x<0) x=-x;
return x;
}
int main()
{
int i=0;
int n,n0,n1;
int length,x=0,y=0;
scanf("%d %d %d",&n0,&n1,&n);
length=1000000;//赋给一个最大的差值
for(i=1;i<n;i++)
{
if(n0%i==0&&n0/i!=1&&n1%(n-i)==0&&n1/(n-i)!=1)//将所有在最大数n之内的n0的因数都找出来,且保证不存在单间宿舍
{
if(length>abs(n0/i-n1/(n-i)))
{
length=abs(n0/i-n1/(n-i));
x=i;//女生宿舍个数
y=n-i;//男生宿舍个数
}
}
}
if(length!=1000000) printf("%d %d",x,y);//如果差值已改变则说明有方案
else printf("No Solution");//如果差值未改变则说明没有方案
return 0;
}
L1-082 种钻石(限时20分钟)
https://pintia.cn/problem-sets/994805046380707840/exam/problems/1518581097939525632?type=7&page=0
#include<stdio.h>
int main()
{
int N,v;
scanf("%d %d",&N,&v);
printf("%d",N/v);
return 0;
}
L1-093 猜帽子游戏
https://pintia.cn/problem-sets/994805046380707840/exam/problems/1649748772841508868?type=7&page=0
#include<stdio.h>
int main()
{
int i=0,j=0;
int N,K;
int count0=0,count_yes=0;
int hat[105]={0};
int answer[105]={0};
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&hat[i]);
}
scanf("%d",&K);
for(i=0;i<K;i++)
{
for(j=0;j<N;j++)
{
scanf("%d",&answer[j]);
}
count0=0,count_yes=0;
for(j=0;j<N;j++)
{
if(answer[j]==0) count0++;
else if(answer[j]==hat[j]) count_yes++;
else
{
printf("Ai Ya\n");
count_yes=0;
break;
}
}
if(count0==N) printf("Ai Ya\n");
else if(count_yes>=1) printf("Da Jiang!!!\n");
}
return 0;
}
L1-094 剪切粘贴
https://pintia.cn/problem-sets/994805046380707840/exam/problems/1649748772841508869?type=7&page=0
#include<stdio.h>
#include<string.h>
//对str的l到r的字符做剪切操作并放到cut里
void cut_str(int l,int r,char* str, char* cut)
{
int len = strlen(str),i,j;
for(i=l-1;i<=r-1;++i)
{
cut[i-l+1] = str[i];
}
for(i=l-1;i<len;++i)
{
str[i] = str[i+(r-l+1)];
}
}
//找到能匹配front+end的位置
int find(char * str, char * front, char* end)
{
int len = strlen(str), len_front = strlen(front), len_end = strlen(end),i,j;
for(i=len_front-1;i<len;++i)
{
int flag = 1;
for(j=0;j<len_front;++j) if(front[j] != str[i-len_front + j + 1]) flag = 0;
for(j=0;j<len_end;++j) if(end[j] != str[i+1+j]) flag = 0;
if(flag) return i+1;
}
return len;
}
//将cut的内容粘贴到str的x位置
void paste(int x,char * str,char* cut)
{
int len = strlen(str),len_cut = strlen(cut),i,j;
for(i=len+len_cut-1;i>=x+len_cut;i--) str[i] = str[i-len_cut];
for(i=x;i<x+len_cut;++i) str[i] = cut[i-x];
}
int main()
{
char str[215] = {0};
int n,i,j;
scanf("%s",str);
scanf("%d",&n);
while(n--){
int l,r;
char front[10]={0},end[10]={0},cut[205]={0};
scanf("%d %d %s %s",&l,&r,front,end);
cut_str(l,r,str,cut); //剪切操作
int x = find(str,front,end); //找到粘贴的位置
paste(x,str,cut); //粘贴操作
}
printf("%s",str);
}
L1-047 装睡
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805083282194432?type=7&page=0
#include<stdio.h>
int main()
{
int i=0;
int N=0,x=0,y=0;
scanf("%d",&N);
for(i=0;i<N;i++)
{
char name[10]={0};
scanf("%s %d %d",name,&x,&y);
if(x<15||x>20||y<50||y>70)
{
printf("%s\n",name);
}
}
return 0;
}
L1-033 出生年
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805099426070528?type=7&page=0
#include<stdio.h>
int judge(int y)
{
int x=0,i=0,count=0;
int num[10]={0};
for(i=0;i<4;i++)
{
x=y%10;
y=y/10;
num[x]++;
}
for(i=0;i<10;i++)
{
if(num[i]!=0) count++;
}
return count;
}
int main()
{
int y=0,n=0,count=0,year=0;
scanf("%d %d",&y,&n);
count=judge(y);
while(count!=n)
{
year++;
y++;
count=judge(y);
}
printf("%d %04d",year,y);
return 0;
}
L1-059 敲笨钟
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/1111914599412858880?type=7&page=0
L1-083 谁能进图书馆
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/1518581226784346112?type=7&page=0