编程介的小学生 2017-02-08 15:11 采纳率: 20.5%
浏览 1029
已采纳

The Water Bowls

Description

The cows have a line of 20 water bowls from which they drink. The bowls can be either right-side-up (properly oriented to serve refreshing cool water) or upside-down (a position which holds no water). They want all 20 water bowls to be right-side-up and thus use their wide snouts to flip bowls.

Their snouts, though, are so wide that they flip not only one bowl but also the bowls on either side of that bowl (a total of three or -- in the case of either end bowl -- two bowls).

Given the initial state of the bowls (1=undrinkable, 0=drinkable -- it even looks like a bowl), what is the minimum number of bowl flips necessary to turn all the bowls right-side-up?
Input

Line 1: A single line with 20 space-separated integers
Output

Line 1: The minimum number of bowl flips necessary to flip all the bowls right-side-up (i.e., to 0). For the inputs given, it will always be possible to find some combination of flips that will manipulate the bowls to 20 0's.
Sample Input

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0
Sample Output

3
Hint

Explanation of the sample:

Flip bowls 4, 9, and 11 to make them all drinkable:
0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [initial state]
0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 4]
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 9]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping bowl 11]

  • 写回答

1条回答 默认 最新

  • octopusflying 2017-02-10 05:21
    关注
     #include<stdio.h>
    
    int compute(int* num) {
        int result = 0; 
    
        for(int i = 1; i < 19; i++) {
            if(num[i - 1] == 1) {
                num[i - 1] = 1 - num[i - 1];
                num[i] = 1 - num[i];
                num[i + 1] = 1 - num[i + 1]; 
                result++;
            }
        }
    
        if(num[18] == 1) {
            num[18] = 1 - num[18];
            num[19] = 1 - num[19];
            result++;
        }
    
        if(num[19] == 0) {
            return result;
        } else {
            return 21;
        }
    }
    
    int main(void) {
        int num1[20], num2[20];
    
        for(int i = 0; i < 20; i++) {
            scanf("%d", &num1[i]);
            num2[i] = num1[i];
        }
    
        int result1 = 0;
        int result2 = 0;
    
        result1 += compute(num1);
    
        num2[0] = 1 - num2[0];
        num2[1] = 1 - num2[1];
        result2 = 1;
    
        result2 += compute(num2);
    
        printf("%d\n", result1 > result2 ? result2 : result1);
    
        return 0;
    } 
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services