69,393
社区成员
发帖
与我相关
我的任务
分享
private static string fristRow(string lastCol)
{
int index0 = lastCol.Length;
int[] link = new int[index0];
foreach (char c in lastCol) index0 -= c & 1;
for (int index1 = lastCol.Length, index = lastCol.Length - 1; index >= 0; --index)
{
link[lastCol[index] == '0' ? --index0 : --index1] = index;
}
char[] value = new char[lastCol.Length];
index0 = link[0];
for (int end = lastCol.Length, index = 0; index != end; ++index, index0 = link[index0])
{
value[index] = lastCol[index0];
}
return new string(value);
}
private static string fristRow(string lastCol)
{
int index0 = lastCol.Length;
int[] link = new int[index0];
foreach (char c in lastCol)
{
if (c == '1') --index0;
}
for (int index1 = lastCol.Length, index = lastCol.Length - 1; index >= 0; --index)
{
link[lastCol[index] == '0' ? --index0 : --index1] = index;
}
char[] value = new char[lastCol.Length];
index0 = link[0];
for (int end = lastCol.Length, index = 0; index != end; ++index, index0 = link[index0])
{
value[index] = lastCol[index0];
}
return new string(value);
}
#include <stdio.h>
/* 定义序列位数 */
#define LEN 5
/* 简单的排序
* 序列1 --> 序列2*/
static void sort(size_t *array, size_t len)
{
size_t i, j;
size_t dst,tmp;
for (i = 0; i < len; i++) {
dst = i;
for (j = i+1; j < len; j++) {
if ( array[dst] > array[j]) {
dst = j;
}
}
if (dst != i) {
tmp = array[i];
array[i] = array[dst];
array[dst] = tmp;
}
}
}
/* 验证序列是否正确
* @dst,为目标序列,如二进制 11000 */
static size_t check(size_t *array, size_t len, size_t dst)
{
size_t num = 0;
size_t i;
for (i = 0; i < len; i++) {
num = (num << 1) | (array[i] & 1);
}
return (num == dst);
}
/* 更新序列1 */
static void update(size_t *array, size_t len, size_t new)
{
size_t i = 0;
for (i = 0; i < len - 1; i++) {
array[i] = array[i+1];
}
array[len-1] = new;
}
static void __printf(size_t num)
{
size_t i = 0;
size_t bit = 0;
for (i = 0; i < LEN; i++) {
bit = (num & (1 << (LEN - i - 1))) >> (LEN - i - 1);
printf("%d", bit);
}
}
int main()
{
size_t a = 0x14; /* 起始二进制 10100*/
size_t i = 0;
size_t dst = 0x18; /* 比对序列 11000 */
size_t array[LEN] = {0};
while (1){
/* 对序列移位操作*/
a = ((~(1 << (LEN -1)) & a) << 1) | ((a & (1 << (LEN-1))) >> (LEN -1));
if (i < LEN) {
/* 初始化序列1*/
array[i++] = a;
} else {
/* 排序,得到序列2,然后校对 */
sort(array, LEN);
if (check(array, LEN, dst)) {
__printf(array[0]);
printf(" 是你要找的序列\n");
break;
} else {
update(array, LEN, a);
}
}
}
/* 打印出序列2 */
printf("序列2: \n");
for (i = 0; i < LEN; i++) {
__printf(array[i]);
printf("\n");
}
printf("\n");
return 0;
}