帮助二维数组? [英] help with two dimensional array?

查看:65
本文介绍了帮助二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我正在实施C-36密码

hello
i am implementing C-36 cipher

const int wheels = 5;
const int positions = 26;
int pin[positions][wheels]={{0,1,0,1,0},{1,1,1,1,1},{1,1,0,0,0},{0,0,1,1,0},{1,1,1,0,1},{1,1,0,1,0},{0,1,1,1,1},{1,0,0,0,0},{0,0,1,0,0},{1,1,1,1,0},{0,0,0,1,1},{1,1,0,1,0},{1,0,1,0,1},{0,1,0,1,1},{1,1,0,0,0},{1,0,1,1,1},{1,0,1,0,1},{0,1,1,0,2},{1,0,1,1,2},{0,0,0,2,2},{0,1,0,2,2},{0,1,2,2,2},{0,0,2,2,2},{1,2,2,2,2},{0,2,2,2,2}};


该数组可以简单地通过
显示


this array can simply be displayed by

for(int i=0;i<25;i++)
{
     for(int j=0;j<5;j++)
     cout<<pin[i][j];
     cout<<endl;
}


我想要什么?
pin[position][wheel]==2
那么我需要将wheel的值重置为初始状态,即状态0


what i want?
when pin[position][wheel]==2
then i need that the values of wheel reset to initial state i.e. state 0

0	1	0	1	0
1	1	1	1	1
1	1	0	0	0
0	0	1	1	0
1	1	1	0	1
1	1	0	1	0
0	1	1	1	1
1	0	0	0	0
0	0	1	0	0
1	1	1	1	0
0	0	0	1	1
1	1	0	1	0
1	0	1	0	1
0	1	0	1	1
1	1	0	0	0
1	0	1	1	1
1	0	1	0	1
0	1	1	0	0
1	0	1	1	1
0	0	0	1	0
0	1	0	1	0
0	1	0	0	1
0	0	1	1	0
1	1	0	0	1
0	1	1	1	0



最好的问候

[Stefan_Lang对问题的解释]
引脚表示一组模式.每个模式都是5个值(0或1)的序列.存储为2的pin值实际上只是0或1的占位符.a 2代表的实际值可以直接从第一个和以下模式,但只有按顺序解释整个模式序列才能轻松得出:

当您遍历模式列表时,在给定位置中2的首次出现与该位置中第一个模式的位值相对应.第二次出现对应于该位置中第二个模式的位值,依此类推.

在上面给出的示例中,前17个样式不包含2,并且使用时不变.模式18在最后一个位置包含一个2,实际上,所有随后的模式在那个最后位置也包含一个2.对于模式18,最后一位应采用与第一个模式的最后一位相同的值.然后,模式19应使用第二个模式的最后一位,依此类推.模式20和随后的所有位在第二个最后一个位置都具有2,因此在模式20中,该位应采用第一个模式中相应位的值.模式,下一个模式然后使用模式1中的相应位,依此类推.

[Sherlock_Holmes_Mode]
上面的解释引起了一些问题,例如,如果在一个模式中将特定位置的值存储为2,而不是在以下模式中存储该怎么办?或者,如果应该从自身取走钻头的模式在该位置上有2,将会发生什么情况?显然已经假定不会发生这种情况,但是为什么呢?

