HDU 5047 Sawtooth (大数模板+找规律)
发布时间:2020-12-14 03:01:25 所属栏目:大数据 来源:网络整理
导读:题目链接:HDU 5047 Sawtooth 题意:给出如图的M型,求N张这样的图片可以把平面最多分成几个平面。 找规律:8*N^2-7*N+1。 据说会卡JVAV,C++大数模板用了一发。 模板地址:http://www.voidcn.com/article/p-nwhuwovc-vg.html AC代码: # includestdio.h# in
题目链接:HDU 5047 Sawtooth 题意:给出如图的M型,求N张这样的图片可以把平面最多分成几个平面。 找规律:8*N^2-7*N+1。 据说会卡JVAV,C++大数模板用了一发。 模板地址:http://www.voidcn.com/article/p-nwhuwovc-vg.html AC代码: # include<stdio.h> # include<string.h> # include<malloc.h> void add(char* a,char* b,char* c) { int i,j,k,max,min,n,temp; char *s,*pmax,*pmin; max=strlen(a); min=strlen(b); if (max<min) { temp=max; max=min; min=temp; pmax=b; pmin=a; } else { pmax=a; pmin=b; } s=(char*)malloc(sizeof(char)*(max+1)); s[0]='0'; for (i=min-1,j=max-1,k=max;i>=0;i--,j--,k--) s[k]=pmin[i]-'0'+pmax[j]; for (;j>=0;j--,k--) s[k]=pmax[j]; for (i=max;i>=0;i--) if (s[i]>'9') { s[i]-=10; s[i-1]++; } if (s[0]=='0') { for (i=0;i<=max;i++) c[i-1]=s[i]; c[i-1]=' '; } else { for (i=0;i<=max;i++) c[i]=s[i]; c[i]=' '; } free(s); } /*大数减法*/ void subtract(char* a,ca,cb; ca=strlen(a); cb=strlen(b); if (ca>cb||(ca==cb&&strcmp(a,b)>=0)) { for (i=ca-1,j=cb-1;j>=0;i--,j--) a[i]-=(b[j]-'0'); for (i=ca-1;i>=0;i--) if (a[i]<'0') { a[i]+=10; a[i-1]--; } i=0; while (a[i]=='0') i++; if (a[i]==' ') { c[0]='0'; c[1]=' '; } else { for (j=0;a[i]!=' ';i++,j++) c[j]=a[i]; c[j]=' '; } } else { for (i=ca-1,j=cb-1;i>=0;i--,j--) b[j]-=(a[i]-'0'); for (j=cb-1;j>=0;j--) if (b[j]<'0') { b[j]+=10; b[j-1]--; } j=0; while (b[j]=='0') j++; i=1; c[0]='-'; for (;b[j]!=' ';i++,j++) c[i]=b[j]; c[i]=' '; } } /* 大数乘法*/ void multiply(char* a,cb,* s; ca=strlen(a); cb=strlen(b); s=(int*)malloc(sizeof(int)*(ca+cb)); for (i=0;i<ca+cb;i++) s[i]=0; for (i=0;i<ca;i++) for (j=0;j<cb;j++) s[i+j+1]+=(a[i]-'0')*(b[j]-'0'); for (i=ca+cb-1;i>=0;i--) if (s[i]>=10) { s[i-1]+=s[i]/10; s[i]%=10; } i=0; while (s[i]==0) i++; for (j=0;i<ca+cb;i++,j++) c[j]=s[i]+'0'; c[j]=' '; free(s); } char s1[1000]; char temp1[1000],temp2[1000],ans[1000]; char qi[100],ba[100],yi[100]; int main() { int t,cas=1; scanf("%d",&t); qi[0]='7'; qi[1]=' '; ba[0]='8'; ba[1]=' '; yi[0]='1'; yi[1]=' '; while(t--) { scanf("%s",s1); memset(ans,sizeof ans); multiply(s1,s1,temp1); multiply(temp1,ba,temp1); multiply(qi,temp2); subtract(temp1,temp2,ans); add(ans,yi,ans); int len=strlen(ans); printf("Case #%d: ",cas++); for(int i=0;i<len;i++) printf("%c",ans[i]); printf("n"); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |