这一次写一下自己的解题办法,话说有的明明一眼就知道怎么做了,却敲了好长时间
1
用1,2,3……9组成三个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi为1:2:3,求出所有解。
这个就简单了,只要换一个想法,直接让这三个三位数为1:2:3就行了
int i,k,sum,lk,temp=1,flag=0; char shuzu[99]; for(i=100;i<304;i++) { int m=i; int n=2*i; int j=3*i; flag=0; sprintf(shuzu,"%d%d%d",m,n,j); for(temp=0;temp<9;temp++) { for(j=temp+1;j<9;j++) { if(shuzu[temp]==shuzu[j]) { flag=1;//如果则为1; } } //printf("%c",shuzu[temp]); //if(temp==8)printf("\n"); } if(flag==0) { printf("%d %d %d\n",m,n,3*m); }
注意i取值是100到304,这些数的三倍不能超过三位数范畴。
2
如果一个字符串可以由某个长度为K的字符串重复多次得到,则称该串以K为周期,例如abcabcabc以三为周期,输入一个长度不超过80的字符串,输出其最小周期。
思路大致是得到每个字符出现的个数,然后求它们的最大公约数,然后求这个字符串的长度,对公约数求整就OK了
char in[90],shuzu[90]; int count[110],i=0,j=0,k=1,m=0,ch,flag=0,temp=0,stemp=0,sum=0; memset(shuzu,0,sizeof(shuzu));//此处可能产生bug memset(count,0,sizeof(count));//全部1 scanf("%s",in); shuzu[0]=in[0]; for(ch=0;ch<strlen(in);ch++) { for(j=0;j<i+1;j++) { if(in[ch]==shuzu[j]) {count[j]++;break;} if(j==i) {shuzu[i+1]=in[ch];i++;} } } for(temp=0;temp<i;temp++) { for(stemp=temp+1;stemp<i;stemp++) { if(count[temp]>count[stemp]) { int t; t=count[temp]; count[temp]=count[stemp]; count[stemp]=t; } } } for(k=count[0];flag==0;k--)//此处没考虑1的 { for(m=0;m<i;m++) { if((count[m])%k!=0)break; if(m==i-1){flag=1;break;}//一定是偶数,最大公约数找到,然后余数相加 } } for(m=0;m<=i;m++) { sum+=count[m]; } printf("%d\n",sum/(k+1));话说这个小程序被我写的好失败,这么长(ノ=Д=)ノ┻━┻
给大家一点借鉴