Revisar la estructura de datos 1: preguntas de cepillado pta (solo preguntas con 20 puntos o más)
Pregunta 1: 01-Complejidad 2 Suma máxima posterior (25 puntos)
Mi código de CA:
#include <iostream>
using namespace std;
const int maxn = 10005;
int n, a[maxn], s, t, j, ans = -1, cnt;
int main() {
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> a[i];
cnt += a[i];
if (cnt < 0)
{
cnt = 0;
j = i + 1;
}
else if (cnt > ans)
{
ans = cnt;
s = j;
t = i;
}
}
if (ans >= 0)
cout << ans << ' ' << a[s] << ' ' << a[t];
else
cout << 0 << ' ' << a[0] << ' ' << a[n - 1];
return 0;
}
Pregunta 2: 02-Estructura lineal 2 Multiplicación y suma de polinomios de una variable (20 puntos)
Mi código de CA:
Nota: Si esta pregunta está escrita en una lista vinculada, ¡será bastante problemático!
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Node {
int v, e;
Node(int v_, int e_) :v(v_), e(e_) {
}
};
vector<Node> vec1, vec2, ansSum, ansMul;
int n, m;
bool cmp(Node n1, Node n2) {
return n1.e > n2.e; }
void Add() {
vector<Node>::iterator it1, it2;
it1 = vec1.begin(), it2 = vec2.begin();
while (vec1.end() != it1 && vec2.end() != it2) {
if (it1->e == it2->e) {
if (0 != it1->v + it2->v)
ansSum.push_back(Node(it1->v + it2->v, it1->e));
it1++;
it2++;
}
else if (it1->e > it2->e) {
if (0 != it1->v)
ansSum.push_back(Node(it1->v, it1->e));
it1++;
}
else {
if (0 != it2->v)
ansSum.push_back(Node(it2->v, it2->e));
it2++;
}
}
while (vec1.end() != it1) {
if (0 != it1->v)
ansSum.push_back(Node(it1->v, it1->e));
it1++;
}
while (vec2.end() != it2) {
if (0 != it2->v)
ansSum.push_back(Node(it2->v, it2->e));
it2++;
}
sort(ansSum.begin(), ansSum.end(), cmp);
}
void Mul() {
vector<Node>::iterator it1, it2;
for (it1 = vec1.begin(); vec1.end() != it1; ++it1)
for (it2 = vec2.begin(); vec2.end() != it2; ++it2)
if (0 != it1->v * it2->v)
ansMul.push_back(Node(it1->v * it2->v, it1->e + it2->e));
sort(ansMul.begin(), ansMul.end(), cmp);
}
void Show(vector<Node> v) {
if (0 == (int)v.size())
{
cout << 0 << ' ' << 0 << endl;
return;
}
for(int i = 0;i < (int)v.size();++i)
for (int j = i + 1; j < (int)v.size(); ++i)
{
if (v[i].e != v[j].e)
break;
else
{
v[i].v += v[j].v;
v[j].v = 0;
}
}
for (int i = 0; i < (int)v.size() - 1; ++i)
if (v[i].v)
cout << v[i].v << ' ' << v[i].e << ' ';
if (v[(int)v.size() - 1].v)
cout << v[(int)v.size() - 1].v << ' ' << v[(int)v.size() - 1].e << endl;
}
int main() {
cin >> n;
while (n--) {
int a, b;
cin >> a >> b;
vec1.push_back(Node(a, b));
}
cin >> m;
while (m--) {
int a, b;
cin >> a >> b;
vec2.push_back(Node(a, b));
}
Mul();
Show(ansMul);
Add();
Show(ansSum);
return 0;
}
Pregunta 3: Contratación de Google 1094 (20 puntos)
Mi código de CA:
#include <iostream>
#define LL long long
using namespace std;
const int maxn = 1005;
int n, k, book;
char a[maxn] = {
'\0' };
LL String_to_Integer(int s) {
LL res = 0;
for (int i = s; i < s + k; ++i)
res = res * 10 + (a[i] - '0');
return res;
}
bool check(LL s) {
for (int i = 2; i * i <= s; ++i)
if (!(s % i))
return false;
return true;
}
int GetLen(LL s) {
int Len = 0;
while (s) {
s /= 10;
Len++;
}
return Len;
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i <= n - k; ++i) {
LL Temp = String_to_Integer(i);
if (check(Temp))
{
book = 1;
if (k > GetLen(Temp))
for (int i = 0; i < k - GetLen(Temp); ++i)
cout << 0;
cout << Temp;
break;
}
}
if (!book)
cout << 404;
return 0;
}