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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
题解:
这个题很难绷,因为本人蒟蒻,当时考试的时候一直做不对,每次都有小地方出问题,最后狠下心来把代码删了从头理一遍思路,终于过了,这题没有涉及高级的方法,就是看能不能把情况考虑全面,条件能不能写对
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main() {
int a;
cin >> a;
string b;
cin >> b;
int qian = 0;
int hou = 0;
int c = 0;
for (int e = 0; e < a; e++) {
hou++;
if (b[e] == '.') {
c++;
if (qian == 0 && hou == a) {
for (int ee = 0; ee < a; ee++) {
cout << 'C';
}
}
else if (hou == a) {
if (c >= 2) {
cout << '.';
for (int ee = 0; ee < c - 1; ee++) {
cout << "C";
}
}
else {
for (int ee = 0; ee < c; ee++) {
cout << ".";
}
}
}
}
if (b[e] == 'L') {
if (qian == 0) {
if (c >= 2) {
for (int ee = 0; ee < c - 1; ee++) {
cout << "C";
}
cout << '.';
cout << 'L';
}
else {
for (int ee = 0; ee < c; ee++) {
cout << ".";
}
cout << 'L';
}
qian = 1;
c = 0;
}
else {
if (c >= 3) {
cout << '.';
for (int ee = 0; ee < c - 2; ee++) {
cout << "C";
}
cout << '.';
cout << 'L';
}
else {
for (int ee = 0; ee < c; ee++) {
cout << ".";
}
cout << 'L';
}
c = 0;
}
}
}
return 0;
}
7-2 a * b
给出两个不超过1000位的十六进制数a,b。
求a∗b的值
输入格式:
输入共两行,两个十六进制的数
输出格式:
输出一行,表示a∗b
输入样例:
在这里给出一组输入。例如:
1BF52
1D4B42
输出样例:
在这里给出相应的输出。例如:
332FCA5924
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
题解 :
这个题主要是看眼界宽不宽,能不能想到直接用十六进制列竖式做乘法,本蒟蒻当时考试的时候脑子一根筋,光想着把十六进制转化为十进制相乘,之后再把结果化为十六进制,可以说这个过程非常复杂和混乱,我到现在都没想通,直到我突然反应过来可以直接进行十六进制的竖式相乘,录入数据时,把字母化为对应的数值,相乘之后储存在一个数组里,输出时如果数字大于9,输出对应的字母,否则输出原数字,终于对了。
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int aa[1010] = {0};
int bb[1010] = {0};
int y = 1;
int zhongzhuan = 1;
int jieguo[100000] = { 0 };
int main()
{
string a, b;
cin >> a >> b;
int sizea = a.size();
int sizeb = b.size();
y = 1;
for (int e = sizea; e >= 1; e--) {
if (a[e-1] >= '0' && a[e-1] <= '9') {
aa[y] = a[e-1] - '0';
}
else {
aa[y] = a[e-1] - '0' - 7;
}
y++;
}
y = 1;
for (int e = sizeb; e >= 1; e--) {
if (b[e-1] >= '0' && b[e-1] <= '9') {
bb[y] = b[e-1] - '0';
// cout << b[e - 1] << " " << b[e - 1] - '0' << " " << bb[y] << endl;
}
else {
bb[y] = b[e-1] - '0' - 7;
//cout << b[e - 1] << " " << b[e - 1] - '0' << " " << bb[y] << endl;
}
y++;
}
/*for (int e = sizea; e >= 1; e--) {
cout << aa[e]<<" ";
}
cout << endl;
for (int e = sizeb; e >= 1; e--) {
cout << bb[e]<<" ";
}
cout << endl;*/
y++;
int jinwei = 0;
int k = 0;
int kk = 0;
for (int e = 1; e <= sizeb; e++) {
for (int ee = 1; ee <= sizea; ee++) {
zhongzhuan = aa[ee] * bb[e] + jinwei + jieguo[ee + k];
jinwei = zhongzhuan / 16;
zhongzhuan %= 16;
jieguo[ee + k] = zhongzhuan;
kk = ee + k;
if (ee == sizea && jinwei != 0) {
jieguo[ee + 1 + k] = jinwei;
kk = ee + 1 + k;
}
}
/*for (int e = kk; e >= 1; e--) {
cout << jieguo[e];
}*/
/* cout << endl;*/
jinwei=0;
k++;
}
for (int e = kk; e >= 1; e--) {
if (jieguo[e] >= 0 && jieguo[e] <= 9) {
cout << jieguo[e];
}
else if (jieguo[e] == 10) {
cout << 'A';
}
else if (jieguo[e] == 11) {
cout << 'B';
}
else if (jieguo[e] == 12) {
cout << 'C';
}
else if (jieguo[e] == 13) {
cout << 'D';
}
else if (jieguo[e] == 14) {
cout << 'E';
}
else if (jieguo[e] == 15) {
cout << 'F';
}
}
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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
题解:
这个题主要考二分法,题目不是很难,有个重要的地方是时间要累积统计,不然会超时,而且定义前标后标的时候要注意,前标等于0的时候如果为后标赋值的数组数据是1,那mid就会变成0,check函数就会持续返回错误的数据,好家伙这个坑我踩了一个多小时,一直优化逻辑怎么都全对不了,最后发现是这里出了问题。代码如下。
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>M;
bool check(int min,int b) {
int k = 0;
int jishi = min;
for (int e = 0; e <= M.back()+10; e++) {
/*cout << "e=" << e << endl;
cout << "k=" << k << endl;
cout << "jishi=" << jishi << endl;
cout << "M[k]=" << M[k] << endl;
cout << "M[k]-e=" << M[k]-e << endl;
cout << endl;*/
if (M[k] - e < 0) {
return 0;
}
if (M[k]-e<=b) {
if (jishi >= min) {
if (k == M.size()-1) {
return 1;
}
k++;
jishi = 0;
}
}
jishi++;
}
}
int main() {
int a, b;
cin >> a >> b;
M.resize(a);
for (int e = 0; e < a; e++) {
cin >> M[e];
}
sort(M.begin(), M.end());
int qian = 0;
int hou = M.back();
int zhong;
while (qian+1<hou) {
// cout << M.back();
//system("pause");
zhong = (qian + hou) / 2;
if (check(zhong, b)) {
qian = zhong;
}
else {
hou = zhong;
}
// cout << qian << " " << hou << " ";
//cout << "while出问题";
}
if (check(qian,b)) {
cout << qian;
/*cout << endl;
cout << "qian";*/
}
else if(check(hou,b)) {
cout << hou;
/*cout << endl;
cout << "hou";*/
}
system("pause");
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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
题解:
这个题敲了好久,因为把题目看错了,我一直以为只用翻转一次,但他是翻转好多次,最后看对题目以后整对了,这个题主要考链表,回顾一下,链表的建立,输入很简单,占不了多长时间,但是翻转需要考虑诸多因素,我用了两个循环嵌套,外循环负责翻转次数,内循环负责每组的翻转,需要注意的是这个题有一个很ex人的地方就是他会输入不在链表里的数据,所以还需要遍历一遍链表记录链表长度,ac代码如下:
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
using namespace std;
int zancun;
int i;
struct s {
int dizhi;
int xuhao;
int next;
int next1;
};
s sz[1000000];
void shuru(int dizhi,int xuhao,int next) {
sz[dizhi].xuhao =xuhao;
sz[dizhi].dizhi =dizhi;
sz[dizhi].next =next;
sz[dizhi].next1=next;
}
int main()
{
int a, b, c;
cin >> a >> b >> c;
sz[a].dizhi = a;
for (int e = 0; e < b; e++) {
int aa, bb, cc;
cin >> aa >> bb >> cc;
shuru(aa, bb, cc);
}
int xxx=1;
for (int i = a; sz[i].next != -1; i = sz[i].next) {
xxx++;
}
int x = xxx /c;
int jiedian;
zancun = a;
int xx = 0;
int jiedian2;
int kaitou;
if (c != 1) {
for (int ee = 0; ee < x; ee++) {
jiedian = zancun;
for (int e = 0; e < c - 1; e++) {
sz[sz[zancun].next1].next = sz[zancun].dizhi;
zancun = sz[zancun].next1;
if (ee == 0 && e == c - 2) {
kaitou = zancun;
}
}
if (ee > 0) {
sz[jiedian2].next = sz[zancun].dizhi;
}
if (ee != x - 1) {
zancun = sz[zancun].next1;
}
jiedian2 = jiedian;
if (ee == x - 1) {
sz[jiedian].next = sz[zancun].next1;
}
}
i = kaitou;
}
else {
i = a;
}
for ( ; sz[i].next != -1; i = sz[i].next) {
cout << setw(5) << setfill('0') << sz[i].dizhi;
cout << " " << sz[i].xuhao << " ";
cout << setw(5) << setfill('0') << sz[i].next;
cout << endl;
}
cout << setw(5) << setfill('0') << sz[i].dizhi;
cout << " " << sz[i].xuhao << " ";
cout << sz[i].next;
system("pause");
return 0;
}
7-5 一元三次方程
给定一个形如ax3+bx2+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:
对于方程:ax2+bx+c=0(Δ=b2−4ac≥0),它的两个根分别为:x1=2a−b−Δ,x2=2a−b+Δ
代码长度限制
16 KB
时间限制
500 ms
内存限制
128 MB
题解:
这个题主要考察二分法(其实别的方法也可以,舍友用了数学方法反而更快更简单),先求导算出两个极值点x1x2,然后在p ,x1,x2,q四点间的三个区间里分别用二分法求出对应的解,需要注意的是x3的系数a可能小于0,需要及时调整x1x2的顺序,另外需要注意数据调整要用double,int会丢失数据,下附ac代码:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double qiugen1(double a, double b, double c) {
double derta = b * b - 4 * a * c;
derta = sqrt(derta);
double x1 = (-b - derta) / 2 / a;
return x1;
}
double qiugen2(double a, double b, double c) {
double derta = b * b - 4 * a * c;
derta = sqrt(derta);
double x2 = (-b +derta) / 2 / a;
return x2;
}
bool check(double mid,double a, double b, double c, double d) {
double x = a * pow(mid, 3) + b * pow(mid, 2) + c * mid + d;
if (x<0) {
return 1;
}
else {
return 0;
}
}
int main()
{
double T;
cin >> T;
double a, b, c,d,p,q;
for (int e = 0; e < T; e++) {
cin >> a >> b >> c>>d>>p>>q;
//cout << a << " " << b << " " << c << " " << d << " " <<p << " " << q << endl;
double x1 = qiugen1(a * 3, b * 2, c);
double x2 = qiugen2(a * 3, b * 2, c);
if (x1 > x2) {
double zhongzhuan = x1;
x1 = x2;
x2 = zhongzhuan;
}
/*cout << x1 << " x1" << endl;
cout << x2 << " x2" << endl;
system("pause");*/
double qian = p;
double hou = x1;
while (hou - qian > 1e-9) {
/*cout << qian << " " << hou << endl;*/
double mid = (qian + hou) / 2;
if (check(mid, a, b, c, d)) {
if (a > 0) {
qian = mid;
}
else {
hou = mid;
}
}
else {
if (a > 0) {
hou = mid;
}
else {
qian = mid;
}
}
}
double jieguo1 = qian;
qian = x1;
hou = x2;
while (hou - qian > 1e-9) {
double mid = (qian + hou) / 2;
//cout << qian << " " << hou <<" "<<mid << endl;
if (check(mid, a, b, c, d)) {
if (a > 0) {
hou = mid;
}
else {
qian = mid;
}
}
else {
if (a > 0) {
qian = mid;
}
else {
hou = mid;
}
}
}
double jieguo2 = qian;
qian = x2;
hou = q;
while (hou - qian > 1e-9) {
double mid = (qian + hou) / 2;
if (check(mid, a, b, c, d)) {
if (a > 0) {
qian = mid;
}
else {
hou = mid;
}
}
else {
if (a > 0) {
hou = mid;
}
else {
qian = mid;
}
}
}
double jieguo3 = qian;
cout << setiosflags(ios::fixed) << setprecision(6) << jieguo1 << " " << jieguo2 << " " << jieguo3 << endl;
}//最外层for循环
system("pause");
return 0;
}