C++入门程序作业3
/*
输出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的更多相关文章
- C++入门程序作业2
程序在Dev-C++5.5.3版本运行 结构体的使用 给结构体赋值,打印出结构体中学生姓名,分数,平均分 #include <iostream>#include <cassert&g ...
- C++入门程序作业1
将一个int A[]={ , , ,}定义的可能重复的数字去掉重复的元素. 了解向量,容器如何使用,size,地址的关系,理解unique erase函数的返回值是什么参数 结果:将1,1,1,2 ...
- mybatis入门_mybatis基本原理以及入门程序
一.传统jdbc存在的问题 1.创建数据库的连接存在大量的硬编码, 2.执行statement时存在硬编码. 3.频繁的开启和关闭数据库连接,会严重影响数据库的性能,浪费数据库的资源. 4.存在大量的 ...
- 1.struts2原理和入门程序
Struts2是一个MVC的Web应用框架,是在Struts1和WebWork发展起来的,以WebWork为核心,采取拦截器机制来处理用户请求. 原理图: 分析步骤: 1.用户发送一个请求 2.请求的 ...
- springMVC2 1入门程序
1入门程序 .1需求 实现商品列表查询 .2需要的jar包 使用spring3.2.0(带springwebmvc模块) .1前端控制器 在web.xml中配置: <?xml version=& ...
- struts2入门程序
struts2入门程序 1.示例 搭建编程环境就先不说了,这里假设已经搭建好了编程环境,并且下好了strut2的jar包,接下来程序. 1.1 新建web项目 点击File->New->D ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...
- springmvc(一) springmvc框架原理分析和简单入门程序
springmvc这个框架真的非常简单,感觉比struts2还更简单,好好沉淀下来学习~ --WH 一.什么是springmvc? 我们知道三层架构的思想,并且如果你知道ssh的话,就会更加透彻的理解 ...
- python web入门程序
python2.x web入门程序 #!/usr/bin/python # -*- coding: UTF-8 -*- # 只在python2.x 有效 import os #Python的标准库中的 ...
随机推荐
- YY的GCD
YY的GCD 给出T个询问,询问\(\sum_{i=1}^N\sum_{j=1}^M(gcd(i,j)\in prime)\),T = 10000,N, M <= 10000000. 解 显然质 ...
- Expression基础体验
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...
- The Last Week
二轮省选前的最后一周了呢. 一路走到这里,真的很希望能继续走下去. 好好调整一下状态,争取能有机会买D吧(虽然现在似乎D也没什么用了 day1 多项式 多项式ln 多项式exp day2 数据结构 L ...
- 还能不能愉快地起一个web服务啦?——1st Step!白话http和代码交互的那点儿事儿~
学写python的时候,我们多多少少都接触到了web程序,然而你有没有想过,当浏览器发送了一个http请求时,等待接收这个请求的后端代码是一种什么样的思想感情? 就像下面这张图里画的一样,后端也许是一 ...
- docker 启动 nginx 服务
docker run -d -p 80:80 --restart=always nginx:latest 参数说明: run 启动某个镜像 -d 让容器在后台运行 -p 指定端口映射,宿主机的80端口 ...
- Windows许可证即将到期激活教程
电脑提示Windows许可证即将到期,于是自己就在网上找了一些教程,但是并没有激活成功,反而由即将到期变为了通知状态,尝试了各种密钥都不行,也下载了激活工具如暴风激活工具,KMS都不管用,尝试了好多方 ...
- JAVA写接口傻瓜(#)教程(四)
接上篇 7.sevlert 啊啊啊终于写到最重要的实现部分了.Servlet = Service + Applet,表示小服务程序.Servlet 是在服务器上运行的小程序.这个词是在 Java ap ...
- powercli 学习随笔
powercli就不多介绍了,基于powershell的好东西,可以百度 #PowerShell基本语法 # 注释 <# .... #> 多行注释 $VAR 所有变量的声明或者使用都需要加 ...
- Form表单发送到服务器时的编码方式
---恢复内容开始--- 表单中的表单中enctype是设置表单的MIME编码. 所谓MIME编码,是指当服务器传送数据给客户端时,必须指定这个文件是什么类型,才能方便客户端调用相应的应用软件来打开该 ...
- spark streamingcontext
一个StreamingContext定义之后,必须做以下几件事情:1.通过创建输入DStream来创建输入数据源.2.通过对DStream定义transformation和output算子操作,来定义 ...