编程介的小学生 2019-08-14 21:53 采纳率: 20.5%
浏览 131

一个用C语言程序来解决的算法,Arithmetically Challenged

Problem Description
Challenge 24 is a popular mathematics game used in many grade schools. In each game, contestants are given a card with four positive integers i1, i2, i3, i4 on it, and the first one who can use all of these numbers and any combination of the four basic arithmetic operations to get 24 wins. Each of the numbers i1, i2, i3, i4 must be used exactly once. Division can be used only if the divisor evenly divides the dividend (i.e., you can perform 6/2 but not 6/4). For example, if the card contains the numbers 7, 2, 5 and 1, possible solutions are (7-2)*5-1 or (7+1)*(5-2). Hmmm . . . this sounds like a source of a good programming problem.

Write a program that determines the longest consecutive sequence of integers that can be obtained by different ways of arithmetically combining the four integers. For example, with 7, 2, 5 and 1 the longest consecutive sequence is -18 to 26 (yes, we're allowing final results to be negative). The "+" and "-" operators must be used as binary operators, not as unary signs.

Input
Each test case will consist of a single line containing the four, not necessarily distinct, positive integers, none of which will exceed 100. A line containing four 0’s will terminate input.

Output
For each test case, output the case number and the longest consecutive sequence of obtainable values, in the format shown in the sample output. If there is more than one longest consecutive sequence, use the one with the largest first value.

Sample Input
7 2 5 1
8 15 38 3
0 0 0 0

Sample Output
Case 1: -18 to 26
Case 2: 150 to 153

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services