HDOJ/HDU 2700 Parity(奇偶判断~)

简介: Problem Description A bit string has odd parity if the number of 1’s is odd. A bit string has even parity if the number of 1’s is even.

Problem Description
A bit string has odd parity if the number of 1’s is odd. A bit string has even parity if the number of 1’s is even.Zero is considered to be an even number, so a bit string with no 1’s has even parity. Note that the number of
0’s does not affect the parity of a bit string.

Input
The input consists of one or more strings, each on a line by itself, followed by a line containing only “#” that signals the end of the input. Each string contains 1–31 bits followed by either a lowercase letter ‘e’ or a lowercase letter ‘o’.

Output
Each line of output must look just like the corresponding line of input, except that the letter at the end is replaced by the correct bit so that the entire bit string has even parity (if the letter was ‘e’) or odd parity (if the letter was ‘o’).

Sample Input
101e
010010o
1e
000e
110100101o
#

Sample Output
1010
0100101
11
0000
1101001010

英文题~看懂题意就ok了。
e代表的是偶数奇偶性校验。
o代表的是奇数奇偶性校验。
0代表是。1代表否~
其实就是判断1的个数~

import java.util.Scanner;

/**
 * @author 陈浩翔
 */
public class Main{

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.next();
            if(str.equals("#")){
                return ;
            }
            int num=0;
            for(int i=0;i<str.length()-1;i++){
                if(str.charAt(i)=='1'){
                    num++;
                }
            }
            if(num%2==0){//1出现的次数为偶数
                if(str.charAt(str.length()-1)=='e'){//偶数奇偶校验
                    //0代表判断正确
                    System.out.println(str.substring(0, str.length()-1)+'0');
                }else{
                    System.out.println(str.substring(0, str.length()-1)+'1');
                }
            }else{
                if(str.charAt(str.length()-1)=='o'){//奇数奇偶校验
                    //0代表判断正确
                    System.out.println(str.substring(0, str.length()-1)+'0');
                }else{
                    System.out.println(str.substring(0, str.length()-1)+'1');
                }
            }
        }
    }
}
目录
相关文章
|
前端开发 rax 算法
互动游戏解决遇到问题之Phaser和Rax双渲染引擎的分层设计的问题如何解决
互动游戏解决遇到问题之Phaser和Rax双渲染引擎的分层设计的问题如何解决
273 2
互动游戏解决遇到问题之Phaser和Rax双渲染引擎的分层设计的问题如何解决
|
机器学习/深度学习 人工智能 算法
跨平台AI模型部署神器:FastDeploy实战Yolov5
跨平台AI模型部署神器:FastDeploy实战Yolov5
944 1
|
存储 Windows
下载Windows ISO镜像的方法
一、镜像介绍 1、大概介绍 .iso 是电脑上镜像的存储格式之一,所以通常在电脑中以后缀.iso命名,俗称iso镜像文件。 2、详细介绍 ISO镜像文件_百度百科 二、下载Windows 11镜像 1、Windows 11 官方下载网址 https://www.microsoft.com/zh-cn/software-download/windows11 2、步骤 点击官网地址,进入windows11官网,找到下载 Windows 11 磁盘映像 (ISO)
9730 0
|
Linux 网络安全 Apache
无需公网IP——搭建第一个网站
无需公网IP——搭建第一个网站
|
Java Python
你知道Java的重写与重载,在Python中该如何实现吗?
你知道Java的重写与重载,在Python中该如何实现吗?
515 0
|
缓存 大数据 CDN
背靠中台实现基于大数据驱动的国际化电商架构(二)
一、背景 老生常谈国际化的技术挑战: 1)国际电商面临的是全球消费者,天然延时大; 2)面临国际互联互通的网络质量问题; 3)国际电商技术还将面临如何快速应对合规、政策等问题; 4)需要发展本地化业务来补足消费者需求,而本地化将面临与全球各地的第三方Service Provider对接; 5)全球范围内的灾备; 6)全球数据一致性; 7)竞争环境及很多的不确定性,需要我们快速迭代试错 这些挑战需要我们用智能高效的技术解决方案。
3772 0
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17594 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全

热门文章

最新文章