1-1
百合花(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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
解题思路:模拟
依次遍历所有网格,如果是“.”并且它的前面和后面都无“L”,则用“C”代替“.”
如果是“L”,则往后面继续遍历
完整代码如下:
#include <bits/stdc++.h> using namespace std; int n; char a[1005]; int main() { cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; for(int i=1; i<=n; i++){ if(a[i]=='.' && a[i+1]!='L' && a[i-1]!='L') a[i]='C'; } for(int i=1; i<=n; i++) cout<<a[i]; return 0; }
1-2
给出两个不超过1000位的十六进制数a,b。 求a∗b的值
输入格式:
输入共两行,两个十六进制的数
输出格式:
输出一行,表示a∗b
输入样例:
在这里给出一组输入。例如:
1BF52 1D4B42
输出样例:
在这里给出相应的输出。例如:
332FCA5924
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
解题思路:高精度乘法
注意这里是用十六进制,用字符串输入后,字母转化为数字减‘7’ e.g. 'A'—>10 ‘A’ -‘7’
完整代码如下:
#include <bits/stdc++.h> using namespace std; char a1[1005],b1[1005],c1[10005]; int a[1000],b[1005],c[100005]; int main() { cin>>a1>>b1; int len_a,len_b; len_a=strlen(a1); len_b=strlen(b1); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); for(int i=1; i<=len_a; i++) { if(a1[len_a-i]>='A' && a1[len_a-i]<='Z') a[i]=a1[len_a-i]-'7'; else a[i]=a1[len_a-i]-'0'; } for(int i=1; i<=len_b; i++){ if(b1[len_b-i]>='A' && b1[len_b-i]<='Z') b[i]=b1[len_b-i]-'7'; else b[i]=b1[len_b-i]-'0'; } //倒序把数字存入数组中 int x; for(int i=1; i<=len_a; i++){ x=0; for(int j=1; j<=len_b; j++){ c[i+j-1]=c[i+j-1]+x+a[i]*b[j]; x=c[i+j-1]/16; c[i+j-1]%=16; } c[i+len_b]=x; } //乘法 int len_c=len_a+len_b; while(c[len_c]==0 && len_c>1) len_c--; //去除前面的零 for(int i=1; i<=len_c; i++){ if(c[i]>=10 && c[i]<=30) c1[i]=c[i]+'7'; else c1[i]=c[i]+'0'; } //重新转化为十六进制的字符 for(int i=len_c; i>=1; i--) cout<<c1[i]; }//'A'-'7' 10+'7'
1-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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
解题思路:二分答案
处理时应把每次敌人前进的距离存起来,要判断下一个敌人时再使用距离(不要每操作一次就使全部敌人前进一次,防止超时)
完整代码如下:
#include <bits/stdc++.h> using namespace std; const int maxn=1e5+5; int n,m,a[maxn],b[maxn],mid; int check(int x); int main() { cin>>n>>m; memset(a,0,sizeof(a)); for(int i=1; i<=n; i++) cin>>a[i]; sort(a+1,a+1+n); for(int i=1; i<=n; i++) b[i]=a[i]; int l=0,r=m; while(l<=r){ mid=(l+r)/2; if(check(mid)) l=mid+1; else r=mid-1; for(int i=1; i<=n; i++) a[i]=b[i]; } cout<<l-1; return 0; } int check(int x) { int t,sum=0; for(int j=1; j<=n; j++){ if(a[j]>m){ t=a[j]-m; sum+=t; } //判断是否在射程内 sum+=x; if(a[j]>=0){ if(j==n) return 1; a[j+1]-=sum; //射杀一个后,后一个敌人的距离 }else{ return 0; } } }
1-4
7-4 Reversing Linked List
分数 300
全屏浏览题目切换布局
作者 陈越
单位 浙江大学
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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
解题思路:单向链表的反转
1先创建一个包含自己地址,自己数字,下一个连结地址的结构体进行输入构成链表
2然后再创建一个结构体,从起点节点开始,把链表进行排序存入
3然后再用reverse直接根据题意对链表进行反转
4最后输出链表
完整代码如下:
#include <bits/stdc++.h> using namespace std; const int maxn=1e6+5; struct stu{ int my; int id; int next; }; struct stu t1[maxn],t2[maxn]; int main() { int begin,n,k; cin>>begin>>n>>k; if(!n) begin=-1; int beg,idd,ne; for(int i=0; i<n; i++){ cin>>beg>>idd>>ne; t1[beg].my=beg; t1[beg].id=idd; t1[beg].next=ne; }//输入所有的节点 int t=0; for(int i=begin; i!=-1; i=t1[i].next) t2[t++]=t1[i];//按序排列能串联的链表 int j=0; for(int i=t; i>=k; i-=k){ reverse(t2+j,t2+j+k); j+=k; }//反转链表 for(int i=0; i<t; i++){ if(i!=t-1) cout<<setw(5)<<setfill('0')<<t2[i].my<<" "<<t2[i].id<<" "<<setw(5)<<setfill('0')<<t2[i+1].my<<endl; else cout<<setw(5)<<setfill('0')<<t2[i].my<<" "<<t2[i].id<<" "<<"-1"<<endl; } return 0; }
1-5
一元三次方程
给定一个形如ax^3+bx^2+cx+d=0的一元三次方程。
已知该方程有三个不同的实数根(根与根之差的绝对值≥10−6),且根范围均在[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:
样例所给方程的图像如下:
提示2:
对于方程:ax^2+bx+c=0(Δ=b^2−4ac≥0),它的两个根分别为:x1,x2
代码长度限制
16 KB
时间限制
500 ms
内存限制
128 MB
解题思路:二分答案
先求一元三次方程的极值点,来缩小范围
再在三个范围内进行二分
#include <bits/stdc++.h> using namespace std; int t; double a,b,c,d,p,q,mid; double func(double m); int main() { cin>>t; while(t--){ cin>>a>>b>>c>>d>>p>>q; double x1,x2,t1,t2; t1=(-2*b-sqrt(4*b*b-12*a*c))/(6*a); t2=(-2*b+sqrt(4*b*b-12*a*c))/(6*a); //先把一元三次方程求导,一元二次方程的根就是两个极值点 for(double i=p; i<=t1; i++){ x1=i,x2=i+1; if(func(x1)==0) cout<<fixed<<setprecision(6)<<x1<<" "; else if(func(x1)*func(x2)<0){ while(fabs(x1-x2)>1e-7){ mid=(x1+x2)/2; if(func(x1)*func(mid)<0) x2=mid; else x1=mid; } cout<<fixed<<setprecision(6)<<x1<<" "; } } for(double i=t1; i<=t2; i++){ x1=i,x2=i+1; if(func(x1)==0) cout<<fixed<<setprecision(6)<<x1<<" "; else if(func(x1)*func(x2)<0){ while(fabs(x1-x2)>1e-7){ mid=(x1+x2)/2; if(func(x1)*func(mid)<0) x2=mid; else x1=mid; } cout<<fixed<<setprecision(6)<<x1<<" "; } } for(double i=t2; i<=q; i++){ x1=i,x2=i+1; if(func(x1)==0) cout<<fixed<<setprecision(6)<<x1<<" "; else if(func(x1)*func(x2)<0){ while(fabs(x1-x2)>1e-7){ mid=(x1+x2)/2; if(func(x1)*func(mid)<0) x2=mid; else x1=mid; } cout<<fixed<<setprecision(6)<<x1<<" "; } } cout<<endl; } return 0; } double func(double m) { return a*m*m*m+b*m*m+c*m+d; }