ACM求助。
http://www.输出-1时需要判断多少次?10^k?
希望大牛提示一下,纠结了
#include <stdio.h> int a[10][105],temp[105]; int main () { int n,k,i,j,b=0,step=1,sstep=0; while(scanf("%d%d",&n,&k)==2&&n&&k) {b=0,step=1,sstep=0; for (j=0;j<k;j++) {a[0][j]=temp[j]=n%10;n/=10;} while (1) { for (i=1;i<10;i++) { for(j=0;j<k;j++) a[i][j]=0; int p; for (p=k-1;p>0;p--)//计算temp位数 if(temp[p]!=0)break; for (int q=0;q<=p;q++) { int c=0; for (j=0;j+q<k;j++) { int s=a[i-1][j]*temp[q]+c; a[i][j+q]+=s%10; c=s/10;if(a[i][j+q]>9){a[i][j+q]-=10;c++;} } } int ok=1,sb=0;//sb表示b是否发生变化 while (ok&&b<k)//b表示已判断的位数 { for (j=0;j<=b;j++) if(a[0][j]!=a[i][j]) {ok=0;break;} if (ok) {b++;sb=1;} } if (sb) { for (j=0;j<k;j++) temp[j]=a[i][j]; for (int r=1;r<i;r++) for (int q=0;q<=p;q++) { int c=0; for (j=0;j+q<k;j++) { int s=temp[q]*a[i][j]+c; temp[j+q]=s%10; c=s/10; } } sstep+=step*i; step=step*i;break; } } if(i==10) {printf("-1\n");break;} if(b==k) {printf("%d\n",sstep);break;} } } return 0; }
#include <stdio.h> #include <string.h> #include <stdlib.h> void Mul(char *a, char *b, char *c, int k) { //求a*b后k位值,放入c中,均逆序 int Result, i, j; int a_len = strlen(a); int b_len = strlen(b); for (i = 0;i <= k;c[i++] = '\0'); for (i = 0;i < a_len && i < k;++i) for (j = 0;j < b_len && i+j < k;++j) { Result = (a[i]-'0') * (b[j]-'0') + c[i+j]; c[i+j] = Result % 10; c[i+j+1] += Result / 10; } for (i = 0;i < k;c[i++] += '0');c[i] = '\0'; } int Get(char *nn, int k) { //别的函数没问题,就是这个。。。 //我忽略了什么??? int mul = 1, i, j; char temp[101], result[101]; strcpy(temp, nn); for (i = 0;i < k;++i) { Mul(nn, temp, result, k); for (j = 1;nn[i] != result[i];++j) { if (j >= 10) return -1; strcpy(temp, result); Mul(nn, temp, result, k); } mul *= j; } return mul; } int fun(char *n, int k) { //将n后k位存入nn,并逆序 if (strcmp(n, "1") == 0 || strcmp(n, "0") == 0) return 1; int i = 0; char *p, nn[101] = {0}; for (p = n;*p;++p); for (--p;p >= n && i < k;nn[i++] = *p--); return Get(nn, k); } int main() { int k; char n[101]; while (scanf("%s%d", n, &k), n[0] != '0' && k) printf("%d\n", fun(n, k)); return 0; }
#include <iostream> #include <cstring> #define N 101 using namespace std; int a[N],b[N],m,f[N]; int getin() { char s[N]; cin>>s>>m; if (m==0) return 0; int j=0; while (s[j]) j++; for (int i=1; i<=j; i++) a[i]=s[j-i]-'0'; return 1; } int same(int *a,int *b,int p) { for (int i=1; i<=p; i++) if (a[i]!=b[i]) return 0; return 1; } int multi(int *a,int *b,int m,int p) { int c[N]; memset(c,0,sizeof(c)); for (int i=1; i<=m; i++) for (int j=1; j<=m; j++) { if (i+j>m+1) break; c[i+j-1]+=a[i]*b[j]; } for (int i=1; i<m; i++) { c[i+1]+=c[i]/10; c[i]%=10; } c[m]%=10; if (same(a,c,p)) return 1; memcpy(b,c,sizeof(int)*(m+1)); return 0; } void getout() { int i,j; memset(f,0,sizeof(f)); f[0]=1; f[1]=1; for (i=1; i<=m; i++) { memcpy(b,a,sizeof(int)*N); for (j=1; j<=10; j++) if (multi(a,b,m,i)) break; if (j<=10) { for (int k=1; k<=f[0]; k++) f[k]*=j; for (int k=1; k<=f[0]; k++) { f[k+1]+=f[k]/10; f[k]%=10; } while (f[f[0]+1]!=0) { f[0]++; f[f[0]+1]=f[f[0]]/10; f[f[0]]%=10; } memcpy(a,b,sizeof(int)*(m+1)); } else { cout<<-1<<endl; return; } } for (int i=f[0]; i>=1; i--) cout<<f[i]; cout<<endl; } int main() { while (getin()) getout(); }
#include <stdio.h> #include <string.h> #include <stdlib.h> void Mul(char *a, char *b, char *c, int k) { int Result, i, j; int a_len = strlen(a); int b_len = strlen(b); for (i = 0;i <= k;c[i++] = '\0'); for (i = 0;i < a_len && i < k;++i) for (j = 0;j < b_len && i+j < k;++j) { Result = (a[i]-'0') * (b[j]-'0') + c[i+j]; c[i+j] = Result % 10; c[i+j+1] += Result / 10; } for (i = 0;i < k;c[i++] += '0');c[i] = '\0'; } void Mul1(char *a, int b) { int i, j, result; int len = strlen(a); char *temp = (char *)malloc(len + 2); memset(temp, 0, len+2); for (i = len-1, j = 0;i >= 0; --i, ++j) { result = (a[i]-'0') * b + temp[j]; temp[j] = result % 10; temp[j+1] = result / 10; } if (result / 10 == 0) --j; for (++i;j >= 0;a[i++] = temp[j--] + '0');a[i] = '\0'; free(temp); } char *Get(char *nn, int k) { int i, j = 0; char *mul = (char *)malloc(101);strcpy(mul, "1"); if (strcmp(nn, "1") == 0 || strcmp(nn, "0") == 0) {return mul;} char temp[101], result1[101], result2[101]; char temp1[101], temp2[101]; strcpy(temp, nn); Mul(nn, temp, result1, k); Mul(result1, temp, result2, k); for (i = 0;i < k;++i) { strcpy(temp1, temp); for (j = 1;result1[i] != result2[i];++j) { if (j >= 10) {free(mul);return NULL;} Mul(temp, temp1, temp2, k); strcpy(temp1, temp2); Mul(nn, temp1, result1, k); Mul(result1, temp1, result2, k); } strcpy(temp, temp1); Mul1(mul, j); } return mul; } char *fun(char *n, int k) { int i = 0; char *p, nn[101] = {0}; for (p = n;*p;++p); for (--p;p >= n && i < k;nn[i++] = *p--); return Get(nn, k); } int main() { int k; char n[101], *p; while (scanf("%s%d", n, &k), n[0] != '0' && k) { p = fun(n, k); printf("%s\n", p ? p : "-1"); free(p); } return 0; }
#include <stdio.h> #include <string.h> int a[10][105],temp[105];char n[105]; int main () { int k,i,j,b=0,step=1,sstep=0; while(scanf("%s %d",n,&k)==2&&n[0]!='\0'&&k) {b=0,step=1,sstep=0; int w=strlen(n); for (int i=0;i<w;i++) a[0][i]=temp[i]=n[w-i-1]-'0'; while (1) { for (i=1;i<10;i++) { for(j=0;j<105;j++) a[i][j]=0; int p; for (p=k-1;p>0;p--)//计算temp位数 if(temp[p]!=0)break; for (int q=0;q<=p;q++) { int c=0; for (j=0;j+q<k;j++) { int s=a[i-1][j]*temp[q]+c; a[i][j+q]+=s%10; c=s/10;if(a[i][j+q]>9){a[i][j+q]-=10;c++;} } } int ok=1,sb=0;//sb表示b是否发生变化 while (ok&&b<k)//b表示已判断的位数 { for (j=0;j<=b;j++) if(a[0][j]!=a[i][j]) {ok=0;break;} if (ok) {b++;sb=1;} } if (sb) { for (j=0;j<k;j++) temp[j]=a[i][j]; for (int r=1;r<i;r++) for (int q=0;q<=p;q++) { int c=0; for (j=0;j+q<k;j++) { int s=temp[q]*a[i][j]+c; temp[j+q]=s%10; c=s/10; } } sstep+=step*i; step=step*i; printf("%d %d\n",step,sstep); break; } } for (int i=0;i<105;i++) {n[0]='\0';a[0][i]=temp[i]=0;} if(i==10) {printf("-1\n");break;} if(b==k) {printf("%d\n",sstep);break;} } } return 0; }自己的思路还是有问题,不做了,以后再看