Watermelon Division: C++ Code Solution for Even Number Split
"Watermelon Division: C++ Code Solution for Even Number Split" This C++ program determines if a watermelon can be divided into two parts, each weighing an even number of kilos. It's a classic coding challenge with a simple yet effective solution. \n\nProblem Description:\nOn a hot summer day, Pete and Billy want to share a watermelon but they only want to divide it into two parts where each part weighs an even number of kilos. Your task is to write a C++ program that determines if this is possible based on the watermelon's weight. \n\nInput:\nThe input is a single integer w representing the weight of the watermelon (1 <= w <= 100). \n\nOutput:\nThe program should output "YES" if the watermelon can be divided as desired, and "NO" otherwise. \n\nC++ Code:\nc++\n#include <iostream>\nusing namespace std;\n\nint main() {\n int w;\n cin >> w;\n \n // If w is even and greater than 2, it can be split into two even parts\n if (w % 2 == 0 && w > 2) {\n cout << \"YES\" << endl;\n } else {\n cout << \"NO\" << endl;\n }\n \n return 0;\n}\n\n\nExplanation:\nThe code works by checking if the watermelon weight w is even (divisible by 2) and greater than 2. If both conditions are true, then the watermelon can be divided into two even parts. Otherwise, it cannot be divided as desired. \n\nExample:\nInput: 8\nOutput: YES\n\nInput: 5\nOutput: NO\n\nThis code provides a straightforward and efficient solution to the watermelon division problem. It demonstrates basic C++ input/output operations and conditional logic, making it a good example for beginners to learn from.
原文地址: https://www.cveoy.top/t/topic/pRCw 著作权归作者所有。请勿转载和采集!