C# 将数组向左平移 n 个位置

2 篇文章 0 订阅
</pre><pre name="code" class="csharp">最近在写棋牌房间座位号 处理, 服务器发送来座位号跟 玩家信息
</pre><pre name="code" class="csharp">但是要把当前用户显示在正中央,所以必须把服务器发来的座位号根据当前用户的座位号排列。
</pre><pre name="code" class="csharp">如果当前用户座位号位2  那么,要把服务器发来的座位号根据 2 来重新排列 ,
</pre><pre name="code" class="csharp">座位号数组向左平移俩个位置,下面就是平移数组的方法。
</pre><pre name="code" class="csharp">其实挺简单, 就是把向左平移的2个单位 先保存在临时数组中,
</pre><pre name="code" class="csharp">把剩下的座位 添加在 结果数组中 ,然后把临时数组中的数据跟在结果数组中。
</pre><pre name="code" class="csharp">将数组平移若干个位置
</pre><pre name="code" class="csharp">
    class Program {
        static void Main(string[] args) {

            int nr = 99;<span style="white-space:pre">				</span>//向左平移单位
            int[] orign = new int[] { 1, 2, 3, 4, 5 };<span style="white-space:pre">	</span>//原始数组
            int[] temp = new int[nr];<span style="white-space:pre">			</span>//临时数组
            int[] result = new int[orign.Length];<span style="white-space:pre">	</span>//结果数组
            nr = nr % orign.Length;<span style="white-space:pre">			</span>//偏移单位根据原始数组取余

            for (int i = 0; i < nr; i++) {
                temp[i] = orign[i];
            }
            for (int i = 0; i < orign.Length - nr; i++) {
                result[i] = orign[i + nr];
            }
            for (int i = 0; i < nr; i++) {
                result[orign.Length - nr + i] = temp[i];
            }
            for (int i = 0; i < result.Length; i++) {
                Console.WriteLine(result[i]);
            }

            Console.ReadKey();
        }

    }



输出结果 向左平移了 99 个单位




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值