/*

输出n位数据的格雷码
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of
gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
Note:
For a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

要求用向量做这道题,不能用公式
*/
#include<math.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

int main()
{
vector<int>num0;//if n is determined, the number need to be transformed
vector<int>num1;//from nomal number to 2 bit vector
vector<int>num2;//to grey
vector<int>num3;//to normal-binary
vector<int>num4;//to align the grey
int j=0;
//according to n generate part of number;
int n=4;

for (int i=0;i<pow(2,n);i++)
num0.push_back(i);

cout<<"Given n = "<<n<<" , return [ ";
for (int i=0;i<num0.size()-1;i++)
cout << num0[i] << ", ";
cout << num0[num0.size()-1];
cout<<" ].";
cout << endl;

for(j=0;j< pow(2,n);j++) {

vector<int>vnum;
int fnum=0;

fnum=num0[j];

int num=fnum;
int i=0;

for(i=0;num>2;i++)//frist push LSB
{

vnum.push_back(num%2);
num=num/2;
}
vnum.push_back(num%2);

int a=1;

num1.push_back(0);//add 0 in the MSB
for (i=1;i<=vnum.size();i++)
num1.push_back(vnum[vnum.size()-i]);

//generate gray code in binary
for (int i=0;i<num1.size()-1;i++)
num2.push_back(num1[i]^num1[i+1] );

//generate gray code in align
num4=num2;

int s=0;
int k=0;
s=n-num1.size();

for(k=0;k<=s;k++){
num4.push_back(0);
}

//grey to normal-binary
int c=0^num2[0];
num3.push_back(c);
for (int i=1;i<num2.size();i++)
{
c=c^num2[i];
num3.push_back(c);

}

//display

for (int i=0;i<num4.size();i++)
cout << num4[i] << " ";
cout <<" - "<<fnum<< endl;
num1.clear( );
num2.clear( );
num3.clear( );
num4.clear( );
}

return 0;
}

C++入门程序作业3的更多相关文章

  1. C++入门程序作业2

    程序在Dev-C++5.5.3版本运行 结构体的使用 给结构体赋值,打印出结构体中学生姓名,分数,平均分 #include <iostream>#include <cassert&g ...

  2. C++入门程序作业1

    将一个int A[]={ ,  ,  ,}定义的可能重复的数字去掉重复的元素. 了解向量,容器如何使用,size,地址的关系,理解unique erase函数的返回值是什么参数 结果:将1,1,1,2 ...

  3. mybatis入门_mybatis基本原理以及入门程序

    一.传统jdbc存在的问题 1.创建数据库的连接存在大量的硬编码, 2.执行statement时存在硬编码. 3.频繁的开启和关闭数据库连接,会严重影响数据库的性能,浪费数据库的资源. 4.存在大量的 ...

  4. 1.struts2原理和入门程序

    Struts2是一个MVC的Web应用框架,是在Struts1和WebWork发展起来的,以WebWork为核心,采取拦截器机制来处理用户请求. 原理图: 分析步骤: 1.用户发送一个请求 2.请求的 ...

  5. springMVC2 1入门程序

    1入门程序 .1需求 实现商品列表查询 .2需要的jar包 使用spring3.2.0(带springwebmvc模块) .1前端控制器 在web.xml中配置: <?xml version=& ...

  6. struts2入门程序

    struts2入门程序 1.示例 搭建编程环境就先不说了,这里假设已经搭建好了编程环境,并且下好了strut2的jar包,接下来程序. 1.1 新建web项目 点击File->New->D ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...

  8. springmvc(一) springmvc框架原理分析和简单入门程序

    springmvc这个框架真的非常简单,感觉比struts2还更简单,好好沉淀下来学习~ --WH 一.什么是springmvc? 我们知道三层架构的思想,并且如果你知道ssh的话,就会更加透彻的理解 ...

  9. python web入门程序

    python2.x web入门程序 #!/usr/bin/python # -*- coding: UTF-8 -*- # 只在python2.x 有效 import os #Python的标准库中的 ...

随机推荐

  1. 《javascript经典入门》-day01

    <javascript经典入门>-day01 1.了解JavaScript 01.浏览器每次加载和显示页面时,都在内存里创建页面及其全部元素的一个内部表示体系,,也就是DOM.在DOM里, ...

  2. Vue-CLI3.0版本配置BootStrap的方法

    一.引入jquery bootstrap popper   用   cnpm install jquery bootstrap@3 popper.js --save  用cnpm 来导入 用npm会出 ...

  3. Git基本命令整理

    git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id git co -- <file> # ...

  4. 笔记《JavaScript 权威指南》(第6版) 分条知识点概要3—表达式和运算符

    [表达式和运算符]原始表达式,初始化表达式(对象和数组的),函数定义表达式,属性访问表达式,调用表达式,对象创建表达式,运算符概述,算术表达式,关系表达式,逻辑表达式,赋值表达式,表达式计算,其他运算 ...

  5. Django全文检索(django-haystack+whoosh+jieba)

    前言: 全文检索就是针对所有内容进行动态匹配搜索的概念,针对特定的关键词建立索引并精确匹配达到性能优化的目的 class Whoose_seach(object): analyzer = Chines ...

  6. zookeeper应用与原理学习总结

    一.什么是zookeeper Zookeeper 分布式服务框架是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集群 ...

  7. oracle如何调试存储过程

    oracle如果存储过程比较复杂,我们要定位到错误就比较困难,那么可以存储过程的调试功能 先按简单的存储过程做个例子,就是上次做的存储过程(proc_test) 1.先在数据库的procedures文 ...

  8. 【cogs247】售票系统【线段树】

    售票系统 输入文件:railway.in 输出文件:railway.out 时间限制:1 s 内存限制:128 MB [问题描述] 某次列车途经C个城市,城市编号依次为1到C,列车上共有S个座位,铁路 ...

  9. python requests + xpath 获取分页详情页数据存入到txt文件中

    直接代码,如有不懂请加群讨论# *-* coding:utf-8 *-* #import jsonimport requestsimport pytesseractimport timeimport ...

  10. 大数据Hadoop入门视频教程:Hadoop的快如入门

    最新在学习hadoop .storm大数据相关技术,发现网上hadoop .storm 相关学习视频少之又少,这里整理了传智播客段海涛老师的hadoop学习视频,出来给大家学习交流. 视频下载地址:h ...