Created
January 7, 2026 00:09
-
-
Save kms0219kms/3a90395c200a9ef1713234547c493286 to your computer and use it in GitHub Desktop.
2025-2026 오성고_공동교육_정보과학 Day 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def main(): | |
| n = int(input("숫자를 입력하세요: ")) | |
| sum_alternating_formula(n) | |
| def sum_alternating_formula(n): | |
| if n % 2 == 0: | |
| return n // 2 | |
| else: | |
| return -(n + 1) // 2 | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def main(): | |
| n = int(input("숫자를 입력하세요: ")) | |
| sum_alternating_loop(n) | |
| def sum_alternating_loop(n): | |
| total = 0 | |
| for i in range(1, n + 1): | |
| if i % 2 == 0: # 짝수는 + | |
| total += i | |
| else: # 홀수는 - | |
| total -= i | |
| return total | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def main(): | |
| n = int(input("숫자를 입력하세요: ")) | |
| sum_alternating_pythonic(n) | |
| def sum_alternating_pythonic(n): | |
| return sum((-i if i % 2 == 1 else i) for i in range(1, n + 1)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment