2022-08-10 CVTE C/C++软件开发苏州
需要保密,就不写选择题题目了,也不记得多少了。
前20题有的是多选有的是单选,没有明确提示是多选还是单选,
但是可以去点击选项,可以选多个选项应该就是多选,后来知道的,但我全选的单选。
**第21题格雷码**
https://leetcode.cn/problems/gray-code/

```
#include<iostream>
#include<vector>
using namespace std;
// 1 0
// 11 10 00 01
// 111 110 100 101 001 000 010 011
std::vector<int> grayCode(int n){
if(n==1){
return {1,0};
}
std::vector<int> v = grayCode(n-1);
std::vector<int> ans;
for(auto i:v){
ans.push_back((1<<(n-1))|i);
}
for(int i=v.size()-1;i>=0;i--){
ans.push_back(v[i]);
}
return ans;
}
int main(){
int n;
cin>>n;
std::vector<int> gC = grayCode(n);
for(auto& i : gC)
cout<<i<<" ";
return 0;
}
```
不同的是力扣要求第一个数是0,这里第一个数是n位都是1的二进制数
可以做个转化在力扣上过:
```
class Solution {
public:
vector<int> f(int n){
if(n==1){
return {1,0};
}
std::vector<int> v = f(n-1);
std::vector<int> ans;
for(auto i:v){
ans.push_back((1<<(n-1))|i);
}
for(int i=v.size()-1;i>=0;i--){
ans.push_back(v[i]);
}
return ans;
}
std::vector<int> grayCode(int n){
vector<int> a = f(n);
vector<int> b;
int i=0;
while(a[i]!=0) i++;
int j=i;
cout<<i<<"\n";
while(j<1<<n){
b.push_back(a[j++]);
}
j=0;
while(j<i)
b.push_back(a[j++]);
return b;
}
};
```
**第22题二维图像差值**
```
#include<iostream>
using namespace std;
struct Image{
uint32_t* data;
uint32_t width;
uint32_t height;
};
// 宽/高 的比值分别是 u、v
// 两数相除只有一个比值,怎么来的两个比值
// P(result) = P(startx,starty)*(1-u)*(1-v) + P(startx,endy)*(1-u)*v + \
// P(endx,starty)*u*(1-v) + P(endx,endy)*u*v
// result 代表哪个点?
// 如果 uv是已知的常数,则 result = f(startx,starty,endx,endy)
// 说明两个点的位置确定一个终点的位置,四个点的值确定一个终点的值
// 0, 100,
// 100, 200
// 0, 50, 100,
// 50, 100, 150,
// 100, 150, 200
Image scale_image(const Image& src, uint32_t dst_width, uint32_t dst_height){
uint32_t sw = src.width, sh = src.height, dw = dst_width, dh = dst_height;
const uint32_t* sd = src.data;
Image dst;
dst.width = dw;
dst.height = dh;
dst.data = new uint32_t[dw*dh];
uint32_t* dd = dst.data;
int dis = (d[sw-1]-d[0])/(dw-1);
dd[0]=sd[0];
for(uint32_t i=1;i<dw;i++)
dd[i]=dd[i-1]+dis;
int t=1;
for(uint32_t i=dw;i<dw*sw;i+=dw){
dd[i] = dd[t++];
}
dis=(d[sw*sh-1]-d[sw-1])/(dh-1);
for(uint32_t i=2*dw-1;i<dw*dh;i+=dw){
dd[i] = d[i-dw] + dis;
}
for(uint32_t i = 1; i < dh; i++){
uint32_t* begin = dd[i*dw];
dis = (begin[dw-1]-begin[0])/(dw-1);
for(uint32_t j = 1; j < dw; j++)
begin[j] = begin[j-1] + dis;
}
return dst;
}
```
#cvte##CVTE#