zoukankan      html  css  js  c++  java
  • 10494,没过,待解决,大数除法

    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();
    	}
    
    }
    

      

  • 相关阅读:
    QAbstractItemModel使用样例与解析(Model::index使用了createIndex,它会被销毁吗?被销毁了,因为栈对象出了括号就会被销毁)
    更多的人为了追求自己真正热爱的事,甚至会在职业生涯刚开始时拒绝许多高薪工作,这样的人最终都成了真正的赢家。
    MYSQL分库分表之sharding-jdbc第四篇
    MYSQL分库分表之 Sharding-JDBC第三篇
    MySQL分库分表之Sharding-JDBC第二篇
    MySQL分库分表之Sharding-JDBC第一篇
    增加复杂度的12危险信号
    ASP.NET-Core-Web-API-Best-Practices-Guide
    聚合
    浏览器输入www.baidu.com后干啥了-web性能优化指南
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6336666.html
Copyright © 2011-2022 走看看