声明:题目部分皆为南阳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的阶乘,并在输出结束之后输入一个换行符
样例输入
  1. 50
样例输出
  1. 30414093201713378043612608166064768844377641568960512000000000000
来源
经典题目
上传者

张云聪

  1. #include<stdio.h>
  2. int a[1000001];
  3. int main()
  4. {
  5. int n;
  6. scanf("%d",&n);
  7. int len=1;
  8. a[1]=1;
  9. for(int i=2; i<=n; ++i)
  10. {
  11. int b=0;
  12. for(int j=1; j<=len; ++j)
  13. {
  14. int t=a[j]*i+b;
  15. a[j]=t%10;
  16. b=t/10;
  17. if(j==len&&b!=0)
  18. len++;
  19. }
  20. }
  21. for(int i=len; i>0; --i)
  22. printf("%d",a[i]);
  23. printf("\n");
  24. }
  1. #include<stdio.h>//标程
  2. #include<string.h>
  3. const int maxn=20000;
  4. int a[maxn];
  5. int main() {
  6. int n,i,j,s,c;
  7. scanf("%d",&n);
  8. memset(a,0,sizeof(a));
  9. a[0]=1;
  10. for(i=2; i<=n; i++) {
  11. c=0;
  12. for(j=0; j<=maxn; j++) {
  13. s=a[j]*i+c;
  14. a[j]=s%10;
  15. c=s/10;
  16. }
  17. }
  18. for(j=maxn; j>=0; j--) if(a[j]) break;
  19. for(i=j; i>=0; i--) printf("%d",a[i]);
  20. printf("\n");
  21. return 0;
  22. }

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;
样例输入
  1. 3123
样例输出
  1. 1521
  1. /*
  2. * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=45
  3. * by jtahstu on 2015/3/31 20:00
  4. * (2^(2*k))/3 就是这个结果
  5. */
  6. import java.math.BigInteger;
  7. import java.util.Scanner;
  8. public class Main {
  9. public static Scanner cin = new Scanner(System.in);
  10. public static void main(String[] args) {
  11. // TODO 自动生成的方法存根
  12. int T = cin.nextInt();
  13. while (T-- != 0) {
  14. int k = cin.nextInt();
  15. BigInteger ans=BigInteger.valueOf(2);
  16. System.out.println(ans.pow(2*k).divide(BigInteger.valueOf(3)));
  17.  
  18. }
  19. }
  20. }
  1. #include<iostream>//标程
  2. #include<string.h>
  3. using namespace std;
  4. int main()
  5. {
  6. int n;
  7. cin>>n;
  8. while(n--)
  9. {
  10. int a[100];
  11. memset(a,0,sizeof(a));
  12. int size;
  13. cin>>size;
  14. a[0]=1;
  15. if(size==1)
  16. cout<<a[0]<<endl;
  17. int i,j,k;
  18. for(i=2;i<=size;++i)
  19. {
  20. for(j=0;j<100;++j)
  21. a[j]=4*a[j];
  22. a[0]++;
  23. for(j=0;j<99;++j)
  24. {
  25. a[j+1]+=a[j]/10;
  26. a[j]=a[j]%10;
  27. }
  28. }
  29. for(i=99;i>=0;--i)
  30. if(a[i]) break;
  31. for(j=i;j>=0;--j)
  32. cout<<a[j];
  33. cout<<endl;
  34. }
  35. return 0;
  36. }

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”。
样例输入
  1. 111111111111111111111111111 88888888888888888888
  2. -1111111111111111111111111 22222222
  3. 0 0
