啥也别说了

看C++和算法,眼泪哗哗的。。。
 
 

常用链接

  • 我的随笔
  • 我的评论
  • 我参与的随笔

留言簿(4)

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类

  • algorithm(14) (rss)
  • pku/acm(59) (rss)
  • 数字图像(1) (rss)

随笔档案

  • 2010年5月 (1)
  • 2010年3月 (5)
  • 2009年3月 (1)
  • 2008年12月 (1)
  • 2008年11月 (66)

搜索

  •  

最新评论

  • 1. re: ACM 2325 Persistent Number 大数相除
  • 大数相除部分,貌似100/20的结果是错的。
  • --Raise
  • 2. re: 字典树原理(转)
  • 一看就是c++外行写的代码,
  • --ddd
  • 3. re: ACM 1664 放苹果
  • 赞。。新手 看了豁然开朗。.。谢谢了
  • --mokuku
  • 4. re: 字典树原理(转)
  • 代码风格不是很好
  • --ygqwna
  • 5. re: 字典树原理(转)[未登录]
  • 只有new,没有delete,必然内存泄露
  • --123

阅读排行榜

  • 1. 字典树原理(转)(7990)
  • 2. STL 堆排序使用和体会(转)(2091)
  • 3. ACM 2325 Persistent Number 大数相除(1872)
  • 4. 二叉树实例(1738)
  • 5. 大概了解cin,cin.getline,cin.clear,cin.ignore,cin.get()的用法(1626)

评论排行榜

  • 1. 字典树原理(转)(7)
  • 2. ACM 1730 Perfect Pth Powers(3)
  • 3. ACM 1929 Calories from Fat(2)
  • 4. ACM 2325 Persistent Number 大数相除(2)
  • 5. ACM 2316 SPIN(2)

Powered by: 博客园
模板提供:沪江博客
C++博客 | 首页 | 发新随笔 | 发新文章 | 联系 | 聚合 | 管理

ACM 2070 Filling Out the Team
建议自己电脑上实现的就不要在这上面浪费时间了,估计判定又出问题了。。。

printf能AC,而cout就WA。。。

#include<stdio.h>
int main()
{
 
double speed,weight,strength;
 
bool flag;
 
while(scanf("%lf%lf%lf",&speed,&weight,&strength)&&speed&&weight&&strength)
 
{
  flag
=0;
  
if((speed<=4.5)&&(weight>=150)&&(strength>=200))
  
{
   printf(
"Wide Receiver ");
   flag
=1;
  }

  
if((speed<=6.0)&&(weight>=300)&&(strength>=500))
  
{
   printf(
"Lineman ");
   flag
=1;
  }

  
if((speed<=5.0)&&(weight>=200)&&(strength>=300))
  
{
   printf(
"Quarterback ");
   flag
=1;
  }

  
if(!flag)printf("No positions");
  printf(
"\n");
 }

 
return 0;
}

posted @ 2008-11-27 18:45 hunter 阅读(294) | 评论 (0) | 编辑 收藏
 
ACM 2042 Lagrange's Four-Square 剪枝快了2倍
这道题我刚下手就是递归,虽然一次成功但发现时间可能不够,提交果然TLE

分析后发现,如果一次计算后,剩余的值如果比下一次开始最大值的平方的剩余次数倍还大,那就不可能了,
于是剪枝,最后采用了313MS,快了至少2倍多

#include <iostream>
#include 
<string>
#include 
<vector>
#include 
<cmath>
using namespace std;

int count;

void getCount(int current,int start,int times)
{
    
if(times==0) return;
    
for (int i=start;i>0;i--)
    
{
         
if (i*i<=current)
         
{
             current
-=i*i;
             
if (current==0)
               count
++;
             
else
             
{ 
                 
if (i*i*(times-1)<current)    //重要剪枝,若下次递归的最大值平方的times-1倍还没当前值大则剪枝
                     return;     
                 getCount(current,i,times
-1); 
             }

         current
+=i*i;
         }

    }

}


int main() 
{
    
int n;
    
while (scanf("%d",&n)!=EOF)
    
{
        
if (n==0)
          
break;
        
int start=(int)sqrt((double)n);
        count
=0;
        getCount(n,start,
4);
        printf(
"%d\n",count);
    }

}




posted @ 2008-11-27 16:13 hunter 阅读(200) | 评论 (0) | 编辑 收藏
 
ACM 2038 Team Rankings 全排的另一应用
STL里的next_permutation和strchr的配合使用,很方便

#include <iostream>
#include 
<string>
#include 
<vector>
#include 
<cmath>
#include 
<algorithm>
using namespace std;

