Hdu5429,数学+高精度+等比序列判断
来源:互联网 发布:中国m2历年数据曲线图 编辑:程序博客网 时间:2024/05/19 13:13
题意:
给出n个高精度的数,判断是否是等比序列。
范围:0<n<=100,每个数值长度不超过100。
分析:
根据题意,可得这道题肯定得用高精度解决,用数组模拟乘法计算过程。
判断是否为等比序列方法:a[i],a[i+1],a[i+2],对于任意的i,有a[i]*a[i+2]=a[i+1]*a[i+1]。
所以用高精度模拟乘法计算,判断相邻三项是否都满足上述关系式即可。
注意:
①题目中给出的数可能有前导零;
②等比序列的公比为非零的数;但是当序列全为零的时候,也是等比序列
如:0 0 0 0是等比序列,因为公比可以为1,2……,
而:1 0 0 0不是等比序列,公比为0。
注意这两件事,具体代码还是比较简单的,注意高精度乘法的书写细节。
具体代码:
#include<cstdio>#include<algorithm>#include<iostream>#include<vector>#include<cstring>#include<cmath>using namespace std;typedef long long LL;int t,n;char s[110][110],str[110][110];int res[300];char Res[300];string product(char a[],char b[]){// cout<<"***************************"<<endl;// cout<<a<<' '<<b<<endl; int len1=strlen(a); int len2=strlen(b); memset(res,0,sizeof(res)); memset(Res,0,sizeof(Res)); for(int i=0;i<len1;i++) //2 { for(int j=0;j<len2;j++) //1 { int c,d; c=a[i]-'0'; d=b[j]-'0'; res[i+j]+=c*d; res[i+j+1]+=(res[i+j]/10); res[i+j]%=10; //printf("%d %d %d %d\n",c,d,res[i+j],res[i+j+1]); } } for(int i=0;i<len1+len2-1;i++) Res[i]=res[i]+'0'; if(res[len1+len2-1]) Res[len1+len2-1]=res[len1+len2-1]+'0';// cout<<Res<<endl; return Res;}bool judge(string a,string b){// cout<<a<<' '<<b<<endl;// cout<<"--------------------------"<<endl; int len1=a.size(); int len2=b.size(); if(len1!=len2) return false; for(int i=0;i<len1;i++) { if(a[i]!=b[i]) return false; } return true;}int main(){ scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%s",str[i]); } int e=0; for(int i=0;i<n;i++) { int len=strlen(str[i]); for(int j=len-1;j>=0;j--) { s[i][len-1-j]=str[i][j]; } while((s[i][len-1]=='0')&&(len>1)) { len-=1; } s[i][len]='\0'; if((len==1)&&(s[i][len-1]=='0')) { e++; }// cout<<s[i]<<endl; } if(e>0) { if(e<n) cout<<"No"<<endl; else cout<<"Yes"<<endl; continue; } bool flag=true; string A,B;// char A[200],B[200]; for(int i=1;i<n-1;i++) { A=product(s[i-1],s[i+1]);// cout<<"A= "<<A<<endl; B=product(s[i],s[i]);// cout<<"B= "<<B<<endl; bool f=judge(A,B); if(!f) { flag=false; break; } } if(flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0;}/*101031 1 131 4 2516 8 4 2 1*/
0 0
- Hdu5429,数学+高精度+等比序列判断
- 【模拟高精度+进制转换】数学序列
- BZOJ 1005 明明的烦恼 Prufer序列+组合数学+高精度
- uva10056 等比序列求和公式
- 数学基础常识--数列--等差与等比
- BZOJ 1005 明明的烦恼(Purfer序列+组合数学+数论+高精度)
- POJ3982《序列》方法:高精度
- Poj1001!高精度幂计算!【数学】
- [数学 高精度] HDU 5241 Friends
- BZOJ 2729 高精度+组合数学
- hdu 1165 Eddy's research II(数学:等差 等比公式)
- HDU5429大数模板
- POJ 3982 序列(高精度)
- 序列判断
- 序列判断
- php中BC math 高精度数学函数
- UVa 10198 Counting (组合数学&高精度)
- UVa 10157 Expressions (组合数学&高精度)
- Android 去掉ListView/GridView下拉出空白回弹的效果,以及使用PullToRefresh出现的问题
- 1067. Sort with Swap(0,*) (25)
- cmd命令行大全 dos命令 cmd命令整理
- ER 图
- 如何在Linux上运行Mono
- Hdu5429,数学+高精度+等比序列判断
- Action Recognition with Trajectory-Pooled Deep-Convolutional Descriptors
- mysql导入与导出sql文件指令
- 用nodejs解析json数据
- 你想学好rails+vim+ruby+titanium等等成为全栈工程师牛人么?这里是牛人博客网站地址
- php xss filter
- 解决sessions should be nested with care, unset $TMUX to force错误
- Bestcoder#54 A problem of sorting
- php数组与字符串的转换