样例输出
  1. a>b
  2. a<b
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s1, s2;
  7. while (cin >> s1 >> s2,s1[0] != '0' && s2[0] != '0')
  8. {
  9. if (s1[0] == '-' && s2[0] != '-')
  10. {
  11. cout << "a<b" << endl;
  12. }
  13. else if (s2[0] == '-' && s1[0] != '-')
  14. {
  15. cout << "a>b" << endl;
  16. }
  17. else
  18. {
  19. if (s1[0] == '-' && s2[0] == '-') //为负数
  20. {
  21. if (s1.size() > s2.size())
  22. cout << "a<b" << endl;
  23. else if (s1.size() < s2.size())
  24. cout << "a>b" << endl;
  25. else if(s1 > s2) cout<<"a<b"<<endl;
  26. else if(s1 < s2) cout<<"a>b"<<endl;
  27. else cout << "a==b" << endl;
  28. }
  29. else //为正数
  30. {
  31. if (s1.size() > s2.size())
  32. cout << "a>b" << endl;
  33. else if (s1.size() < s2.size())
  34. cout << "a<b" << endl;
  35. else if(s1 > s2) cout<< "a>b" <<endl;
  36. else if(s1 < s2) cout<< "a<b" <<endl;
  37. else cout << "a==b" << endl;
  38. }
  39. }
  40. }
  41. }//Orz AC
  1. #include<iostream>//标程
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string a,b;
  7. while(cin>>a>>b)
  8. {
  9. if(a=="0"&&b=="0")
  10. return 0;
  11. if(a==b)
  12. cout<<"a==b"<<endl;
  13. else if(a[0]=='-'&&b[0]=='-')
  14. {
  15. if(a.substr(1,string::npos)>b.substr(1,string::npos)||a.length()>b.length())
  16. cout<<"a<b"<<endl;
  17. else cout<<"a>b"<<endl;
  18. }
  19. else if(a>"0"&&b>"0"||a<"0"&&b<"0"&&a.length()>b.length()||a>b)
  20. cout<<"a>b"<<endl;
  21. else if(a<"0"&&b>"0"&&a.length()>b.length()||a>b)
  22. cout<<"a<b"<<endl;
  23. }
  24. }

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.
样例输入
  1. 2
  2. 1 2
  3. 112233445566778899 998877665544332211
样例输出
  1. Case 1:
  2. 1 + 2 = 3
  3. Case 2:
  4. 112233445566778899 + 998877665544332211 = 1111111111111111110
  1. /*
  2. * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=103
  3. * by jtahstu on 2015/2/12 20:00
  4. * hdu 1002
  5. */
  6. #include<iostream>
  7. #include<string>
  8. #include<stdio.h>
  9. using namespace std;
  10. int main() {
  11. int n,count=0,m;
  12. string a1,b1;
  13. cin>>n;m=n;
  14. while(n--) {
  15. int a[1001]= {0},b[1001]= {0};
  16. count++;
  17. cin>>a1>>b1;
  18. for(int i=0; i<a1.size(); i++)//大数相加算法
  19. a[i]+=a1[a1.size()-i-1]-'0';
  20. for(int i=0; i<b1.size(); i++)
  21. b[i]+=b1[b1.size()-i-1]-'0';
  22. for( int i = 0 ; i < 1001; i++ ) {
  23. a[i] += b[i] ;
  24. if( a[i] >= 10 ) {
  25. a[i+1] += a[i]/10 ;
  26. a[i]%=10;
  27. }
  28. }
  29. int i;
  30. for(i = 1000 ; i >= 0; i -- ) {
  31. if( a[i] != 0 )break;
  32. }
  33. cout<<"Case "<<count<<":"<<endl;
  34. cout<<a1<<" "<<"+"<<" "<<b1<<" "<<"="<<" ";
  35. for(; i>=0; i--)
  36. cout<<a[i];
  37. cout<<endl;
  38. // if(count!=m)//最后一行不能多一个换行,bt
  39. // cout<<endl;
  40. }
  41. return 0;
  42. }
  1. import java.math.BigInteger;//标程
  2. import java.util.Scanner;
  3. public class Main{
  4. public static void main(String args[]) {
  5. Scanner cin=new Scanner(System.in);
  6. int n=cin.nextInt();
  7. BigInteger a,b;
  8. for(int i=1;i<=n;i++){
  9. a=cin.nextBigInteger();
  10. b=cin.nextBigInteger();
  11. System.out.println("Case "+i+":");
  12. System.out.println(a.toString()+" + "+b.toString()+" = "+a.add(b));
  13. }
  14. }
  15. }

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 1
样例输出
  1. 69087442470169316923566147
  1. /*
  2. * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=803
  3. * by jtahstu on 2015/3/31 20:00
  4. */
  5. import java.math.BigInteger;
  6. import java.util.Scanner;
  7. public class Main {
  8. public static Scanner cin = new Scanner(System.in);
  9. public static void main(String[] args) {
  10. // TODO 自动生成的方法存根
  11. BigInteger res[] = new BigInteger[105];
  12. while (cin.hasNext()) {
  13. res[0] = cin.nextBigInteger();
  14. res[1] = cin.nextBigInteger();
  15. res[2] = cin.nextBigInteger();
  16. for (int i = 3; i <= 99; i++) {
  17. res[i] = res[i - 1].add(res[i - 2]).add(res[i - 3]);
  18. }
  19. System.out.println(res[99]);
  20. }
  21. }
  22. }
  1. #include<stdio.h>//标程
  2. #include<string.h>
  3. #include <stdlib.h>
  4. void add(char a[],char b[],char back[])
  5. {
  6. int i,j,k,up,x,y,z,l;
  7. char *c;
  8. if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2;
  9. c=(char *) malloc(l*sizeof(char));
  10. i=strlen(a)-1;
  11. j=strlen(b)-1;
  12. k=0;up=0;
  13. while(i>=0||j>=0)
  14. {
  15. if(i<0) x='0'; else x=a[i];
  16. if(j<0) y='0'; else y=b[j];
  17. z=x-'0'+y-'0';
  18. if(up) z+=1;
  19. if(z>9) {up=1;z%=10;} else up=0;
  20. c[k++]=z+'0';
  21. i--;j--;
  22. }
  23. if(up) c[k++]='1';
  24. i=0;
  25. c[k]='\0';
  26. for(k-=1;k>=0;k--)
  27. back[i++]=c[k];
  28. back[i]='\0';
  29. }
  30. int main()
  31. {
  32. char a[3][1000],temp[1000];
  33. int roll=3;
  34. while(scanf("%s%s%s",a[0],a[1],a[2]) !=EOF)
  35. {
  36. for (int i=3;i<=99;i++)
  37. {
  38. add(a[(i-1)%3],a[(i-2)%3],temp);
  39. add(temp,a[(i-3)%3],a[i%3]);
  40. }
  41. printf("%s\n",a[0]);
  42. }
  43. }

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 。如果输出是整数,不要输出小数点。
样例输入
  1. 95.123 120.4321 205.1234 156.7592 998.999 101.0100 12