int main()
{
   
int n; 
   
char input[100][6];
   
while (scanf("%d",&n)!=EOF)
   
{
      
if (n==0)
         
break;
      
char team[6]={'A','B','C','D','E'};
      
for (int i=0;i<n;i++)
      
{
          scanf(
"%s",input[i]);
      }

      
int count,min=9999;
      
char output[6];
      
do 
      
{
          count
=0;
          
for (int i=0;i<n;i++)
          
{
              
for (int j=0;j<4;j++)
              
{
                  
for (int k=j+1;k<5;k++)
                  
{
                      
if (strchr(input[i],team[j])>strchr(input[i],team[k]))
                      
{
                          count
++;
                      }

                  }

              }
   
          }

          
if (count<min)
          
{
              min
=count;
              memcpy(output,team,
6);
          }

      }
 while (next_permutation(team,team+5));
      printf(
"%s is the median ranking with value %d.\n",output,min);
   }

}


posted @ 2008-11-27 13:36 hunter 阅读(444) | 评论 (0) | 编辑 收藏
 
ACM 2027 No Brainer
如此水题,和第一题差不多了。。。

#include <iostream>
#include 
<string>
#include 
<vector>
#include 
<cmath>
using namespace std;

int main()
{
   
int n,x,y;
   scanf(
"%d",&n);
   
while (n--)
   
{
      scanf(
"%d %d",&x,&y);
      
if (x>=y)
      
{
          printf(
"MMM BRAINS\n");
      }

      
else
          printf(
"NO BRAINS\n");
   }

}


posted @ 2008-11-25 23:31 hunter 阅读(333) | 评论 (0) | 编辑 收藏
 
ACM 2021 Relative Relatives
计算Ted后代的年龄并排序

第一次用结构,居然一次AC了,很爽。。

运用递归查找相应节点的上一代年龄,没有则再向上查

#include <iostream>
#include 
<string>
#include 
<vector>
#include 
<cmath>
using namespace std;

struct descedant 
{
    
char father[20];
    
char child[20];
    
int age;    //child的年龄
    int differ;
    
void init();
}
;

void descedant::init()   //初始化
{
    age
=0;
    differ
=0;
}


descedant des[
100];

void getAge(descedant& d,int n)    //递归计算出年龄
{
    
for (int j=0;j<n;j++)
    
{
        
if (strcmp(d.father,des[j].child)==0)
        
{
            
if (des[j].age==0)   //未计算则递归算上一代
            {
                getAge(des[j],n);
            }

                d.age
=des[j].age-d.differ;
                
break;
        }

    }

}


int cmp(const void* a,const void* b)    //排序,年龄从大到小,若年龄相同则按字典顺序
{
    descedant
* c=(descedant*)a;
    descedant
* d=(descedant*)b;
    
if (c->age==d->age)
      
return strcmp(c->child,d->child);
    
return d->age-c->age;
}

int main()
{
   
int n,num,count=1;
   scanf(
"%d",&n);
   
while (n--)
   
{
       scanf(
"%d",&num);
       
int k=0;
       
for (int i=0;i<num;i++)
       
{
           des[k].init();
           scanf(
"%s %s %d",des[k].father,des[k].child,&des[k].differ);
           
if (strcmp(des[k].father,"Ted")==0)
           
{
               des[k].age
=100-des[k].differ;
           }

           k
++;
       }

       
for (int i=0;i<num;i++)
       
{
           
if (des[i].age==0)
           
{
               getAge(des[i],num);
           }

       }

       qsort(des,num,
sizeof(descedant),cmp);
       printf(
"DATASET %d\n",count++);
       
for (int i=0;i<num;i++)
       
{
           printf(
"%s %d\n",des[i].child,des[i].age);
       }

   }

}


posted @ 2008-11-25 23:02 hunter 阅读(542) | 评论 (0) | 编辑 收藏
 
合并排序
实现《算法导论》里的算法,一步一步来。。。

合并过程中的变量容易搞错,特别不能忽略合并过程中数组边界的改变!!!
#include <iostream>
#include 
<string>
#include 
<vector>
#include 
<cmath>
using namespace std;

void Merge(int a[],int p,int k,int q)
{
   
int num1=k-p+1;
   
int num2=q-k;
   
int i;
   
int* b1=new int[num1+1];
   
int* b2=new int[num2+1];
   
for (i=0;i<num1;i++)
   
{
       b1[i]
=a[p+i];
   }

   b1[i]
=9999;
   
for (i=0;i<num2;i++)
   
{
       b2[i]
=a[k+i+1];
   }

   b2[i]
=9999;
   
int j=0;i=0;
   
for(int kk=p;kk<=q;kk++)    //注意!!!
   
{
       
if (b1[i]<=b2[j])
           a[kk]
=b1[i++];
       
else
           a[kk]
=b2[j++];
   }

   delete [] b1;
   delete [] b2;
}

void MergeSort(int a[],int p,int q)
{
   
if (p<q)
   
{
       
int k=(p+q)/2;
       MergeSort(a,p,k);
       MergeSort(a,k
+1,q);
       Merge(a,p,k,q);
   }

}