尽管未在问题中说明,但使用术语wheel使我考虑到,实际上是要周期性地重复一系列比特值.换句话说,列n i中的位值实际上代表具有N[i]段的轮子上的值,其中-根据数组pin-N[0]==25, N[1]==23, N[2]==21, N[3]==19, N[4]==17[中的数据. (请注意,仅定义了25个模式,而没有定义26个(缺少一个吗?))

基于这些假设,不难提供合理的解决方案.只有一个悬而未决的问题,那就是我们是否预先知道车轮尺寸,还是必须通过搜索2的第一次出现来从pin数组中得出它们.如果没有更多信息,它将具有成为后者.
[/Sherlock_Holmes_Mode]


[/Stefan_Lang对问题的解释]



best regards

[interpretation of the problem by Stefan_Lang]
pin represents an array of patterns. Each pattern is a sequence of 5 values, either 0 or 1. A pin value stored as the value 2, is in fact just a placeholder for a 0 or 1. The actual value a 2 stands for can be directly derived from the first and following patterns, but can only be easily derived if the whole sequence of patterns is interpreted sequentially:

When you iterate through the list of patterns, the first occurence of a 2 in a given position corresponds to the bit value of the first pattern in that same position. The second occurence corresponds to the bit value of the second pattern in that position, and so on.

In the example given above, the first 17 patterns do not contain a 2 and are used unchanged. Pattern 18 contains a 2 in the last position, and in fact all following patterns also have a 2 in that last position. For pattern 18, the last bit should assume the same value as the last bit of the first pattern. Pattern 19 then should use the last bit of the second pattern, etc.. Pattern 20 and the following ones all have a 2 at the second last position, so in pattern 20, that bit should assume the value of the corresponding bit in the first pattern, the next pattern then uses the corresponding bit from pattern 1, etc..

[Sherlock_Holmes_Mode]
The above interpretation raises some questions, such as what would happen if in one pattern the value at a specific position is stored as 2, but not in the following pattern? Or what would happen if the pattern that the bit should be taken from itself has a 2 in that position? Obviously it was assumed this would not happen, but why?

The use of the term wheel made me consider, although not stated in the question, that the series of bit values are actually meant to be repeated periodically. In other words, the bit values in columnn i actually represent values on a wheel with N[i] segments, where - according to the data in array pin - N[0]==25, N[1]==23, N[2]==21, N[3]==19, N[4]==17[. (Note that there are only 25 patterns defined, not 26 (is one missing?))

Based on these assumptions it won''t be hard to provide a reasonable solution. There is only one open question, and that is whether or not we know the wheel sizes in advance, or have to derive them from the pin array by searching for the first occurence of a 2. Given no further information, it will have to be the latter.
[/Sherlock_Holmes_Mode]


[/interpretation of the problem by Stefan_Lang]

推荐答案

听起来像是您想要的

It sounds like you want

for(int position=0; position<positions; ++position)
    for(int wheel=0; wheel<wheels; ++wheel) //hard-coding 5 and 25 was a big crime!
        if (pin[position][wheel]==2)
             pin[position][wheel] = 0;



如果获得第一名的话看起来并不合理,但是...
另外,还不清楚怎么可能有问题.

—SA



It does not look reasonable if first place, but…
Also, it''s not clear how could it be a problem.

—SA


鉴于我在上面添加的解释,可能会这样:
Given the interpretation I added above, something like this might do:
const unsigned int positions = 25;
const unsigned int wheels = 5;
int pin[positions][wheels]; // add initialization array here

// determine the number of 'sections' for each wheel and store in the sizes array
void getWheelSizes(int array[positions][wheels], unsigned int sizes[wheels]) {
   for (unsigned int wheel = 0; wheel < wheels; ++wheel) {
      unsigned int n = 0;
      while (n < positions && array[n][wheel] < 2)
         ++n;
      sizes[wheel] = n;
   }
}
void print_position(unsigned int position, int array[positions][wheels], unsigned int sizes[wheels]) {
   for (unsigned int wheel = 0; wheel < wheels; ++wheel) {
      // calculate index with modulo operator: result is a value between 0 and sizes[wheel]-1
      unsigned int index = position % sizes[wheel];
      std::cout << array[index][wheel] << ' ';
   }
   std::cout << std::endl; // end of line
}
int main() {
   unsigned int sizes[wheels];
   getWheelSizes(pin, sizes);
   unsigned int loops = 123; // number of iterations doesn't matter, we can go on forever!
   for (unsigned int pos = 0; pos < loops; ++pos) { 
      print_position(pos, pin, sizes);
   }
   return 0;
}


这篇关于帮助二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