声明:题目部分皆为南阳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、
#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("\n"); }
#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("\n"); return 0; }
在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的2×2方格(图2为其中缺右下角的一个),去覆盖2k×2k未被覆盖过的方格,求需要类似图2方格总的个数s。如k=1时,s=1;k=2时,s=2
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、
给你两个很大的数,你能不能判断出他们两个数的大小呢?
比如123456789123456789要大于-123456
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
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.
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
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]='\0'; for(k-=1;k>=0;k--) back[i++]=c[k]; back[i]='\0'; } 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\n",a[0]); } }
P155
对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。
现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0
< =n <= 25。
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
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("\n"); memset(a,'0',sizeof(a)); memset(b,'0',sizeof(b)); } }
P517
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("\n"); } 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问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下A-B吧。
现在有两个实数A和B,聪明的你,能不能判断出A-B的值是否等于0呢?
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]='\0'; //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] = '\0'; } //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经常遇见一个奇怪的事情,每当他看时间的时候总会看见11:11,这个很纠结啊。
现在给你m个1,你可以把2个1组合成一个2,这样就不是光棍了,问这样的组合有多少种??
例如(111 可以拆分为 111 12 21 有三种)
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]); } } }
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\n",p); printf("%ld\n",mid); } return 0; }
做了A+B Problem,A/B Problem不是什么问题了吧!
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完再来写,这几天应该就可以了。(*^-^*)
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/jtahstu/article/details/46925997