Mathematical Caclulation Problem
Given the formula for distance between a point and a line, we can simpliyf the calculation to |x-y| * 50:
#include <iostream>
#include <cmath>
int main() {
int x, y;
std::cin >> x >> y;
std::cout << abs(y - x) * 50 << '\n';
return 0;
}
String Output Problem
Simple string output challenge:
#include <iostream>
int main() {
std::cout << "To iterate is human, to recurse divine.\n";
return 0;
}
Contact List Management
Implementing a contact list with search functionality:
#include <iostream>
#include <vector>
#include <string>
struct Contact {
std::string name;
std::string phone;
std::string email;
std::string address;
std::string notes;
};
int main() {
int n;
std::cin >> n;
std::vector<Contact> contacts(n);
for(auto& c : contacts) {
std::cin >> c.name >> c.phone >> c.email >> c.address >> c.notes;
}
int queries;
std::cin >> queries;
while(queries--) {
int index;
std::cin >> index;
if(index >= 0 && index < n) {
auto& c = contacts[index];
std::cout << c.name << ' ' << c.address << ' ' << c.notes << ' '
<< c.email << ' ' << c.phone << '\n';
} else {
std::cout << "Not Found\n";
}
}
return 0;
}
Basic Arithmetic Operations
Performing fundamental arithemtic operations with formatted output:
#include <iostream>
#include <iomanip>
int main() {
int a, b;
std::cin >> a >> b;
std::cout << a << " + " << b << " = " << a + b << '\n';
std::cout << a << " - " << b << " = " << a - b << '\n';
std::cout << a << " * " << b << " = " << a * b << '\n';
if(b == 0) {
std::cout << a << " / " << b << " = Error\n";
} else if(a % b == 0) {
std::cout << a << " / " << b << " = " << a / b << '\n';
} else {
std::cout << a << " / " << b << " = " << std::fixed
<< std::setprecision(2) << (double)a / b << '\n';
}
return 0;
}
Unique Digit Year Calculation
Finding years with a specified number of unique digits:
#include <iostream>
#include <set>
#include <iomanip>
int countUniqueDigits(int year) {
std::set<int> digits;
if(year < 1000) digits.insert(0);
while(year > 0) {
digits.insert(year % 10);
year /= 10;
}
return digits.size();
}
int main() {
int start_year, target_count;
std::cin >> start_year >> target_count;
for(int year = start_year; ; year++) {
if(countUniqueDigits(year) == target_count) {
std::cout << year - start_year << ' '
<< std::setw(4) << std::setfill('0') << year << '\n';
break;
}
}
return 0;
}