菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
	public static String add(String s1, String s2)
	{
		int dx = 0;
		String temp = null;
		if (s1.length() < s2.length())
		{
			temp = s1;
			s1 = s2;
			s2 = temp;
		}
		int length = s2.length();
		int dl = s1.length() - s2.length();
		char cc[] = new char[s1.length()];
		for (int i = length - 1; i >= 0; i--)
		{
			char c1 = s1.charAt(i + dl);
			char c2 = s2.charAt(i);
			int s = c1 - '0' + c2 - '0';
			if (dx != 0)
			{
				s += dx;
			}
			cc[i + dl] = (char) ((s % 10) + '0');
			dx = s / 10;
		}
		for (int i = s1.length() - length - 1; i >= 0; i--)
		{
			if (dx != 0)
			{
				int s = s1.charAt(i) - '0' + dx;
				cc[i] = (char) ((s % 10) + '0');
				dx = s / 10;
			}
			else
			{
				cc[i] = s1.charAt(i);
			}
		}
		if (dx != 0)
		{
			return (dx + "") + new String(cc);
		}
		return new String(cc);
	}

  

posted on 2017-01-21 00:15  好吧,就是菜菜  阅读(133)  评论(0编辑  收藏  举报