UVALive4167 HDU2700 Parity【水题】_the parity problem returns 1 if the input that are-程序员宅基地

技术标签: # ICPC-水题题解三  HDU2700  # ICPC-UVALive  UVALive4167  Parity  # ICPC-HDU  

Parity

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4842    Accepted Submission(s): 3635

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
Source

Regionals 2008 >> North America - Mid-Central USA


问题链接UVALive4167 HDU2700 Parity

题意简述:参见上文。

问题分析:这是一个计算奇偶校验码问题。

程序说明

  由于函数gets()不被推荐使用(容易造成存储越界访问),所有使用函数fgets()来读入一行字符串。

  使用函数fgets()时,需要考虑字符串末尾的'\n''\0',关系大数组的大小。所以符号常量N不能过小,不然会WA。

题记:函数gets()还是可以用的,需要谨慎使用。最好是使用函数fgets()。


AC的C语言程序如下:

/* UVALive4167 HDU2700 Parity */

#include <stdio.h>

#define N 32 + 2
char s[N+1];

int main(void)
{
    while(fgets(s, N, stdin) != NULL) {
        if(s[0] == '#')
            break;

        int i = 0, cnt = 0;
        while(s[i] != '\n' && s[i]) {
            if(s[i] == '1')
                cnt++;
            i++;
        }

        i--;
        if(s[i] == 'e') {
            if(cnt % 2 == 0)
                s[i++] = '0';
            else
                s[i++] = '1';
        } else if(s[i] == 'o') {
            if(cnt % 2 == 0)
                s[i++] = '1';
            else
                s[i++] = '0';
        }

        s[i] = '\0';
        printf("%s\n", s);
    }

    return 0;
}





版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tigerisland45/article/details/74019847

智能推荐

dos下关于按照日期格式导出数据库dmp文件-程序员宅基地

文章浏览阅读223次。[code="c"]set /A dt=%date:~0,4%%date:~5,2%%date:~8,2%-10set expdt=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%%TIME:~0,2%%TIME:~3,2%del *%dt%*exp lg_dbwizard/lg1qaz@oanet_172.23.8.37 file=d:\dmp\am\l..._数据库导出dmp文件可以选择数据日期吗

ffmpeg java 压缩视频_java 使用ffmpeg压缩视频-程序员宅基地

文章浏览阅读100次。1.在linux下使用的话,需要导入jave包//java代码private boolean ffmpegChange1 (File file,File fileSave){try{AudioAttributes audio = new AudioAttributes();audio.setCodec("libmp3lame");audio.setBitRate(new Integer(56000..._ffmpeg压缩视频获取进度java

PCM音频实时播放:音频字节数组(16/8位)转为PCM ArrayBuffer流_实时播放pcm数据流-程序员宅基地

文章浏览阅读2.8k次,点赞2次,收藏6次。PCM音频实时播放:音频字节数组(16/8位)转为PCM ArrayBuffer流_实时播放pcm数据流

陷波滤波器设计_陷波器设计-程序员宅基地

文章浏览阅读2.3w次,点赞15次,收藏166次。在信号处理中经常要把某些单频(窄带)干扰信号去除,例如求系统采集信号中的把工频信号滤除。实际上有一个很好的方法,便是使用陷波器。陷波器是一种特殊的带阻滤波器,陷波滤波器主要用于消除某个特定频率的干扰,数字陷波器一般为IIR滤波器。由于陷波器频率特性的特殊性,它除了可采用双线性变换进行设计外,还可以采用所谓零极点配置的方法进行设计。陷波滤波器是无限冲击响应(IIR)数字滤波器,该滤波器可以用以下线性差分方程表示:陷波滤波器的理想频率特性曲线如下图所示:图1 陷波滤波器的理想频率特性曲线_陷波器设计

get_magic_quotes_gpc() 函数-程序员宅基地

文章浏览阅读605次。在php的配置文件中,有个布尔值的设置,就是magic_quotes_runtime,当它打开时,php的大部分函数自动的给从外部引入的(包括数据库或者文件)数据中的溢出字符加上反斜线。 当然如果重复给溢出字符加反斜线,那么字符串中就会有多个反斜线,所以这时就要用set_magic_quotes_runtime()与get_magic_quotes_runtime()设置和检测php.

tensorflow pb格式模型预测规范写法。不然耗时多,或者内存溢出_self.sess.graph.as_default()加入名字-程序员宅基地

文章浏览阅读654次。背景:加载pb格式模型文件并预测解决方法:要声明新的图和session并复用,# 加载 self.graph = tf.Graph() # 为每个类(实例)单独创建一个graphwith self.graph.as_default(): output_graph_def = tf.GraphDef() pb_path = wenlp_configs["sentence_matcher"]["pb_model_path"] with open(pb_path, ._self.sess.graph.as_default()加入名字

随便推点

信息爆炸时代:全自动文章采集工具助你解决找文难题-程序员宅基地

文章浏览阅读115次,点赞5次,收藏4次。现代社会信息爆炸在信息泛滥的现代社会,网络成为我们获取信息的主要渠道之一。网络文章丰富多样,涵盖各个领域。然而,海量的网上信息让人无从下手,寻找自己需要的文章变得困难重重。在此背景下,全自动文章数据采集工具应运而生,自动化技术使我们能更轻松地获取所需信息。

VsCode预览Geojson数据_vscode展示json数据-程序员宅基地

文章浏览阅读511次。VsCode预览Geojson数据。_vscode展示json数据

KITTI数据集下载链接-程序员宅基地

文章浏览阅读1.9w次,点赞21次,收藏87次。1、简介KITTI数据集由德国卡尔斯鲁厄理工学院和丰田美国技术研究院联合创办,是目前国际上最大的自动驾驶场景下的计算机视觉算法评测数据集。该数据集用于评测立体图像(stereo),光流(optical flow),视觉测距(visual odometry),3D物体检测(object detection)和3D跟踪(tracking)等计算机视觉技术在车载环境下的性能。KITTI包含市区、乡村和高速公路等场景采集的真实图像数据,每张图像中最多达15辆车和30个行人,还有各种程度的遮挡与截..._kitti数据集下载

SpringBoot banner_springbootbanner-程序员宅基地

文章浏览阅读177次。Spring Boot 打印banner当应用启动时,可以显示应用图标,版本,名称等相关信息,可以通过配置文件指定banner打印模式:spring: main: banner-mode: OFF,CONSOLE,LOG具体流程如下SpringApplication.run(String… args) 执行打印banner入口,SpringApplication:public ConfigurableApplicationContext run(String... args) { _springbootbanner

【网络安全】逻辑漏洞保姆级讲解_安全圈 王师傅 逻辑漏洞-程序员宅基地

文章浏览阅读2k次,点赞53次,收藏56次。逻辑漏洞超强解读_安全圈 王师傅 逻辑漏洞

stc单片机c语言程序头文件(stc12c5a60s2.h,stc12c5a60s2头文件在keil中没法用?-程序员宅基地

文章浏览阅读2.2k次。下面那段程序,头文件是编译正常在stc89c52中正常运行,当然在stc12c5a32s2中不行,就是我上次提的问题。我把头文件改成编译出错,提示:Build target '目标 1'assembling STARTUP.A51...compiling 舵机回中.c...舵机回中.C(33): error C202: 'P2_7': undefined identifier舵机回中.C(35):..._assembling startup.a51... compiling main.c... main.c(3): error c202: 'p3': u