Cals算法

public class Cals { static byte temp = 0; static byte temp2 = 0; public static void main(String[] args) { String num1 = "1111"; String num2 = "111"; char[] nu1 = num1.toCharArray(); char[] nu2 = num2.toCharArray(); int len = nu1.length > nu2.length ? nu1.length + 1 : nu2.length + 1; byte[] n1 = new byte[len]; byte[] n2 = new byte[len]; for (int i = nu1.length - 1; i >= 0; i--) { n1[len - (nu1.length - i)] = (byte) (nu1[i] - 48); } for (int i = nu2.length - 1; i >= 0; i--) { n2[len - (nu2.length - i)] = (byte) (nu2[i] - 48); } String re = ""; for (int i = len - 1; i >= 0; i--) { re = opt(n1[i], n2[i]) + re; } System.out.println(re); String re2 = ""; for (int i = len - 1; i >= 0; i--) { re2 = opt2(n1[i], n2[i]) + re2; } System.out.println(re2); } static byte opt(byte c1, byte c2) { byte r = (byte) (c1 + c2 + temp); temp = (byte) (r / 10); return (byte) (r % 10); } static byte opt2(byte c1, byte c2) { if ((c1 + temp2) >= c2) { return (byte) (c1 + temp2 - c2); } else { byte b = (byte) (10 + c1 + temp2 - c2); temp2 = -1; return b; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值