int main()
{
   
int n;
   scanf(
"%d",&n);
   
int* input=new int[n];
   
for (int i=0;i<n;i++)
   
{
       scanf(
"%d",&input[i]);
   }

   MergeSort(input,
0,n-1);
   
for (int i=0;i<n;i++)
   
{
       printf(
"%d ",input[i]);
   }

   delete [] input;
}


posted @ 2008-11-25 21:03 hunter 阅读(185) | 评论 (0) | 编辑 收藏
 
ACM 2017 Speed Limit

难的做不来,水的不好意思做。。。。

#include <iostream>
#include 
<string>
#include 
<vector>
#include 
<cmath>
using namespace std;

int main()
{
   
int num,sum,left,v,t;
   
while (scanf("%d",&num)!=EOF)
   
{
       
if(num==-1)break; 
       sum
=0;left=0;
       
for (int i=0;i<num;i++)
       
{
           scanf(
"%d %d",&v,&t);
           sum
+=v*(t-left);
           left
=t;
       }

       printf(
"%d miles\n",sum);
   }

}


posted @ 2008-11-25 19:59 hunter 阅读(383) | 评论 (0) | 编辑 收藏
 
ACM 2013 Symmetric Order
没啥说的

#include <iostream>
#include 
<string>
#include 
<vector>
#include 
<cmath>
using namespace std;

int main()
{
   
int num,count=1;
   
while (scanf("%d",&num)!=EOF)
   
{
       
if(num==0)break; 
       
char input[15][26];
       
for (int i=0;i<num;i++)
       
{
           scanf(
"%s",input[i]);
       }

       printf(
"SET %d\n",count++);
       
for (int j=0;j<num;j+=2)
       
{
           printf(
"%s\n",input[j]);
       }

       
for (int k=num-1-num%2;k>0;k-=2)
       
{
           printf(
"%s\n",input[k]);
       }

   }

}


posted @ 2008-11-25 19:51 hunter 阅读(231) | 评论 (0) | 编辑 收藏
 
ACM 2000 Gold Coins
今天挺郁闷的,做了几道都没啥结果,有点气馁了。
想想为了将来还是继续努力吧。。。碰到一道水题,气都撒这上了

解不等式
 (for k = 1 to i)∑k = i * (i + 1) / 2 ≤ n,
 注意到i是满足上式的最小正整数,得到
 i = (√(8 * n + 1) - 1) / 2(取整数)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 于是有:
 金币数
 = (for k = 1 to i)∑k^2 + (n - i) * (i + 1)
 = (i + 1) * (6 * n - 2 * i - i^2) / 6


#include <iostream>
#include 
<string>
#include 
<vector>
#include 
<cmath>
using namespace std;

int main()
{
    
long int sum;
    
int num;
    
while (scanf("%d",&num)!=EOF)
    
{
        
if (num==0)
          
break;
        sum
=0;
        
int n=((int)(sqrt((double)(8*num+1)))-1)/2;
        
for (int j=1;j<=n;j++)
        
{
            sum
+=j*j;
        }

        sum
+=(num-n*(n+1)/2)*(n+1);
        printf(
"%ld %ld\n",num,sum);
    }

}


posted @ 2008-11-25 19:21 hunter 阅读(388) | 评论 (0) | 编辑 收藏
 
ACM 1995 Raising Modulo Numbers 很好的求幂算法
计算h个a的b次幂的和,然后对m取余。。。直接pow是不行的

查找《算法导论》后得到算法,太牛逼了

先将b转换成2进制,存放在一个数组bin里面
d<-1
for i=0 to k-1 k为bin中存放的个数
   d<-d*d mod m
   if bin[i]==1
      d<-d*a mod m
以上是a的b次幂的算法

#include <iostream>
#include 
<string>
#include 
<vector>
using namespace std;

vector
<int> bin,rev;

void GetBinary(long long int b)
{
    
long long int temp;
    
while (b)
    
{
        bin.push_back(b
%2);
        b
=b/2;
    }

    
for (int i=bin.size()-1;i>=0;i--)
    
{
        rev.push_back(bin[i]);
    }

}

int main()
{
    
int num;
    
long long int d,sum,h,m,a,b;
    scanf(
"%d",&num);
    
while (num--)
    
{
        sum
=0;
        scanf(
"%lld",&m);
        scanf(
"%lld",&h);
        
while (h--)
        
{
            scanf(
"%lld %lld",&a,&b);
            bin.clear();rev.clear();
            GetBinary(b);
            d
=1;
           
for(int index=0;index<(size_t)rev.size();index++)
           
{
               d
=(d*d)%m;
               
if (rev[index]==1)
                d
=(d*a)%m;
           }

           sum
+=d;
        }

        printf(
"%lld\n",sum%m);
    }

}


posted @ 2008-11-24 21:38 hunter 阅读(436) | 评论 (0) | 编辑 收藏
 
仅列出标题
共8页: 1 2 3 4 5 6 7 8