博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
10494,没过,待解决,大数除法
阅读量:6250 次
发布时间:2019-06-22

本文共 2680 字,大约阅读时间需要 8 分钟。

import java.io.*;import java.util.*;public class Main{	public static void main(String[] args) throws FileNotFoundException	{		// Scanner scanner = new Scanner(new File("d://1.txt"));		Scanner scanner = new Scanner(System.in);		while (scanner.hasNextLine())		{			String str1 = scanner.nextLine();			String str2 = scanner.nextLine();			System.out.println(sub(str1, str2));		}		scanner.close();	}	public static String sub(String str1, String str2)	{		int minLength = -1;		int maxLength = -1;		if (str1.length() > str2.length())		{			minLength = str2.length();			maxLength = str1.length();		}		else		{			minLength = str1.length();			maxLength = str2.length();			String temp = str1;			str1 = str2;			str2 = temp;		}		// 俩个数相减后剩下的最大长度		char cc[] = new char[maxLength];		// 大数减小数,好像负号和减法并没有关系		int dx = 0;		int cIndex = 0;		int dl = maxLength - minLength;		for (int i = minLength - 1; i >= 0; i--)		{			int s = str1.charAt(i + dl) - str2.charAt(i) - dx;			dx = 0;			if (s < 0)			{				dx = 1;				s += 10;			}			cc[cIndex++] = (char) (s + '0');		}		for (int i = maxLength - cIndex-1; i >= 0; i--)		{			int s = str1.charAt(i) - '0' - dx;			dx = 0;			if (s < 0)			{				dx = 1;				s += 10;			}			cc[cIndex++] = (char) (s + '0');		}		if(cc[cIndex - 1] == '0')		{			return "0";		}		StringBuilder sb = new StringBuilder();		for (int i = cIndex - 1; i >= 0; i--)		{			sb.append(cc[i]);		}		return sb.toString();	}	public static String mul(String str1, String str2)	{		int minLength = -1;		int maxLength = -1;		if (str1.length() > str2.length())		{			minLength = str2.length();			maxLength = str1.length();		}		else		{			minLength = str1.length();			maxLength = str2.length();			String temp = str1;			str1 = str2;			str2 = temp;		}		int[] cc = new int[maxLength + minLength];		int maxIndex = -1;		for (int i = minLength - 1; i >= 0; i--)		{			char c2 = str2.charAt(i);			int cIndex = minLength - 1 - i;			int dx = 0;			for (int j = maxLength - 1; j >= 0; j--)			{				cc[cIndex] = (str1.charAt(j) - '0') * (c2 - '0') + dx						+ cc[cIndex];				dx = cc[cIndex] / 10;				cc[cIndex] = cc[cIndex] % 10;				cIndex++;			}			if (maxIndex < cIndex)			{				maxIndex = cIndex;			}			cIndex = maxIndex;			if (dx != 0)			{				while (dx != 0)				{					cc[cIndex] = cc[cIndex] + dx;					dx = cc[cIndex] / 10;					cc[cIndex] = cc[cIndex] % 10;					cIndex++;				}				maxIndex = cIndex;			}		}		StringBuilder sb = new StringBuilder();		for (int i = maxIndex - 1; i >= 0; i--)		{			if (cc[i] == 0 && i == maxIndex - 1)			{				return "0";			}			sb.append((char) (cc[i] + '0'));		}		return sb.toString();	}}

  

posted on
2017-01-21 11:13 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6336666.html

你可能感兴趣的文章
第十一周项目0-是春哥啊
查看>>
poi做一个简单的EXCAL
查看>>
几种查询emacs帮助的办法
查看>>
Python_基础_(模块,time,random,os,sys,json,shelve,xml,序列化反序列化)
查看>>
异常:Project configuration is not up-to-date with pom.xml解决方案
查看>>
HDU2647 拓扑排序
查看>>
ThinkPHP/---微信支付PC流程
查看>>
JavaScript 05
查看>>
python 多线程编程之threading模块(Thread类)创建线程的三种方法
查看>>
实验三
查看>>
水仙花数
查看>>
P3308 [SDOI2014]LIS(最小割+退流)
查看>>
C语言作业--数据类型
查看>>
压位高精
查看>>
jsp 中对jar 包的引用
查看>>
python操作mysql数据库
查看>>
Yii: gii 403 Error you are not allowed to access this page
查看>>
计算汉字长度
查看>>
Codeforces 911E - Stack Sorting
查看>>
BZOJ 1853: [Scoi2010]幸运数字
查看>>