HDU 3925 Substring

http://acm.hdu.edu.cn/showproblem.php?pid=3925


给出a,b,问 :若sum=a+c,且b是sum的子串,那么 c 最小是多少

其中 a 很大(10^100)


一点一点模拟就可以了,用b从a的低位开始比较,然后b左移补零,依次扫描a的长度

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        BigInteger zero = BigInteger.valueOf(0);
        BigInteger ten = BigInteger.valueOf(10);
        int i,t,time=1;
        BigInteger a,b,min;
        t = cin.nextInt();
        while(t-->0){
            a=cin.nextBigInteger();
            b=cin.nextBigInteger();
            
            if(a.toString().indexOf(b.toString())!=-1){
            	min = zero;
            }
            else {
            	min = new BigInteger("999999999999999999999999999");
            	for(i=b.toString().length();i<=110;i++){
            		BigInteger temp1 = a.mod(ten.pow(i));
            		if(temp1.compareTo(b)==-1){
            			BigInteger temp2 = b.subtract(temp1);
            			if(temp2.compareTo(min)==-1){
            				min = temp2;
            			}
            		}
            		else if(temp1.compareTo(b)==0){
            			min = zero;break;
            		}
            		else {							//a>b的时候,可以考虑b前面补1,比如a=9,b=2
            			BigInteger bob = b.add(ten.pow(b.toString().length()));
            			BigInteger temp2 = bob.subtract(temp1);
            			if(temp2.compareTo(min)==-1 && temp2.compareTo(zero)!=-1){
            				min = temp2;
            			}
            		}
            		b = b.multiply(ten);
            	}
            }
            System.out.println("Case #"+time+": "+min);
            time++;
        }
    }
}


你可能感兴趣的:(c,String,Class,import)