2022/city/10-9/numere solutions

This commit is contained in:
2025-05-15 20:26:44 +03:00
parent 467da5d0bc
commit 8c73dfceea
2 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
#include <bits/stdc++.h>
using namespace std;
// ifstream in("test.in");
// ofstream out("test.out");
int main(int argc, char *argv[]) {
string inName = "test.in";
string outName = "test.out";
if (argc > 1) {
inName = argv[1];
cout << "Using \"" << inName << "\" for test name" << endl;
} else {
cout << "Warning: no in name provided, defaulting to test.in" << endl;
}
if (argc > 2) {
outName = argv[2];
cout << "Using \"" << outName << "\" for out name" << endl;
} else {
cout << "Warning: no out name proviced, defaulting to test.out" << endl;
}
ifstream in(inName);
ofstream out(outName);
int n;
in >> n;
unordered_map<char, int> numMapping;
numMapping['I'] = 1;
numMapping['V'] = 5;
numMapping['X'] = 10;
numMapping['L'] = 50;
numMapping['C'] = 100;
numMapping['D'] = 500;
numMapping['M'] = 1000;
string tmpRoman;
out << n << endl;
for (int i = 0; i < n; i++) {
in >> tmpRoman;
if (!tmpRoman.size()) {
out << "0" << endl;
continue;
}
int total = 0;
int checkpoint = numMapping[tmpRoman[0]];
int stretchTotal = 0;
for (int j = 0; j < tmpRoman.size(); j++) {
int charDex = numMapping[tmpRoman[j]];
if (charDex == checkpoint) {
stretchTotal += charDex;
} else if (charDex > checkpoint) {
total += charDex - stretchTotal;
checkpoint = charDex;
stretchTotal = 0;
} else {
total += stretchTotal;
checkpoint = charDex;
stretchTotal = charDex;
}
}
total += stretchTotal;
// cout << total << endl;
out << total << endl;
}
return 0;
}

View File

@@ -0,0 +1,80 @@
#include <bits/stdc++.h>
using namespace std;
// ifstream in("test.in");
// ofstream out("test.out");
int main(int argc, char *argv[]) {
string inName = "test.in";
string outName = "test.out";
if (argc > 1) {
inName = argv[1];
cout << "Using \"" << inName << "\" for test name" << endl;
} else {
cout << "Warning: no in name provided, defaulting to test.in" << endl;
}
if (argc > 2) {
outName = argv[2];
cout << "Using \"" << outName << "\" for out name" << endl;
} else {
cout << "Warning: no out name proviced, defaulting to test.out" << endl;
}
ifstream in(inName);
ofstream out(outName);
int n;
in >> n;
unordered_map<char, int> numMapping;
numMapping['I'] = 1;
numMapping['V'] = 5;
numMapping['X'] = 10;
numMapping['L'] = 50;
numMapping['C'] = 100;
numMapping['D'] = 500;
numMapping['M'] = 1000;
char tmpRoman;
out << n << endl;
for (int i = 0; i < n; i++) {
in >> tmpRoman;
out << numMapping[tmpRoman] << endl;
// if (!tmpRoman.size()) {
// out << "0" << endl;
// continue;
// }
// int total = 0;
// int checkpoint = numMapping[tmpRoman[0]];
// int stretchTotal = 0;
// for (int j = 0; j < tmpRoman.size(); j++) {
// int charDex = numMapping[tmpRoman[j]];
// if (charDex == checkpoint) {
// stretchTotal += charDex;
// } else if (charDex > checkpoint) {
// total += charDex - stretchTotal;
// checkpoint = charDex;
// stretchTotal = 0;
// } else {
// total += stretchTotal;
// checkpoint = charDex;
// stretchTotal = charDex;
// }
// }
// total += stretchTotal;
// // cout << total << endl;
// out << total << endl;
}
return 0;
}