样例输出
  1. 548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
  1. /*
  2. * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=155
  3. * by jtahstu on 2015/3/31 20:00
  4. */
  5. import java.math.BigDecimal;
  6. import java.util.Scanner;
  7. public class Main {
  8. public static Scanner cin = new Scanner(System.in);
  9. public static void main(String[] args) {
  10. // TODO 自动生成的方法存根
  11. BigDecimal rBigDecimal;
  12. int n;
  13. while (cin.hasNext()) {
  14. rBigDecimal = cin.nextBigDecimal();
  15. n = cin.nextInt();
  16. /*
  17. * BigDecimal ans=BigDecimal.ONE; for (int i = 0; i <n; i++) {
  18. * ans=ans.multiply(rBigDecimal); }
  19. * System.out.println(ans.stripTrailingZeros().toPlainString());
  20. */
  21. String str = rBigDecimal.pow(n).stripTrailingZeros()
  22. .toPlainString();
  23. if (str.startsWith("0"))//需要删除小数点前面的0,我去
  24. str = str.substring(1);
  25. System.out.println(str);
  26. }
  27. }
  28.  
  29. }
  1. import java.util.Scanner;//标程
  2. import java.math.BigDecimal;
  3. /**
  4. *
  5. * @author Administrator
  6. */
  7. public class Main {
  8. /**
  9. * @param args the command line arguments
  10. */
  11. public static void main(String[] args) {
  12. // TODO code application logic here
  13. BigDecimal d1=null;
  14. int p;
  15. String s1;
  16. String s;
  17. Scanner cin=new Scanner(System.in);
  18. while(cin.hasNext()){
  19. s1=cin.next();
  20. p=cin.nextInt();
  21. d1=new BigDecimal(s1);
  22. s=d1.pow(p).stripTrailingZeros().toPlainString();
  23. for(int i=0;i<s.length();i++){
  24. if(i==0&&s.charAt(i)=='0')
  25. continue;
  26. System.out.print(s.charAt(i));
  27. }
  28. System.out.println();
  29. }
  30. }
  31. }

P513

