• 【南阳OJ分类之大数问题】题目+AC代码汇总


           声明:题目部分皆为南阳OJ题目,代码部分包含AC代码(可能不止一个)和标程。

           由于大数问题用c/c++写比较麻烦,而Java的大数类又很好用,所以基本为java代码。实际上竞赛很少会考大数问题,因为竞赛是比的算法,而不是语言特性,不过很多都是大数据,数据上千万级别的,所以算法又很关键,显然那和这篇博客也没啥关系。

          题目不是太难,大家和本人就权当学习或复习下Java吧O(∩_∩)O~。

          该分类南阳oj地址:http://acm.nyist.edu.cn/JudgeOnline/problemset.php?typeid=7 .
         本文由csdn-jtahstu原创,转载请注明出处,欢迎志同道合的朋友一起交流学习。本人QQ:1373758426和博客链接:blog.csdn.net/jtahstu

    ok , 开始Y(^o^)Y

    P28、

    大数阶乘

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:3
     
    描述
    我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?
     
    输入
    输入一个整数m(0<m<=5000)
    输出
    输出m的阶乘,并在输出结束之后输入一个换行符
    样例输入
    50
    样例输出
    30414093201713378043612608166064768844377641568960512000000000000
    
    来源
    经典题目
    上传者

    张云聪

     #include<stdio.h>
    int a[1000001];
    int main()
    {
        int n;
        scanf("%d",&n);
        int len=1;
        a[1]=1;
        for(int i=2; i<=n; ++i)
        {
            int b=0;
            for(int j=1; j<=len; ++j)
            {
                int t=a[j]*i+b;
                a[j]=t%10;
                b=t/10;
                if(j==len&&b!=0)
                    len++;
            }
        }
        for(int i=len; i>0; --i)
            printf("%d",a[i]);
        printf("
    ");
    }   

     

    #include<stdio.h>//标程
    #include<string.h>
    const int maxn=20000;
    int a[maxn];
    int main() {
        int n,i,j,s,c;
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        a[0]=1;
        for(i=2; i<=n; i++) {
            c=0;
            for(j=0; j<=maxn; j++) {
                s=a[j]*i+c;
                a[j]=s%10;
                c=s/10;
            }
        }
        for(j=maxn; j>=0; j--) if(a[j]) break;
        for(i=j; i>=0; i--) printf("%d",a[i]);
        printf("
    ");
        return 0;
    }

    P45、

    棋盘覆盖

    直接给链接吧,这题的图复制过来有问题,http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=45
    时间限制:3000 ms  |  内存限制:65535 KB
    难度:3
    描述

    在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的2×2方格(图2为其中缺右下角的一个),去覆盖2k×2k未被覆盖过的方格,求需要类似图2方格总的个数s。如k=1时,s=1;k=2时,s=2                                                                                    

    输入
    第一行m表示有m组测试数据;
    每一组测试数据的第一行有一个整数数k;
    输出
    输出所需个数s;
    样例输入
    3123
    样例输出
    1521

     

     /*
     * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=45
     * by jtahstu on 2015/3/31 20:00
     * (2^(2*k))/3 就是这个结果
     */
    import java.math.BigInteger;
    import java.util.Scanner;
    public class Main {
    	public static Scanner cin = new Scanner(System.in);
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		int T = cin.nextInt();
    		while (T-- != 0) {
    			int k = cin.nextInt();
    			BigInteger ans=BigInteger.valueOf(2);
    			System.out.println(ans.pow(2*k).divide(BigInteger.valueOf(3)));
    
    		}
    	}
    }

     

     #include<iostream>//标程
    #include<string.h>
    using namespace std;
    int main()
    {
    	int n;
    	cin>>n;
    	while(n--)
    	{
    		int a[100];
    		memset(a,0,sizeof(a));
    		int size;
    		cin>>size;
    		a[0]=1;
    		if(size==1)
    			cout<<a[0]<<endl;
    		int i,j,k;
    		for(i=2;i<=size;++i)
    		{
    			for(j=0;j<100;++j)
    				a[j]=4*a[j];
    			a[0]++;
    			for(j=0;j<99;++j)
    			{
    				a[j+1]+=a[j]/10;
    				a[j]=a[j]%10;
    			}
    		}
    		for(i=99;i>=0;--i)
    			if(a[i]) break;
    		for(j=i;j>=0;--j)
    			cout<<a[j];
    		cout<<endl;
    	}
    	return 0;
    }        

    P73、

    比大小

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:2
     
    描述

    给你两个很大的数,你能不能判断出他们两个数的大小呢?

    比如123456789123456789要大于-123456

     
    输入
    每组测试数据占一行,输入两个不超过1000位的10进制整数a,b
    数据保证输入的a,b没有前缀的0。
    如果输入0 0表示输入结束。测试数据组数不超过10组
    输出
    如果a>b则输出“a>b”,如果a<b则输出“a<b”,如果相等则输出“a==b”。
    样例输入
    111111111111111111111111111 88888888888888888888
    -1111111111111111111111111  22222222
    0 0
    样例输出
    a>b
    a<b
     #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string s1, s2;
        while (cin >> s1 >> s2,s1[0] != '0' && s2[0] != '0')
        {
            if (s1[0] == '-' && s2[0] != '-')
            {
                cout << "a<b" << endl;
            }
            else if (s2[0] == '-' && s1[0] != '-')
            {
                cout << "a>b" << endl;
            }
            else
            {
                if (s1[0] == '-' && s2[0] == '-')   //为负数
                {
                    if (s1.size() > s2.size())
                        cout << "a<b" << endl;
                    else if (s1.size() < s2.size())
                        cout << "a>b" << endl;
                    else if(s1 > s2) cout<<"a<b"<<endl;
                    else if(s1 < s2) cout<<"a>b"<<endl;
                    else cout << "a==b" << endl;
                }
                else   //为正数
                {
                    if (s1.size() > s2.size())
                        cout << "a>b" << endl;
                    else if (s1.size() < s2.size())
                        cout << "a<b" << endl;
                    else if(s1 > s2) cout<< "a>b" <<endl;
                    else if(s1 < s2) cout<< "a<b" <<endl;
                    else cout << "a==b" << endl;
                }
            }
        }
    }//Orz AC

     

     #include<iostream>//标程
    #include<string>
    using namespace std;
    int main()
    {
    	string a,b;	
    	while(cin>>a>>b)
    	{
    		if(a=="0"&&b=="0")
    			return 0;
    		if(a==b)
    			cout<<"a==b"<<endl;
    		else if(a[0]=='-'&&b[0]=='-')
    			{
    				if(a.substr(1,string::npos)>b.substr(1,string::npos)||a.length()>b.length())
    					cout<<"a<b"<<endl;
    				else cout<<"a>b"<<endl;
    			}
    		else if(a>"0"&&b>"0"||a<"0"&&b<"0"&&a.length()>b.length()||a>b)
    				cout<<"a>b"<<endl;
    		else if(a<"0"&&b>"0"&&a.length()>b.length()||a>b)
    				cout<<"a<b"<<endl;	
    	}
    }        

    P103

    A+B Problem II

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:3
     
    描述

    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

    A,B must be positive.

     
    输入
    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
    输出
    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the anequation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation.
    样例输入
    2
    1 2
    112233445566778899 998877665544332211
    样例输出
    Case 1:
    1 + 2 = 3
    Case 2:
    112233445566778899 + 998877665544332211 = 1111111111111111110
     /*
     * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=103
     * by jtahstu on 2015/2/12 20:00
     * hdu 1002
     */
    #include<iostream>
    #include<string>
    #include<stdio.h>
    using namespace std;
    int main() {
        int n,count=0,m;
        string a1,b1;
        cin>>n;m=n;
        while(n--) {
            int a[1001]= {0},b[1001]= {0};
            count++;
            cin>>a1>>b1;
            for(int i=0; i<a1.size(); i++)//大数相加算法
                a[i]+=a1[a1.size()-i-1]-'0';
            for(int i=0; i<b1.size(); i++)
                b[i]+=b1[b1.size()-i-1]-'0';
            for( int i = 0 ; i < 1001; i++ ) {
                a[i] += b[i] ;
                if( a[i] >= 10 ) {
                    a[i+1] += a[i]/10 ;
                    a[i]%=10;
                }
            }
            int i;
            for(i = 1000 ; i >= 0; i -- ) {
                if( a[i] != 0 )break;
            }
            cout<<"Case "<<count<<":"<<endl;
            cout<<a1<<" "<<"+"<<" "<<b1<<" "<<"="<<" ";
            for(; i>=0; i--)
                cout<<a[i];
            cout<<endl;
    //    if(count!=m)//最后一行不能多一个换行,bt
    //        cout<<endl;
        }
        return 0;
    }
    

     

     import java.math.BigInteger;//标程
    import java.util.Scanner;
    public class Main{
        public static void main(String args[]) {
           Scanner cin=new Scanner(System.in);
           int n=cin.nextInt();
           BigInteger a,b;
           for(int i=1;i<=n;i++){
        	   a=cin.nextBigInteger();
        	   b=cin.nextBigInteger();
        	   System.out.println("Case "+i+":");
        	   System.out.println(a.toString()+" + "+b.toString()+" = "+a.add(b));
           }
        }
    }        

    P114

    某种序列

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:4
     
    描述
    数列A满足An = An-1 + An-2 + An-3, n >= 3 
    编写程序,给定A0, A1 和 A2, 计算A99
     
    输入
    输入包含多行数据 
    每行数据包含3个整数A0, A1, A2 (0 <= A0, A1, A2 <= 100000000) 
    数据以EOF结束
    输出
    对于输入的每一行输出A99的值
    样例输入
    1 1 1
    样例输出
    69087442470169316923566147
     /*
     * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=803
     * by jtahstu on 2015/3/31 20:00
     */
    import java.math.BigInteger;
    import java.util.Scanner;
    public class Main {
    	public static Scanner cin = new Scanner(System.in);
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		BigInteger res[] = new BigInteger[105];
    		while (cin.hasNext()) {
    			res[0] = cin.nextBigInteger();
    			res[1] = cin.nextBigInteger();
    			res[2] = cin.nextBigInteger();
    			for (int i = 3; i <= 99; i++) {
    				res[i] = res[i - 1].add(res[i - 2]).add(res[i - 3]);
    			}
    			System.out.println(res[99]);
    		}
    	}
    }       
     #include<stdio.h>//标程
    #include<string.h>
    #include <stdlib.h>
    void add(char a[],char b[],char back[])
    {
    	int i,j,k,up,x,y,z,l;
    	char *c;
    	if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2;
    	c=(char *) malloc(l*sizeof(char));
    	i=strlen(a)-1;
    	j=strlen(b)-1;
    	k=0;up=0;
    	while(i>=0||j>=0)
    	{
    		if(i<0) x='0'; else x=a[i];
    		if(j<0) y='0'; else y=b[j];
    		z=x-'0'+y-'0';
    		if(up) z+=1;
    		if(z>9) {up=1;z%=10;} else up=0;
    		c[k++]=z+'0';
    		i--;j--;
    	}
    	if(up) c[k++]='1';
    	i=0;
    	c[k]='';
    	for(k-=1;k>=0;k--)
    		back[i++]=c[k];
    	back[i]='';
    } 
    int main()
    {
    	char a[3][1000],temp[1000];
    	int roll=3;
    	while(scanf("%s%s%s",a[0],a[1],a[2]) !=EOF)
    	{
    		for (int i=3;i<=99;i++)
    		{
    			add(a[(i-1)%3],a[(i-2)%3],temp);
    			add(temp,a[(i-3)%3],a[i%3]);
    		}
    		printf("%s
    ",a[0]);
    	}
    }        

    P155

    求高精度幂

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:2
    描述

    对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。 

    现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < =n <= 25。

    输入
    输入有多行,每行有两个数R和n,空格分开。R的数字位数不超过10位。
    输出
    对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点。
    样例输入
    95.123 120.4321 205.1234 156.7592  998.999 101.0100 12
    样例输出
    548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
     /*
     * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=155
     * by jtahstu on 2015/3/31 20:00
     */
    import java.math.BigDecimal;
    import java.util.Scanner;
    public class Main {
    	public static Scanner cin = new Scanner(System.in);
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		BigDecimal rBigDecimal;
    		int n;
    		while (cin.hasNext()) {
    			rBigDecimal = cin.nextBigDecimal();
    			n = cin.nextInt();
    			/*
    			 * BigDecimal ans=BigDecimal.ONE; for (int i = 0; i <n; i++) {
    			 * ans=ans.multiply(rBigDecimal); }
    			 * System.out.println(ans.stripTrailingZeros().toPlainString());
    			 */
    			String str = rBigDecimal.pow(n).stripTrailingZeros()
    					.toPlainString();
    			if (str.startsWith("0"))//需要删除小数点前面的0,我去
    				str = str.substring(1);
    			System.out.println(str);
    		}
    	}
    
    } 

     

     import java.util.Scanner;//标程
    import java.math.BigDecimal;
    /**
     *
     * @author Administrator
     */
    public class Main {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            BigDecimal d1=null;
            int p;
            String s1;
            String s;
            Scanner cin=new Scanner(System.in);
            while(cin.hasNext()){
               s1=cin.next();
               p=cin.nextInt();
               d1=new BigDecimal(s1);
               s=d1.pow(p).stripTrailingZeros().toPlainString();
               for(int i=0;i<s.length();i++){
                    if(i==0&&s.charAt(i)=='0')
                        continue;
                    System.out.print(s.charAt(i));
               }
               System.out.println();
            }
        }
    }        

    P513

    A+B Problem IV

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
     
    描述
    acmj最近发现在使用计算器计算高精度的大数加法时很不方便,于是他想着能不能写个程序把这个问题给解决了。
     
    输入
    包含多组测试数据
    每组数据包含两个正数A,B(可能为小数且位数不大于400)
    输出
    每组输出数据占一行,输出A+B的结果,结果需要是最简的形式。
    样例输入
    1.9 0.1
    0.1 0.9
    1.23 2.1
    3 4.0
    样例输出
    2
    1
    3.33
    7
    <pre name="code" class="java"> /*
     * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=513
     * by jtahstu on 2015/3/31 19:00
     */
    import java.math.BigDecimal;
    import java.util.Scanner;
    public class Main {
    	public static Scanner cin = new Scanner(System.in);
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		BigDecimal abigDecimal, bbigDecimal;
    		while (cin.hasNext()) {
    			abigDecimal = cin.nextBigDecimal();
    			bbigDecimal = cin.nextBigDecimal();
    			if (abigDecimal.add(bbigDecimal).compareTo(BigDecimal.ZERO) == 0)
    				System.out.println("0");
    			else
    				System.out.println(abigDecimal.add(bbigDecimal)
    						.stripTrailingZeros().toPlainString());
    		}
    	}
    }        
    
    
     #include <stdio.h>//标程
    #include <string.h>
    #define MAX 1000
    char a[MAX],b[MAX],c[MAX];
    int main()
    {
    	int i,j,k,l,m,n,la,lb,mx;
    	char ch;
    	memset(a,'0',sizeof(a));
    	memset(b,'0',sizeof(b));
    	while(~scanf("%s%s",a,b))
    	{
    		la=strlen(a);
    		lb=strlen(b);
    		a[la]='0';
    		b[lb]='0';
    		i=0;
    		while((a[i]-'.')&&i<la)
    		{
    			i++;
    		}
    		if(i==la)
    			a[la]='.';
    		j=0;
    		while((b[j]-'.')&&j<lb)
    		{
    			j++;
    		}
    		if(j==lb)
    			b[lb]='.';
    		m=(la-i)>(lb-j)?(la-i):(lb-j);
    		if(i>=j)
    		{
    			for(l=mx=i+m,k=0,j=i-j;l>=0;l--)
    			{
    				if(a[l]=='.')
    				{
    					c[l]='.';
    					continue;
    				}
    				ch=(l-j)<0?'0':b[l-j];
    				c[l]=(a[l]-'0'+ch-'0'+k)%10+'0';
    				k=(a[l]-'0'+ch-'0'+k)/10;
    			}
    		}
    		else
    		{
    			for(l=mx=j+m,k=0,j=j-i;l>=0;l--)
    			{
    				if(b[l]=='.')
    				{
    					c[l]='.';
    					continue;
    				}
    				ch=(l-j)<0?'0':a[l-j];
    				c[l]=(ch-'0'+b[l]-'0'+k)%10+'0';
    				k=(ch-'0'+b[l]-'0'+k)/10;
    			}
    		}
    		if(k>0)
    			printf("%d",k);
    		while(c[mx]=='0')
    			mx--;
    		if(c[mx]=='.')
    			mx--;
    		for(i=0;i<=mx;i++)
    			printf("%c",c[i]);
    		printf("
    ");
    		memset(a,'0',sizeof(a));
    		memset(b,'0',sizeof(b));
    	}
    }                

    P517

    最小公倍数

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
     
    描述
    为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致。
    但也并非纯粹的偶然:60是个优秀的数字,它的因子比较多。
    事实上,它是1至6的每个数字的倍数。即1,2,3,4,5,6都是可以除尽60。
     
    我们希望寻找到能除尽1至n的的每个数字的最小整数m.
     
    输入
    多组测试数据(少于500组)。
    每行只有一个数n(1<=n<=100).
    输出
    输出相应的m。
    样例输入
    2
    3
    4
    
    样例输出
    2
    6
    12
     /*
     * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=517
     * by jtahstu on 2015/3/31 20:00
     */
    import java.math.BigInteger;
    import java.util.Scanner;
    public class Main {
    	static BigInteger jt(BigInteger a,BigInteger b)
    	{
    		return (b.compareTo(BigInteger.valueOf(0))==0)?a:jt(b, a.mod(b));
    	}
    	static BigInteger tt(BigInteger a,BigInteger b)
    	{
    		return a.multiply(b).divide(jt(a, b));
    	}
    	public static Scanner cin = new Scanner(System.in);
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		BigInteger res[]=new BigInteger[105];
    		int n;
    		res[1]=BigInteger.valueOf(1);
    		for(int i=2;i<=100;i++){
    			res[i]=tt(BigInteger.valueOf(i), res[i-1]);
    		}
    		while(cin.hasNext()){
    			n=cin.nextInt();
    			System.out.println(res[n]);
    		}
    	}
    }      
     #include <math.h>//标程
    #include <stdio.h>
    #include <string.h>
    const int M = 120;
    bool not_prime[M];
    int prime[30], point[30];
    struct Ac
    {
        int num[50];
        int len;
    }ans;
    void ac_table()
    {
        int top = -1;
        for(int i=2;i<M;i++)
            if(not_prime[i] == false)
            {
                prime[++top]=i;
                for(int j=2*i;j<M;j+=i)
                    not_prime[j] = true;
            }
    }
    void Init()
    {
        memset(point,0,sizeof(point));
        memset(ans.num,0,sizeof(ans.num));
        ans.num[0] = 1;
        ans.len = 0;
    }
    void my_pow(Ac &c,int x)
    {
        for(int i=0;i<=c.len;i++)
            c.num[i] *= x;
        for(int i=0;i<=c.len;i++)
            if(c.num[i] >= 10000)
            {
                c.num[i+1] += c.num[i]/10000;
                c.num[i]    = c.num[i]%10000;
            }
        c.len = c.num[c.len+1]==0 ? c.len : c.len+1;
    }
    void Print(Ac &c)
    {
        for(int i=c.len;i>=0;i--)
            printf(i==c.len? "%d" : "%04d",c.num[i]);
        printf("
    ");
    }
    void divide(int x)
    {
        for(int i=0;prime[i]<=x;i++)
        {
            int tmp = 0;
            while(x%prime[i]==0)
            {
                tmp++;
                x /= prime[i];
            }
            if(tmp > point[i])
            {
                my_pow(ans,pow(prime[i] ,tmp - point[i]));
                point[i] = tmp;
            }
        }
    }
    int main()
    {
        int n;
        ac_table();
        while(~scanf("%d",&n))
        {
            Init();
            for(int i=2;i<=n;i++)
                divide(i);
            Print(ans);
        }
        return 0;
    }       

    P524

    A-B Problem

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
     
    描述

    A+B问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下A-B吧。

    现在有两个实数A和B,聪明的你,能不能判断出A-B的值是否等于0呢?

     
    输入
    有多组测试数据。每组数据包括两行,分别代表A和B。
    它们的位数小于100,且每个数字前中可能包含+,- 号。
    每个数字前面和后面都可能有多余的0。
    每组测试数据后有一空行。
    输出
    对于每组数据,输出一行。
    如果A-B=0,输出YES,否则输出NO。
    样例输入
    1
    1
    
    1.0
    2.0
    
    样例输出
    YES
    NO
     /*
     * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=524
     * by jtahstu on 2015/3/31 20:00
     */
    import java.math.BigDecimal;
    import java.util.Scanner;
    
    public class Main {
    	public static Scanner cin = new Scanner(System.in);
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		BigDecimal a,b;
    		while(cin.hasNext()){
    			a=cin.nextBigDecimal();
    			b=cin.nextBigDecimal();
    			//cin.nextLine();//这里留意下
    			if(a.compareTo(b)==0)
    				System.out.println("YES");
    			else {
    				System.out.println("NO");
    			}
    		}
    	}
    
    }

     

    #include <stdio.h>//标程
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    const int M=100;
    void back(char* c,int &l)
    {
        if(strchr(c,'.') == NULL)
            return ;
        for(int i=l-1;i>=1;i--)
        {
            if(c[i]!='0')
                break;
            l--;
        }
        if(c[l-1]=='.')
            l--;
        c[l]='';
        //puts("after back->");
        //puts(c);
    }
    void front(char* c,int &l,bool &f)
    {
        int cnt = isdigit(c[0]) ? 0 : 1;
        if(c[0]=='-')
            f = false;
        for(int i=cnt;i<l-1;i++)
        {
            if(c[i+1]=='.' || c[i]!='0')
                break;
            cnt++;
        }
        if(cnt)
        {
            for(int i=0;i<l-cnt;i++)
                c[i] = c[i+cnt];
            c[l-cnt] = '';
        }
        //puts("after front->");
        //printf(f?"+ ":"- ");
        //puts(c);
    }
    void deal(char *c,bool &f)
    {
        int l = strlen(c);
        f = true;
        back(c,l);
        front(c,l,f);
    }
    bool Cmp(char* A,char* B,bool a,bool b)
    {
        if(strcmp(A,B) == 0)
        {
            if(strcmp(A,"0") == 0 || a^b == 0)
                return true;
        }
        return false;
    }
    int main()
    {
        char A[M],B[M];
        bool a,b;
        while(~scanf("%s%s",A,B))
        {
            deal(A,a);
            deal(B,b);
            puts(Cmp(A,B,a,b)?"YES":"NO");
        }
    
        return 0;
    }        

    P655

    光棍的yy

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:2
     
    描述

    yy经常遇见一个奇怪的事情,每当他看时间的时候总会看见11:11,这个很纠结啊

    现在给你m个1,你可以把2个1组合成一个2,这样就不是光棍了,问这样的组合有多少种??

    例如(111  可以拆分为 111 12 21  有三种)

     
    输入
    第一行输入一个n表示有n个测试数据
    以下n行,每行输入m个1
    (1 <= n,m <= 200)
    输出
    输出这种组合种数,占一行
    样例输入
    3
    11
    111
    11111
    样例输出
    2
    3
    8
     
    import java.math.BigInteger;
    import java.util.Scanner;
    public class Main {
    	public static Scanner cin = new Scanner(System.in);
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		int n = cin.nextInt();
    		BigInteger a[] = new BigInteger[201];
    		a[1] = BigInteger.valueOf(1);
    		a[2] = BigInteger.valueOf(2);
    		for (int i = 3; i <= 200; i++)
    			a[i] = a[i - 1].add(a[i - 2]);//递推,只是这是大数
    		while (n-- > 0) {
    			String s;
    			s = cin.next();
    			int len = s.length();
    			System.out.println(a[len]);
    		}
    	}
    }        


    P773

    开方数

    时间限制:500 ms  |  内存限制:65535 KB
    难度:3
     
    描述
    现在给你两个数 n 和 p ,让你求出 p 的开 n 次方。
     
    输入
    每组数据包含两个数n和p。当n和p都为0时表示输入结束。(1<=n<=200,1<=p<=10^101)
    输出
    对于每个输出对用输出开方后的结果k(结果小于10^9)。
    样例输入
    2 16
    3 27
    7 4357186184021382204544
    0 0
    
    样例输出
    4
    3
    1234
     
    import java.util.Scanner;
    
    public class Main {
    	public static Scanner input = new Scanner(System.in);
    	public static void main(String[] args) {
    		while (true) {
    			int n = input.nextInt();
    			double p = input.nextDouble();
    			if (n == 0 && p == 0)
    				break;
    			System.out.println(String.format("%.0f", Math.pow(p, 1.0 / n)));
    		}
    	}
    }
            

     

     #include <stdio.h>//标程
    #include <string.h>
    #include <math.h>
    int main()
    {
        //freopen("Input.txt","r",stdin);
        //freopen("Output1.txt","w",stdout);
        long mid,left,right;
        double p;
        int n;
        while(~scanf("%d%lf",&n,&p)){
            if(n==0 && p==0.0) break;
            left=0,right=1000000000;
            while(left<right)
            {
                mid=(left+right)/2;
                if(pow(mid,n)==p) break;
                if(pow(mid,n)<p)left=mid;
                if(pow(mid,n)>p)
                    right=mid;
            }
            //printf("%lf
    ",p);
            printf("%ld
    ",mid);
        }
        return 0;
    }
            


    P803

    A/B Problem

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
     
    描述

     做了A+B Problem,A/B Problem不是什么问题了吧!

     
    输入
    每组测试样例一行,首先一个号码A,中间一个或多个空格,然后一个符号( / 或者 % ),然后又是空格,后面又是一个号码B,A可能会很长,B是一个int范围的数。
    输出
    输出结果。
    样例输入
    110 / 100
    99 % 10
    2147483647 / 2147483647
    2147483646 % 2147483647
    样例输出
    1
    9
    1
    2147483646

     

     /*
     * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=803
     * by jtahstu on 2015/3/31 20:00
     */
    
    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
    	public static Scanner cin = new Scanner(System.in);
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		String string;
    		BigInteger a,b;
    		while(cin.hasNext()){
    			a=cin.nextBigInteger();
    			string=cin.next();
    			b=cin.nextBigInteger();
    			if(string.compareTo("/")==0)
    				System.out.println(a.divide(b));
    			else 
    				System.out.println(a.mod(b));
    			
    		}
    	}
    }
            

      大数分类里就这12道题,Java复习的怎么样了啊?接下来应该会写分类的STL练习,容我先把题A完再来写,这几天应该就可以了。(*^-^*)

  • 相关阅读:
    mybatis系列-04-mybatis开发dao的方法
    mybatis系列-03-入门程序
    mybatis系列-02-mybatis框架
    mybatis系列-01-JDBC
    对代码的理解
    jenkins api调用
    lcov收集覆盖率
    Spring MVC @ModelAttribute
    shell文件/路径处理
    gcc static静态编译选项提示错误修正(/usr/lib/ld: cannot find -lc)
  • 原文地址:https://www.cnblogs.com/jtahstu/p/4278330.html
Copyright © 2020-2023  润新知