A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zhiyuan0932 中级黑马   /  2014-9-16 19:35  /  981 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class LuckNumber
{
        public static void main(String[] args)
        {
                int[] arr = {1,2,3,4,5,6,7,8};

                int pos = -1;

                for(int x=0;x<arr.length-1; x++)
                {
                        for(int y=0; y<3;y++)
                        {
                                pos++;

                                if(pos==arr.length)
                                        pos=0;
                                while(arr[pos]==0)
                                {
                                        pos++;
                                        if(pos==arr.length)
                                                pos=0;
                                }
                        }
                        arr[pos] = 0;
                        //System.out.println(arr[pos]);
                }

                for(int x=0; x<arr.length; x++)
                {
                        if(arr[x]!=0)
                        {
                                System.out.println(arr[x]);
                                break;
                        }
                }
        }
}

2 个回复

倒序浏览
本帖最后由 xpaibeyond 于 2014-9-16 21:29 编辑

    被绕进去了,  如果没错的话, 结果应该是7.
x=0   pos=2   while no  arr[2]=0  
x=1   pos=5  while no   arr[5]=0
x=2   pos=8  pos=0  while no   arr[0]=0
x=3  y=0   pos=1   y=1    pos=2    while    pos=3 y=2  pos=4  arr[4]=0
x=4  y=0   pos=5  while  pos=6   y=1   pos=7  y=2 pos=8  pos=0  while pos=1  arr[1]=0   

x=5  y=0   pos=2 while pos=3   y=1   pos=4  while  pos=5   pos=6  y=2  pos=7  arr[7]=0

x=6  y=0   pos=8  pos=0  while  pos=1  while  pos=2   pos=3  y=1   pos=4    while  pos=5   while pos=6  
y=2  pos=7   while pos=8 pos=0   pos=1  while pos=2  pos=3  arr[3]=0



评分

参与人数 1技术分 +1 收起 理由
陈君 + 1

查看全部评分

回复 使用道具 举报
怎么又有人提这个问题呢.....
{1,2,3,4,5,6,7,8}
当x=0; {1,2,0,4,5,6,7,8}
当x=1; {1,2,0,4,5,0,7,8}
当x=2; {0,2,0,4,5,0,7,8}
当x=3; {0,2,0,4,0,0,7,8}
当x=4; {0,0,0,4,0,0,7,8}
当x=5; {0,0,0,4,0,0,7,0}
当x=6; {0,0,0,0,0,0,7,0}
经过3个数字后将该数字变成0,如果经过的数字中有0,则不计算跳过,这样写楼主应该就不会晕了.

评分

参与人数 1技术分 +1 收起 理由
陈君 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马