A+B Problem IV

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述
acmj最近发现在使用计算器计算高精度的大数加法时很不方便,于是他想着能不能写个程序把这个问题给解决了。

 
输入
包含多组测试数据
每组数据包含两个正数A,B(可能为小数且位数不大于400)
输出
每组输出数据占一行,输出A+B的结果,结果需要是最简的形式。
样例输入
  1. 1.9 0.1
  2. 0.1 0.9
  3. 1.23 2.1
  4. 3 4.0
样例输出
  1. 2
  2. 1
  3. 3.33
  4. 7
  1. <pre name="code" class="java"> /*
  2. * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=513
  3. * by jtahstu on 2015/3/31 19:00
  4. */
  5. import java.math.BigDecimal;
  6. import java.util.Scanner;
  7. public class Main {
  8. public static Scanner cin = new Scanner(System.in);
  9. public static void main(String[] args) {
  10. // TODO 自动生成的方法存根
  11. BigDecimal abigDecimal, bbigDecimal;
  12. while (cin.hasNext()) {
  13. abigDecimal = cin.nextBigDecimal();
  14. bbigDecimal = cin.nextBigDecimal();
  15. if (abigDecimal.add(bbigDecimal).compareTo(BigDecimal.ZERO) == 0)
  16. System.out.println("0");
  17. else
  18. System.out.println(abigDecimal.add(bbigDecimal)
  19. .stripTrailingZeros().toPlainString());
  20. }
  21. }
  22. }
  1.  
  1. #include <stdio.h>//标程
  2. #include <string.h>
  3. #define MAX 1000
  4. char a[MAX],b[MAX],c[MAX];
  5. int main()
  6. {
  7. int i,j,k,l,m,n,la,lb,mx;
  8. char ch;
  9. memset(a,'0',sizeof(a));
  10. memset(b,'0',sizeof(b));
  11. while(~scanf("%s%s",a,b))
  12. {
  13. la=strlen(a);
  14. lb=strlen(b);
  15. a[la]='0';
  16. b[lb]='0';
  17. i=0;
  18. while((a[i]-'.')&&i<la)
  19. {
  20. i++;
  21. }
  22. if(i==la)
  23. a[la]='.';
  24. j=0;
  25. while((b[j]-'.')&&j<lb)
  26. {
  27. j++;
  28. }
  29. if(j==lb)
  30. b[lb]='.';
  31. m=(la-i)>(lb-j)?(la-i):(lb-j);
  32. if(i>=j)
  33. {
  34. for(l=mx=i+m,k=0,j=i-j;l>=0;l--)
  35. {
  36. if(a[l]=='.')
  37. {
  38. c[l]='.';
  39. continue;
  40. }
  41. ch=(l-j)<0?'0':b[l-j];
  42. c[l]=(a[l]-'0'+ch-'0'+k)%10+'0';
  43. k=(a[l]-'0'+ch-'0'+k)/10;
  44. }
  45. }
  46. else
  47. {
  48. for(l=mx=j+m,k=0,j=j-i;l>=0;l--)
  49. {
  50. if(b[l]=='.')
  51. {
  52. c[l]='.';
  53. continue;
  54. }
  55. ch=(l-j)<0?'0':a[l-j];
  56. c[l]=(ch-'0'+b[l]-'0'+k)%10+'0';
  57. k=(ch-'0'+b[l]-'0'+k)/10;
  58. }
  59. }
  60. if(k>0)
  61. printf("%d",k);
  62. while(c[mx]=='0')
  63. mx--;
  64. if(c[mx]=='.')
  65. mx--;
  66. for(i=0;i<=mx;i++)
  67. printf("%c",c[i]);
  68. printf("\n");
  69. memset(a,'0',sizeof(a));
  70. memset(b,'0',sizeof(b));
  71. }
  72. }

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。
样例输入
  1. 2
  2. 3
  3. 4
