对对碰

【链接】nbu1811点击打开链接

【题目】

对对碰

Time Limit:1000MS  Memory Limit:65536K

Description

对对碰游戏:倘若有连续的两个字符相等,则发生对对碰,消掉这两个,合并被截开的两段。

Input

多组测试数据,每组输入一个字符串s(都是小写英文字母),长度小于500,000,

Output

每组输出一行,发生对对碰的次数。

Sample Input

AbCC
AbCCb

Sample Output

1
2

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对对碰游戏是一种简单的益智类游戏,可以使用 Java 编程语言实现。 以下是一个简单的对对碰游戏 Java 实现的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MatchGame extends JFrame implements ActionListener { private JButton[] buttons; private int[] values; private int firstButtonIndex, secondButtonIndex; private int count; private Timer timer; public MatchGame() { setTitle("Match Game"); setSize(400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new GridLayout(4, 4)); getContentPane().add(mainPanel); buttons = new JButton[16]; values = new int[16]; for (int i = 0; i < 8; i++) { int value = i + 1; for (int j = 0; j < 2; j++) { int k; do { k = (int) (Math.random() * 16); } while (values[k] != 0); values[k] = value; } } for (int i = 0; i < 16; i++) { buttons[i] = new JButton(); buttons[i].setBackground(Color.white); buttons[i].addActionListener(this); mainPanel.add(buttons[i]); } timer = new Timer(1000, this); } public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { JButton button = (JButton) e.getSource(); int index = -1; for (int i = 0; i < 16; i++) { if (buttons[i] == button) { index = i; break; } } if (index >= 0 && values[index] != 0) { button.setText("" + values[index]); button.setEnabled(false); if (count == 0) { firstButtonIndex = index; count = 1; } else { secondButtonIndex = index; count = 0; timer.start(); } } } else if (e.getSource() == timer) { timer.stop(); if (values[firstButtonIndex] == values[secondButtonIndex]) { values[firstButtonIndex] = 0; values[secondButtonIndex] = 0; } else { buttons[firstButtonIndex].setText(""); buttons[firstButtonIndex].setEnabled(true); buttons[secondButtonIndex].setText(""); buttons[secondButtonIndex].setEnabled(true); } } } public static void main(String[] args) { MatchGame game = new MatchGame(); game.setVisible(true); } } ``` 该代码使用 `JFrame` 和 `JButton` 等 Swing 组件实现了对对碰游戏的界面。在构造函数中生成随机的 16 个数字,并将它们随机分配到 16 个按钮上。当用户点击一个按钮时,该按钮上的数字将被显示出来。如果是第一次点击,则记录下该按钮的位置;如果是第二次点击,则比较两个按钮上的数字是否相同。如果相同,则将这两个按钮禁用,否则将这两个按钮的数字隐藏起来。 还有一个 `Timer` 对象用于在比较两个按钮上的数字时等待一段时间,以便用户可以看到这两个数字。如果两个数字不相同,则在等待一段时间后将这两个按钮的数字隐藏起来。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值