目录
7-1 Lily
百合花(Lily)是一种美丽的花。她通常一年只开一次花,所以如果你看到百合花盛开,它会非常珍贵。然而,她对猫有剧毒,所以你必须注意让好奇的猫远离可爱的百合花。
你有n个网格的土壤土地排成一行,从1到n,其中一些是百合花。我们不想伤害百合,也不想伤害猫。你可以在网格上放一些猫粮,但对于任何有猫粮的网格i,在区域[i−1,i+1]不得含有百合花。你喜欢猫和百合,所以你想最大限度地增加有猫粮的格子。
设计满足上述要求的计划。
输入格式:
有一个整数n(1≤n≤1000)表示网格的数量。
第二行包含仅由“L”和“.”组成的字符串R,表示有和没有百合花的格子。
输出格式:
输出包含一行,字符串R′仅由“L”、“.”组成和“C”,其中“C”表示在满足上述要求的同时分配给R中空网格的猫粮。
输入样例:
在这里给出一组输入。例如:
5
..L..
输出样例:
在这里给出相应的输出。例如:
C.L.C
解析:对于其中某一"."点,当它的两边都没有“L”时即可放猫粮,“.”转换为“C”即可。
字符串两端判断规则不同,分开判断。
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; string a; int main() { int n;scanf("%d",&n); cin>>a; for(int i=0;i<n;++i) { if(a[i]!='L') { if(i==0)//起始位置和末尾需单独判断 { if(a[i+1]!='L') a[i]='C'; } else if(i==n-1) { if(a[i-1]!='L') a[i]='C'; } else if(a[i-1]!='L'&&a[i+1]!='L') a[i]='C'; } } for(int i=0;i<n;++i) printf("%c",a[i]); return 0; }
7-2 a * b
给出两个不超过1000位的十六进制数a,b。
求a∗b的值
输入格式:
输入共两行,两个十六进制的数
输出格式:
输出一行,表示a∗b
输入样例:
在这里给出一组输入。例如:
1BF52
1D4B42
输出样例:
在这里给出相应的输出。例如:
332FCA5924
解析:大体思路与十进制的高精乘法相同,只有进位规则不同。
我们运算时直接将16进制的数以十进制放在位数数组中,数组中存储0~15,最后计算完成后再转换。
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; string a,b; int aa[100010]; int bb[100010]; int cc[100010]; int zh(char c)//转换为16进制 { if(c<='9'&&c>='0') return c-'0'; else return c-'A'+10; } char zh1(int t)//字符转换为数字 { if(t<10) return '0'+t; else return 'A'+t-10; } int main() { cin>>a>>b; int l1=a.length(); int l2=b.length(); for(int i=1;i<=l1;++i) { aa[i]=zh(a[l1-i]); } for(int i=1;i<=l2;++i) { bb[i]=zh(b[l2-i]); } for(int i=1;i<=l1;++i) { for(int j=1;j<=l2;++j) { cc[i+j-1]+=aa[i]*bb[j]; int cnt=cc[i+j-1]/16;//进位方式不同 cc[i+j]+=cnt; cc[i+j-1]%=16; } } int l=l1+l2+3; while(cc[l]==0) l--;//去起始零 for(int i=l;i>=1;--i) { printf("%c",zh1(cc[i])); } return 0; }
7-3 山头狙击战
题目描述
小明为了掩护大部队,单枪匹马同敌人周旋,后来被敌人包围在某山头……等等,为什么怎么听怎么像狼牙山五壮士!不过不用着急,这次小明携带了足够的弹药,完全可以将涌上来的敌人一个一个干掉。小明是个神枪手,只要他的枪膛中有子弹,他就能将在他射程m(用从敌人位置到山头的直线距离算)以内的一个敌人瞬间射杀。但如果在射程内没有敌人,出于节约子弹考虑和面子问题,小明会等待敌人靠近然后射击。
正当小明为自己的强大而自我膨胀时,他忽然发现了一个致命的失误:他携带的枪是单发枪,每射出一发子弹都必须花k秒钟的时间装子弹。而凶残的敌人才不会花时间等你换子弹呢。他们始终在以1m/s的速度接近山头。而如果在一个敌人到达山头时小明无法将他击毙,那么我们可怜的小明就将牺牲在敌人的刺刀下。现在小明用心灵感应向你发出求助:要保住自己的性命并且歼灭所有敌人,小明最多只能用多少时间给枪装上一发子弹?
说明:假设一开始小明的枪中就有一发子弹,并且一旦确定一个装弹时间,小明始终会用这个时间完成子弹的装卸。希望你能帮助小明脱离险境。
输入格式
每组输入数据,第一行有两个整数n和m,(2≤n≤100,000; 1≤m≤10,000,000)n代表敌人个数,m代表小明的射程。
接下来有n行,每行一个整数mi,(1≤mi≤10,000,000),代表每个敌人一开始相对山头的距离(单位为米)。
输出格式
每组输出数据仅有一个整数,代表小明的换弹时间(单位为秒)。
样例输入
6 100
236
120
120
120
120
120
样例输出
25
解析:二分答案。多出了最大射程,初始子弹等条件。
我们可以将敌人一齐匀速向小明移动视为小明匀速向静止的敌人移动,由于有初始子弹,我们直接将第一个敌人移除即可。
代码:
#include<iostream>//二分答案 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[10000010]; int n,m; int check(int k)//二分换弹时间 { int t=0;//目前时间(位置) int hd=0;//换弹缓冲时间 for(int i=1;i<=n;++i) { while(a[i]-t>m)//在未接近敌人且正处于换弹冷却时可以先填装子弹 { ++t; if(hd>=0) --hd; } t+=hd;hd=k; if(t>=a[i]) return 0;//碰到或超过敌人,即来不及打 } return 1; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) scanf("%d",&a[i]); sort(a+1,a+n+1);//为二分创造条件 int l=0,r=a[n]; while(l<r) { int mid=(l+r+1)/2; if(check(mid)) l=mid; else r=mid-1; } printf("%d",l); return 0; }
7-4 Reversing Linked List
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is an integer, and Next
is the position of the next node.
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
解析:英文题目,
看不懂。让我们对链表以k为区间进行翻转。
输入:第一行:链表初始的编号,元素个数,区间k。
第2~n+1行:第一列是链表序号,第二列是链表储存的值,第三列是下一项的编号。
输出:当前项链表编号,存储的值,然后下一项编号。按顺序输出。
方法:用另一个数组 b [ m ] 的下标 m 记录链表某一项的位置,数组存这一项的编号,之后直接接更改这个数组,用改过顺序的数组输出即可。
可能有不进入链表的无效数据,需要更新n。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct node { int val; int next; }a[100010]; int list[100010];//将链表位置和编号对应 int re[100010]; int n,k; int main() { int p;scanf("%d",&p); scanf("%d%d",&n,&k); for(int i=1;i<=n;++i) { int b,c; int l; scanf("%d%d%d",&b,&l,&c); a[b].val=l; a[b].next=c; } n=0; for(int i=p;i!=-1;i=a[i].next) { ++n; }//有不在链表中的无效数据,更新n值 list[1]=p; int cnt=n/k; for(int i=2;i<=n;++i) { list[i]=a[list[i-1]].next; } list[n+1]=-1; for(int i=1;i<=cnt;++i)//区间翻转 { int tot=0; for(int j=(i-1)*k+1;j<=(i*k);++j) { re[++tot]=list[j]; } for(int j=tot;j>=1;--j) { int h=i*k-j+1; list[h]=re[j]; } } for(int i=1;i<n;++i) { printf("%05d %d %05d\n",list[i],a[list[i]].val,list[i+1]); } printf("%05d %d -1\n",list[n],a[list[n]].val); return 0; }
7-5 一元三次方程
给定一个形如的一元三次方程。
已知该方程有三个不同的实数根(根与根之差的绝对值≥),且根范围均在[p,q]之间,你需要解出这个方程的三个根。
输入格式:
第一行一个整数T(1≤T≤1000),表示有T组数据
接下来T行,每行6个实数,分别表示a,b,c,d,p,q
数据保证:−102≤p,q≤102,且对于∀x∈[p,q],−106≤f(x)≤106
输出格式:
输出三个实数,表示方程的三个解。
你的答案可以以任意顺序输出。
一个答案被认为是正确的,当且仅当其与标准答案的绝对误差不超过10−6
输入样例:
在这里给出一组输入。例如:
1
1.000000 -5.000000 -4.000000 20.000000 -10.000000 10.000000
输出样例:
在这里给出相应的输出。例如:
-2.000000 2.000000 5.000000
提示1:
样例所给方程的图像如下:
解析:二分
函数满足:在单调区间内,函数值变化单调,此时寻找函数值等于0的x,很容易想到二分。
一元三次方程有两个转折点(极值点),我们可以将函数分为三段,分别二分求解。
对三次方程求导,导函数为零时,是方程极值点,由此分割。
连续函数 f(x) ,x1,x2 在定义域范围内,当 f (x1) * f (x2) < 0 时,函数值异号,表明函数在 x1,x2 间有零点。可由此缩小二分范围。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; double a,b,c,d,p,q; double eps=1e-10; double f(double x)//算函数值 { return a*x*x*x+b*x*x+c*x+d; } double solve(double l,double r)//二分 { while(l<r&&r-l>=eps) { double mid=(l+r)/2; if(f(l)*f(mid)<=0) r=mid; else l=mid; } return l; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&p,&q); double a1=3*a,b1=2*b; double mid1=(-b1-sqrt(b1*b1-4*a1*c))/2/a1; double mid2=(-b1+sqrt(b1*b1-4*a1*c))/2/a1; //求出函数极值点 if(mid1-mid2>eps) swap(mid1,mid2); double ans1=solve(p,mid1); double ans2=solve(mid1,mid2); double ans3=solve(mid2,q); printf("%.6lf %.6lf %.6lf\n",ans1,ans2,ans3); } return 0; }