COCKS,HENS,CHICKS 0,25,75 4,18,78 8,11,81 12,4,84
Cannot buy!
COCKS,HENS,CHICKS 8,40,252
HINT
本题重点在于了解多重循环的运行效率问题,减少一层循环可以降低很大规模的运算量。通过在循环体内加计数器可以统计出循环的运行次数,当测试数据变大时,运行次数的增长会非常可观,这就是超时的原因了。
Append Code
Eg:#include
int a,b,c;
for(a=1;a<20;a++) {
for(b=1;b<33;b++) {
for(c=3;c<100;c+=3)
{
if((a+b+c==100)&&(a*5+b*3+c/3==100)) printf(\ } } } }
答案:#include
char s[50];
long int a,b,m,n,x,y,z,flag; int c,d; gets(s);
while(scanf(\ {
flag=0;
for(x=0; x<=n; x++) {
if((((a-b)*x+b*n-m)*d)%(b*d-c)==0) {
z=(((a-b)*x+b*n-m)*d)/(double)(b*d-c); y=n-z-x;
if(fabs(a*x+b*y+z*c/(double)d-m)<0.000000001&&z*c%d==0&&x>=0&&y>=0&&z>=0) {
flag=1; } } }
if(flag==0)
printf(\ else {
printf(\ for(x=0; x<=n; x++) {
if((((a-b)*x+b*n-m)*d)%(b*d-c)==0) {
z=(((a-b)*x+b*n-m)*d)/(double)(b*d-c);
y=n-z-x;
if(fabs(a*x+b*y+z*c/(double)d-m)<0.000000001&&z*c%d==0&&x>=0&&y>=0&&z>=0) {
printf(\ } } } }
printf(\ }
return 0; }
Problem H: Print Graphics Problerm (II)
Time Limit: 1 Sec Memory Limit: 16 MB Submit: 2378 Solved: 845 [Submit][Status][Web Board]
Description
向标准输出上打印一些用ASCII字符组成的图形。
Input
输入为多个整数n,0 Output 若n为偶数,则输出一个正向的n层等腰三角形;n为奇数,则输出一个倒向的n层等腰三角形。三角形由“+”组成。任意两个图形之间有一个空行分隔,格式见sample。 Sample Input 5 4 0 Sample Output +++++++++ +++++++ +++++ +++ + + +++ +++++ +++++++ HINT Append Code [Submit][Status][Web Board] ???< 中文 ????? English ??? All Copyright Reserved 2010-2011 SDUSTOJ TEAM GPL2.0 2003-2011 HUSTOJ Project TEAM Anything about the Problems, Please Contact Admin:admin #include int n,i,j; while(n!=0) { scanf(\ if(n%2==0&&n!=0) { for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) printf(\ for(j=1;j<=2*i-1;j++) printf(\ printf(\ } } else { for(i=n;i>=1;i--) { for(j=n-i;j>=1;j--) printf(\ for(j=2*i-1;j>=1;j--) printf(\ printf(\ } } printf(\ } return 0; } Problem I: 编写函数:求三个整数的最大值 (Append Code)