九度OJ 题目1016:火星A+B
来源:互联网 发布:阿里云域名解析冲突 编辑:程序博客网 时间:2024/05/17 07:13
题目链接:http://ac.jobdu.com/problem.php?pid=1016
解题时间:2013/4/14
程序源码:
#include <stdio.h>#include <stdlib.h>#include <math.h>/*int primes[] ={ 2,3,5,7,11, 13,17,19,23,29, 31,37,41,43,47, 53,59,61,67,71, 73,79,83,89,97};*/int primes[25];int a[25];int b[25];int aLen,bLen;int ans[26];int ansLen;//n>=2int IsPrime(int n){ int mid; mid = sqrt(n); if(n == 2) return 1; if(n%2==0) return 0; int i; for(i = 3; i<= mid; i += 2) if(n%i == 0) return 0; return 1;}void GetPrimes(int n){ int i=0; int number=2; while(1) { if(i>=n) break; if(IsPrime(number)) { primes[i++]=number; } number++; }}//对阶void Match(){ int xLen;//位数差值 int i; if(aLen<bLen) { xLen=bLen-aLen; for(i=1; i<=aLen; i++) { a[bLen-i]=a[aLen-i]; } for(i=0; i<xLen; i++) { a[i]=0; } aLen=bLen; } if(aLen>bLen) { xLen=aLen-bLen; for(i=1; i<=bLen; i++) { b[aLen-i]=b[bLen-i]; } for(i=0; i<xLen; i++) { b[i]=0; } bLen=aLen; }}//加void Add(){ int carry=0;//进位 int i,j; for(i=aLen-1,j=0; i>=0; i--,j++) { if((a[i]+b[i]+carry)>=primes[j]) { ans[j]=a[i]+b[i]+carry-primes[j]; carry=1; } else { ans[j]=a[i]+b[i]+carry; carry=0; } } ansLen=aLen; if(carry==1) { ans[ansLen]=1; ansLen++; }}int main(){ char ch; GetPrimes(25); while(1) { aLen=0; while(1) { scanf("%d",&a[aLen++]); scanf("%c",&ch); if(ch==' ') break; if(ch==',') continue; } bLen=0; while(1) { scanf("%d",&b[bLen++]); scanf("%c",&ch); if(ch=='\n') break; if(ch==',') continue; } if(a[0]==0&&b[0]==0) break; //相加操作 Match(); Add(); //输出 int i; for(i=ansLen-1; i>0; i--) { printf("%d,",ans[i]); } printf("%d\n",ans[0]); } GetPrimes(25); return 0;}
- 九度OJ 题目1016:火星A+B
- 九度oj 题目1016:火星A+B
- 九度OJ 题目1016:火星A+B
- 九度OJ-题目1016 火星A+B
- 九度OJ 1016 火星A+B
- 九度oj 题目1016:火星A+B 【ZJU2006考研机试题2】
- 九度OJ 1016:火星A+B 长整数加法
- hdu 1230&&九度oj 1016 火星A+B
- 九度:1016<火星A+B>
- 九度[1016]-火星A+B
- a+b (九度oj 题目60)
- 九度OJ 题目1010:A + B
- 九度OJ 题目1003:A+B
- 九度OJ题目1003:A+B
- 九度OJ-题目1010:A + B
- 九度OJ 题目1010:A + B
- 九度OJ 题目1003:A+B
- 九度OJ-题目1003:A+B
- url.openStream()和url.openConnection().getInputStream()两者区别
- 实现重定向
- Mysql的函数大全(1)
- [4月8日的脚本] 显示活动目录(AD)中被删除的对象 (PowerShell)
- 【DP_背包专辑】【4.04最新更新】
- 九度OJ 题目1016:火星A+B
- HDU 2602 Bone Collector(裸01背包)
- 学习笔记08—类加载器
- pv操作
- 第四章代码
- android中Path类的知识总结
- Mysql的函数大全(2)
- oracle函数 总结
- [4月9日的脚本] 如何删除Windows 8中的“Windows.old”文件夹