样例输出
  1. 2
  2. 6
  3. 12
  1. /*
  2. * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=517
  3. * by jtahstu on 2015/3/31 20:00
  4. */
  5. import java.math.BigInteger;
  6. import java.util.Scanner;
  7. public class Main {
  8. static BigInteger jt(BigInteger a,BigInteger b)
  9. {
  10. return (b.compareTo(BigInteger.valueOf(0))==0)?a:jt(b, a.mod(b));
  11. }
  12. static BigInteger tt(BigInteger a,BigInteger b)
  13. {
  14. return a.multiply(b).divide(jt(a, b));
  15. }
  16. public static Scanner cin = new Scanner(System.in);
  17. public static void main(String[] args) {
  18. // TODO 自动生成的方法存根
  19. BigInteger res[]=new BigInteger[105];
  20. int n;
  21. res[1]=BigInteger.valueOf(1);
  22. for(int i=2;i<=100;i++){
  23. res[i]=tt(BigInteger.valueOf(i), res[i-1]);
  24. }
  25. while(cin.hasNext()){
  26. n=cin.nextInt();
  27. System.out.println(res[n]);
  28. }
  29. }
  30. }
  1. #include <math.h>//标程
  2. #include <stdio.h>
  3. #include <string.h>
  4. const int M = 120;
  5. bool not_prime[M];
  6. int prime[30], point[30];
  7. struct Ac
  8. {
  9. int num[50];
  10. int len;
  11. }ans;
  12. void ac_table()
  13. {
  14. int top = -1;
  15. for(int i=2;i<M;i++)
  16. if(not_prime[i] == false)
  17. {
  18. prime[++top]=i;
  19. for(int j=2*i;j<M;j+=i)
  20. not_prime[j] = true;
  21. }
  22. }
  23. void Init()
  24. {
  25. memset(point,0,sizeof(point));
  26. memset(ans.num,0,sizeof(ans.num));
  27. ans.num[0] = 1;
  28. ans.len = 0;
  29. }
  30. void my_pow(Ac &c,int x)
  31. {
  32. for(int i=0;i<=c.len;i++)
  33. c.num[i] *= x;
  34. for(int i=0;i<=c.len;i++)
  35. if(c.num[i] >= 10000)
  36. {
  37. c.num[i+1] += c.num[i]/10000;
  38. c.num[i] = c.num[i]%10000;
  39. }
  40. c.len = c.num[c.len+1]==0 ? c.len : c.len+1;
  41. }
  42. void Print(Ac &c)
  43. {
  44. for(int i=c.len;i>=0;i--)
  45. printf(i==c.len? "%d" : "%04d",c.num[i]);
  46. printf("\n");
  47. }
  48. void divide(int x)
  49. {
  50. for(int i=0;prime[i]<=x;i++)
  51. {
  52. int tmp = 0;
  53. while(x%prime[i]==0)
  54. {
  55. tmp++;
  56. x /= prime[i];
  57. }
  58. if(tmp > point[i])
  59. {
  60. my_pow(ans,pow(prime[i] ,tmp - point[i]));
  61. point[i] = tmp;
  62. }
  63. }
  64. }
  65. int main()
  66. {
  67. int n;
  68. ac_table();
  69. while(~scanf("%d",&n))
  70. {
  71. Init();
  72. for(int i=2;i<=n;i++)
  73. divide(i);
  74. Print(ans);
  75. }
  76. return 0;
  77. }

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
  2. 1
  3.  
  4. 1.0
  5. 2.0
