约瑟夫环

本文介绍了解决约瑟夫环问题的两种方法:递归方式与非递归方式。递归方式通过数学推导得出递推公式,并用Java实现;非递归方式则利用ArrayList动态数组简化操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:略

递归方式:

设有(1,2,3,……,k-1,k,k+1,……,n)n个数,当k出列时,那么有
k+1 -->1
k+2 -->2
...
n -->n-k
1 -->n-k+1
...
k-1 -->n-1
由上面一组式子可以推出,若知道新产生的n-1个数中某个数x,那么很显然可以推出x在原数列里的位置,即x‘=(x+k)%n,

比如说:有数1,2,3,4,5,6,7,8  八个数,数到4出列,

这样,第一次出列后按顺序剩下5,6,7,8,1,2,3   这时1在第5个位置,可以得知它原来在第(5+4)%9 = 1 个位置,

由此,我们可以得到一个递推公式
f[1]=1
f[n]=(f[n-1]+k)%n (n>1)
上面的递推公式中,在某种情况下,f[n-1]+k会整除n,如n=2,k=3,这时我们修要对上式进行修正:
f[n]=(f[n-1]+k)%n;if(f[n]==0)f[n]=n;()

public class ExeDemo65 {

	public static void main(String[] args) {
		int n = 0, k = 0;
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		k = sc.nextInt();
		System.out.println(jos(n, k));
	}

	public static int jos(int n, int k) {
		int x;
		if (n == 1)
			x = 1;
		else {
			x = (jos(n - 1, k) + k) % n;
			if (x == 0)
				x = n;
		}
		return x;
	}
}


非递归方式:

运用ArrayList类采用动态数组方式,使得问题简易方便。

public class ExeDemo65 {
	public static void main(String[] args) {
		ArrayList<String> al = new ArrayList<String>();
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int search = 0;
		for (int i = 1; i <= n; i++) {
			al.add(i + " ");
		}
		while (al.size() > 0) {
			search += (m - 1);
			search %= al.size();
			System.out.print(al.remove(search));
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值