辅助信息记录(快速读入等)

1 快速读入

2.1 C++的数字快速读入

inline int read()    
{    
    char ch;  
    bool flag = false;  
    int a = 0;    
    while(true) {
            ch = getchar();
            if(ch >= '0' && ch <= '9' || ch == '-') {
                    break;
            }
    }
    (ch == '-') ? (flag = true) : (a = a * 10 + ch - '0');
    while(true) {  
            ch = getchar(); 
            if(ch >= '0' && ch <= '9') {
                    a = a * 10 + ch - '0';
            } else {
                    break;
            }
    }     
    if(flag) {  
            a = -a;  
    }  
    return a;    
}  

2.2 java的快速读入

import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AC solver = new AC();
        solver.solve(in, out);
        //只有在测试用例输出完全后才会输出
        out.close();
    }
}

class AC {
	InputReader in;
	PrintWriter out;
	int prime[]=new int[1000];
	int tot;
	boolean check[]=new boolean[2020];
	void init() {           //现行筛素数的方法,时间复杂度为O(n)  
	    Arrays.fill(check,false);
	    int i,j;  
	    tot=0;  
	    for(i=2;i<2000;i++)  {  
	    	//out.println(i);
	        if(!check[i])prime[tot++]=i;  
	        for(j=0;j<tot;j++)  {  
	            if(i*prime[j]>2000)break;  
	            check[i*prime[j]]=true;  
	            if(i%prime[j]==0)break;  
	        }  
	    }  
	    //for(i=0;i<tot;i++)out.println(prime[i]);
	} 
	public void work(){
		int T;
		T=in.nextInt();
		while(T!=0){
			T--;
			BigInteger a,b=BigInteger.ONE;
			int i=0;
			a=in.nextBigInteger();
			for(i=0;i<tot;i++)
			{
				b=b.multiply(BigInteger.valueOf(prime[i]));
				if(b.compareTo(a)>0)break;
			}
			b=b.divide(BigInteger.valueOf(prime[i]));
			out.println(b);
		}
	}
    public void solve(InputReader in, PrintWriter out) {
         this.in = in; this.out = out;
         //ubuntu(乌班图操作系统) 下以ctrl + d终止 while(in.hasNext())
         init();
         work();
    }
}
class InputReader {
    BufferedReader reader;
    StringTokenizer tokenizer;
    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        if (!hasNext())
            throw new RuntimeException();
        return tokenizer.nextToken();
    }

    boolean hasNext() {
        while (tokenizer == null || !tokenizer.hasMoreTokens())
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (Exception e) {
                return false;
            }
        return true;
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }
    public BigInteger nextBigInteger() {
    	return new BigInteger(next());
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值