样例输出
  1. YES
  2. NO
  1. /*
  2. * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=524
  3. * by jtahstu on 2015/3/31 20:00
  4. */
  5. import java.math.BigDecimal;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9. public static Scanner cin = new Scanner(System.in);
  10. public static void main(String[] args) {
  11. // TODO 自动生成的方法存根
  12. BigDecimal a,b;
  13. while(cin.hasNext()){
  14. a=cin.nextBigDecimal();
  15. b=cin.nextBigDecimal();
  16. //cin.nextLine();//这里留意下
  17. if(a.compareTo(b)==0)
  18. System.out.println("YES");
  19. else {
  20. System.out.println("NO");
  21. }
  22. }
  23. }
  24.  
  25. }
  1. #include <stdio.h>//标程
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. const int M=100;
  6. void back(char* c,int &l)
  7. {
  8. if(strchr(c,'.') == NULL)
  9. return ;
  10. for(int i=l-1;i>=1;i--)
  11. {
  12. if(c[i]!='0')
  13. break;
  14. l--;
  15. }
  16. if(c[l-1]=='.')
  17. l--;
  18. c[l]='\0';
  19. //puts("after back->");
  20. //puts(c);
  21. }
  22. void front(char* c,int &l,bool &f)
  23. {
  24. int cnt = isdigit(c[0]) ? 0 : 1;
  25. if(c[0]=='-')
  26. f = false;
  27. for(int i=cnt;i<l-1;i++)
  28. {
  29. if(c[i+1]=='.' || c[i]!='0')
  30. break;
  31. cnt++;
  32. }
  33. if(cnt)
  34. {
  35. for(int i=0;i<l-cnt;i++)
  36. c[i] = c[i+cnt];
  37. c[l-cnt] = '\0';
  38. }
  39. //puts("after front->");
  40. //printf(f?"+ ":"- ");
  41. //puts(c);
  42. }
  43. void deal(char *c,bool &f)
  44. {
  45. int l = strlen(c);
  46. f = true;
  47. back(c,l);
  48. front(c,l,f);
  49. }
  50. bool Cmp(char* A,char* B,bool a,bool b)
  51. {
  52. if(strcmp(A,B) == 0)
  53. {
  54. if(strcmp(A,"0") == 0 || a^b == 0)
  55. return true;
  56. }
  57. return false;
  58. }
  59. int main()
  60. {
  61. char A[M],B[M];
  62. bool a,b;
  63. while(~scanf("%s%s",A,B))
  64. {
  65. deal(A,a);
  66. deal(B,b);
  67. puts(Cmp(A,B,a,b)?"YES":"NO");
  68. }
  69.  
  70. return 0;
  71. }

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)
输出
输出这种组合种数,占一行
样例输入
  1. 3
  2. 11
  3. 111
  4. 11111
样例输出
  1. 2
  2. 3
  3. 8
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static Scanner cin = new Scanner(System.in);
  5. public static void main(String[] args) {
  6. // TODO 自动生成的方法存根
  7. int n = cin.nextInt();
  8. BigInteger a[] = new BigInteger[201];
  9. a[1] = BigInteger.valueOf(1);
  10. a[2] = BigInteger.valueOf(2);
  11. for (int i = 3; i <= 200; i++)
  12. a[i] = a[i - 1].add(a[i - 2]);//递推,只是这是大数
  13. while (n-- > 0) {
  14. String s;
  15. s = cin.next();
  16. int len = s.length();
  17. System.out.println(a[len]);
  18. }
  19. }
  20. }

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)。
样例输入
  1. 2 16
  2. 3 27
  3. 7 4357186184021382204544
  4. 0 0
样例输出
  1. 4
  2. 3
  3. 1234
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static Scanner input = new Scanner(System.in);
  5. public static void main(String[] args) {
  6. while (true) {
  7. int n = input.nextInt();
  8. double p = input.nextDouble();
  9. if (n == 0 && p == 0)
  10. break;
  11. System.out.println(String.format("%.0f", Math.pow(p, 1.0 / n)));
  12. }
  13. }
  14. }
  1. #include <stdio.h>//标程
  2. #include <string.h>
  3. #include <math.h>
  4. int main()
  5. {
  6. //freopen("Input.txt","r",stdin);
  7. //freopen("Output1.txt","w",stdout);
  8. long mid,left,right;
  9. double p;
  10. int n;
  11. while(~scanf("%d%lf",&n,&p)){
  12. if(n==0 && p==0.0) break;
  13. left=0,right=1000000000;
  14. while(left<right)
  15. {
  16. mid=(left+right)/2;
  17. if(pow(mid,n)==p) break;
  18. if(pow(mid,n)<p)left=mid;
  19. if(pow(mid,n)>p)
  20. right=mid;
  21. }
  22. //printf("%lf\n",p);
  23. printf("%ld\n",mid);
  24. }
  25. return 0;
  26. }

P803

A/B Problem

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

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

 
输入
每组测试样例一行,首先一个号码A,中间一个或多个空格,然后一个符号( / 或者 % ),然后又是空格,后面又是一个号码B,A可能会很长,B是一个int范围的数。
输出
输出结果。
样例输入
  1. 110 / 100
  2. 99 % 10
  3. 2147483647 / 2147483647
  4. 2147483646 % 2147483647
