码迷,mamicode.com
首页 > 其他好文 > 详细

1217: 01字串 [水题]

时间:2017-12-23 11:56:34      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:stdio.h   输出   hid   turn   规律   arc   技术分享   pos   log   

1217: 01字串 [水题]

时间限制: 1 Sec 内存限制: 128 MB

提交: 116 解决: 93 统计

题目描述

对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:

00000

00001

00010

00011

00100

请按从小到大的顺序输出这32种01串。

输入

本试题没有输入。 

输出

输出32行,按从小到大的顺序每行一个长度为5的01串。

来源

 

自己想的是暴力的解法,看了别人的博客后发现原来是有规律可循的

暴力解法

技术分享图片
#include<stdio.h>

int main()
{
    printf("00000\n");
    printf("00001\n");
    printf("00010\n");
    printf("00011\n");
    printf("00100\n");
    printf("00101\n");
    printf("00110\n");
    printf("00111\n");
    printf("01000\n");
    printf("01001\n");
    printf("01010\n");
    printf("01011\n");
    printf("01100\n");
    printf("01101\n");
    printf("01110\n");
    printf("01111\n");
    printf("10000\n");
    printf("10001\n");
    printf("10010\n");
    printf("10011\n");
    printf("10100\n");
    printf("10101\n");
    printf("10110\n");
    printf("10111\n");
    printf("11000\n");
    printf("11001\n");
    printf("11010\n");
    printf("11011\n");
    printf("11100\n");
    printf("11101\n");
    printf("11110\n");
    printf("11111\n");
    
    return 0;
}
View Code

规律解法

技术分享图片
#include <iostream>
using namespace std;
int main()
{
    for(int i=0;i<32;i++){
        cout<<i%32/16<<i%16/8<<i%8/4<<i%4/2<<i%2<<endl;
    }
    return 0;
}
View Code

 

1217: 01字串 [水题]

标签:stdio.h   输出   hid   turn   规律   arc   技术分享   pos   log   

原文地址:http://www.cnblogs.com/ruruozhenhao/p/8092289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!