从0加到9很容易吗?

今天玩了玩递归,没想到玩出事情了

简单的一个问题:从0加到9

一般我们是这么做:

package com.rjxy.p1;

public class Sum {
	
	public static void main(String args[]) {
		int sum = 0;
		for (int i = 0; i <= 9; i++) {
			sum += i;
		}
		System.out.println(sum);
	}
}

但现在不让你用循环,只准用递归,怎么破?
其实也不难,代码如下:

package com.rjxy.p1;
public class Sum {
	public static int addAll(int[] a, int begin) {
		int sum;
		if(begin == a.length)
			return 0;
		sum = a[begin] + addAll(a, begin+1);
		return sum;
	}
	public static int addAll2(int a[], int end) {
		if(end < 0)
			return 0;
		return a[end] + addAll2(a, end-1);
	}
	
	public static void main(String args[]) {
		int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
		System.out.println(addAll(a, 0));
		System.out.println(addAll2(a, 9));
	}
}

这两个递归是我们通常情况下用的,一个正着来,一个反着来,很简单相信大家都懂,但现在我不想用这种方法,我想用二分法,怎么破?
我就想试试,就随手打了段代码,真的是随手,但是居然成功了!
代码如下:

package com.rjxy.p1;

public class Sum {
	public static int addAll2(int a[], int end) {
		if(end < 0)
			return 0;
		return a[end] + addAll2(a, end-1);
	}
	
	public static int addAll3(int a[], int begin, int end) {
		if(end==begin)
			return a[begin];
		int s1 = addAll3(a, begin, (begin+end)/2);
		int s2 = addAll3(a,((begin+end)/2+1),end);
		return s1 + s2;
	}
	public static void main(String args[]) {
		int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
		System.out.println(addAll2(a, 9));
		System.out.println(addAll3(a, 0, 9));
	}
}

运行结果是正确的,大家可以试一下!关键是运行的过程,花了我好大功夫,画出来是方法栈好像是后序遍历调用的,大家看我的过程,若有不对,请留言,小编及时改正!
下面这张是左子树的调用原理:

在这里插入图片描述
整棵树是这样的:
在这里插入图片描述
后续遍历层层返回累加和。
递归这东西还大有深意,以后有了感悟,在和大家分享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用Java写出RSA使用公钥解密的方法: ```java import java.security.*; import java.security.spec.*; import javax.crypto.*; import javax.crypto.spec.*; import java.util.Base64; public class RSADecryptionExample { private static final String PUBLIC_KEY_STRING = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjRcaCJES+VPnOdJ7dSNPctoMC67ix5ju5Gi5+1qa5l9JuDKrA4YScNAv6Qktega4w4U+4ZBjzTXFvlUXlfU6zS+6GxzLSe8OvAYjNxoDwF1R0yuZb8jvzJx3cqH/9ztk" + "vPQTETCcTtsTihjpR/wVS1RsLsFJ/s3xqoX9QHkbKFEYBIt2fB1wAtWo4i4zW8jv1/U6ssnai6S9Ro6CfpYuo1+0a0J48KctCFiynZmI5Z5AA5Kx0CxX9rL7VglY/evuI7CIwEmjKuIxNUszAZsmOcNJPVBeOHD9+EjYJyGc5iAitvv7lI+P/mo5D982Q5n5fwIDAQAB"; /** * Decrypts the specified cipher text using the specified public key string. * * @param cipherText the cipher text to decrypt * @param publicKeyString the public key string to use for decryption * @return the decrypted plain text * * @throws Exception if an error occurs while decrypting */ public static String decrypt(String cipherText, String publicKeyString) throws Exception { // Convert the cipher text and public key string to bytes byte[] cipherTextBytes = Base64.getDecoder().decode(cipherText); byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyString); // Create a public key from the byte array X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); // Decrypt the cipher text Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] plainTextBytes = cipher.doFinal(cipherTextBytes); // Return the decrypted plain text as a string return new String(plainTextBytes, "UTF-8"); } public static void main(String[] args) throws Exception { // Example usage String cipherText = "W88pknTCSXS7IuBSATGHe1yLhM69mIz7VtnB0Co0z7V/uqZKWw5QPN31NYsf5rNtvIlzuL0ykl93cEzDtfe9EQ=="; String plainText = decrypt(cipherText, PUBLIC_KEY_STRING); System.out.println(plainText); } } ``` 请注意,此示例代码仅适用于使用PKCS#1 padding算法的RSA密钥。如果使用其他算法,请相应地更改代码。另外,使用公钥进行解密通常不是一种很好的实践,因为这可能会暴露您的公钥并使系统容易受到中间人攻击。通常,RSA加密是使用私钥进行加密,使用公钥进行解密。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值