样例输出
  1. 1
  2. 9
  3. 1
  4. 2147483646
  1. /*
  2. * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=803
  3. * by jtahstu on 2015/3/31 20:00
  4. */
  5.  
  6. import java.math.BigInteger;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10. public static Scanner cin = new Scanner(System.in);
  11. public static void main(String[] args) {
  12. // TODO 自动生成的方法存根
  13. String string;
  14. BigInteger a,b;
  15. while(cin.hasNext()){
  16. a=cin.nextBigInteger();
  17. string=cin.next();
  18. b=cin.nextBigInteger();
  19. if(string.compareTo("/")==0)
  20. System.out.println(a.divide(b));
  21. else
  22. System.out.println(a.mod(b));
  23.  
  24. }
  25. }
  26. }

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

akoj-1048-求某一整数序列的全排列问题的更多相关文章

  1. //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和

    //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和 # include<stdio.h> void main() { ,sum1; ]={,- ...

  2. 11. 求奇数分之一序列前N项和

    求奇数分之一序列前N项和 #include <stdio.h> int main() { int denominator, i, n; double item, sum; while (s ...

  3. 10. 求N分之一序列前N项和

    求N分之一序列前N项和 #include <stdio.h> int main() { int i, n; double item, sum; while (scanf("%d& ...

  4. IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果

    问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...

  5. Interview----判断整数序列是否是二叉搜索树的后序遍历结果

    题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果:   ...

  6. c 求两个整数的最大公约数和最小公倍数

    //求最大公约数是用辗转相除法,最小公倍数是根据公式 m,n 的 最大公约数* m,n最小公倍数 = m*n 来计算 #include<stdio.h> //将两个整数升序排列 void ...

  7. 求两个整数的最大公约数GCM

    思路分析: (1)求差判定法:  如果两个数相差不大,可以用大数减去小数,所得的差与小数的最大公约数就是原来两个数的最大公约数.例如:求78和60的最大公约数.78-60=18,18和60的最大公约数 ...

  8. 给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 <把一个整数各个数位进行全排列>

    """给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 -> 把一个整数各个数位进行全排列""" # 使用 permu ...

  9. 《剑指offer》写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。

    弱菜刷题还是刷中文题好了,没必要和英文过不去,现在的重点是基本代码能力的恢复. [题目] 剑指offer 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. [思路] 直觉 ...

随机推荐

  1. C 猜数游戏

    char c; clock_t start,end; time_t a,b; double var; int i,guess; srand(time(NULL));loop: printf(" ...

  2. mysql日志文件相关的配置【2】

    1.二进制日志是什么? mysql 的二进制日志用于记录数据库上做的变更. 2.二进制日志什么时间写到磁盘 1.总的来说二进制日志会在释放锁之前就写入磁盘.也就是说在commit完成之前:client ...

  3. Gradle Android客户端程序打包(基于gradle 1.12版本验证通过)

    一.前言 android客户端开发进入尾声,负责SEO同事突然发给我一个涉及45个发布渠道的噩耗,之前只发布自有渠道的工作方式(手动修改参数打包)已经不满足需求,所以引入最近比较流行的gradle打包 ...

  4. Codeforces 573B Bear and Blocks

    http://codeforces.com/problemset/problem/573/B  题目大意: 给出n个连续塔,每个塔有高度hi,每次取走最外层的块,问需要多少次操作能够拿光所有的块. 思 ...

  5. 面试题 43 n 个骰子的点数

    ; void printfProbability(int number) { ) return; ]; p[] = ]; p[] = ]; memset(p[], , )); memset(p[], ...

  6. STL 查找vector容器中的指定对象:find()与find_if()算法

    1 从vector容器中查找指定对象:find()算法 STL的通用算法find()和find_if()可以查找指定对象,参数1,即首iterator指着开始的位置,参数2,即次iterator指着停 ...

  7. java中如果需要精确的计算答案,请避免使用double类型与float类型

    double类型与float类型主要用于科学计算与工程计算而设计的,用于二进制浮点计算.对于普通计算通常是结果不准确的,所以对于普通的浮点数的加减法等,解决的方法需要用int,long,BigDeci ...

  8. cf492E Vanya and Field

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. ZooKeeper编程指导

    简介 对于想要利用ZooKeeper的协调服务来创建一个分布式应用的开发人员来说,这篇文章提供了指导.包含了一些概念和实际性操作的信息. 这篇文章的前四个章节介绍了各种ZooKeeper的概念,这对理 ...

  10. JavaScript闭包函数的写法

    <script type="text/javascript"> //通过js内置的函数构造器创建函数 var func=new Function('a','b','re ...