啥也别说了

看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 大数相除(1873)
  • 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 1979 Red and Black

类似走迷宫。。。简单的深度搜索,递归就行了

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

char tiles[101][101];
int used[101][101];
int count,w,h;

void countstep(int i,int j)
{
    count
++;
    
if (i>1&&used[i-1][j]==0)  //向上
    {
        used[i
-1][j]=1;countstep(i-1,j);
    }

    
if (i<h&&used[i+1][j]==0)  //向下
    {
        used[i
+1][j]=1;countstep(i+1,j);
    }

    
if (j>0&&used[i][j-1]==0)  //向左
    {
        used[i][j
-1]=1;countstep(i,j-1);
    }

    
if (j<w-1&&used[i][j+1]==0)  //向右
    {
        used[i][j
+1]=1;countstep(i,j+1);
    }

}

int main()
{
   
int starti,startj;
   
while (scanf("%d %d",&w,&h)!=EOF)
   
{
       
if (w==0||h==0)
          
break;
       count
=0;
       memset(used,
0,sizeof(used));
       
for (int i=1;i<=h;i++)
       
{
           scanf(
"%s",&tiles[i]);
       }

       
for(int i=1;i<=h;i++)
       
{
           
for (int j=0;j<w;j++)
           
{  
               
if (tiles[i][j]=='#')
                  used[i][j]
=1;
               
else if (tiles[i][j]=='@')
               
{
                   used[i][j]
=1;
                   startj
=j;
                   starti
=i;
               }

           }

       }

       countstep(starti,startj);
       printf(
"%d\n",count);
   }

}


posted @ 2008-11-23 16:47 hunter 阅读(1030) | 评论 (0) | 编辑 收藏
 
ACM 1970 The Game
     摘要: 简单的五子棋判定游戏注意边界问题即可,5个以上的连续不算赢 #include <iostream>#include <string>using namespace std;int stone[20][20];bool match(int type,int j,int i){ ...  阅读全文
posted @ 2008-11-23 15:46 hunter 阅读(257) | 评论 (0) | 编辑 收藏
 
ACM 1969 Count on Canton
水题,只要找出规律就好做了。。。。

以斜列来看,列从下往上,分子分母依次递减和递增
但奇数列是从下往上数,而偶数列则相反

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

int main()
{
   
int n;
   
while (scanf("%d",&n)!=EOF)
   
{
       
int k=0,raw=n;
       
while (n>k)
       
{
           n
-=k;
           k
++;
       }

       
if (k%2==0)
           printf(
"TERM %d IS %d/%d\n",raw,n,k-n+1);
       
else
           printf(
"TERM %d IS %d/%d\n",raw,k-n+1,n);
   }

}


posted @ 2008-11-23 13:57 hunter 阅读(402) | 评论 (0) | 编辑 收藏
 
ACM 1959 Darts
未完成。。。

想用穷举法做,但不知道怎么排除重复的情况,如3*1+1*3+0和1*3+3*1+0是一样的。。。

牛人请帮忙

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

int darts[61];
int sum=0,everysum=1;

int dartscount(int current,int times,int start)
{
    
for (int i=start;i>0;i--)
    
{
          
if (darts[i]>0)
          
{
              current
=current-i;
              
if (current==0&&times>0)
              
{
                  sum
+=everysum*darts[i];
              }

              
else if (current>0&&times>1)
              
{
                  everysum
*=darts[i];
                  sum
=dartscount(current,times-1,i);
                  everysum
/=darts[i];
              }

              current
+=i;
          }

    }

    
return sum;
}


int main()
{
   memset(darts,
0,sizeof(darts));
   darts[
25]=1;darts[50]=1;
   
for (int i=1;i<=60;i++)
   
{
       
if (i<=20)
         darts[i]
++;
       
if (i%2==0&&i/2<=20)
         darts[i]
++;
       
if (i%3==0&&i/3<=20)
         darts[i]
++;
   }

  
int n,num,k=1;
  scanf(
"%d",&n);
  
while (n>0)
  
{
      printf(
"Scenario #%d:\n",k++);
      scanf(
"%d",&num);
      sum
=0;everysum=1;
      printf(
"%d\n\n",dartscount(num,3,num));
      n
--;
  }

}


posted @ 2008-11-23 13:36 hunter 阅读(223) | 评论 (0) | 编辑 收藏
 
ACM 1953 World Cup Noise
典型的DP打表输出水题。。。不过用递归却会超时,ORZ

对于这样一个0,1序列,a1,a2,a3,...a(i-2),a(i-1),a(i)...
设f(i)为输入的数对应的结果
用DP做的话,假设f(i-2),f(i-1)已经知道了,那么求f(i)应该如下:
当取a(i)=0,那么结果肯定和f(i-1)是一样的,因为在后面追加的是0,肯定不会导致存在相邻;
当取a(i)=1,那么此时只要知道a(i-2)就可以了,因为我们可以使a(i-1)为0,这样就不会导致相邻的1了;
所以a[i]=a[i-1]+a[i-2];

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

int fabi[50];
void init()
{
    fabi[
1]=2;
    fabi[
2]=3;
    
for (int j=3;j<=45;j++)
    
{
        fabi[j]
=fabi[j-1]+fabi[j-2];
    }

}


int main()
{
     
int n,i=1,num;
     init();
     cin
>>n;
     
while (n>0)
     
{
         cin
>>num;
         printf(
"Scenario #%d:\n",i++);
         printf(
"%d\n\n",fabi[num]);
         n
--;
     }

}




posted @ 2008-11-22 19:45 hunter 阅读(318) | 评论 (0) | 编辑 收藏
 
ACM 1951 Exta Krunch

又是一道恶心题。。。PE了N次

前后不能有空格,标点符号前没空格,标点符号可以重复

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

int main()
{
    
char check[30];
    check[
0]='A';check[1]='E';check[2]='I';check[3]='O';check[4]='U';
    
char input[100];
    
int j=5;bool fst=true,cfst;  //fst代表是否是第一个输入字符串,cfst代表输入字符串的第一个字符
    
while (scanf("%s",input)!=EOF)
    
{
        
int len=strlen(input);
        cfst
=true;
        
for (int i=0;i<len;i++)
        
{
            
if (strchr(check,input[i])==0)
            
{
                
if (input[i]!='.'&&input[i]!=','&&input[i]!='?'&&cfst&&fst==false)
                
{ 
                    printf(
" %c",input[i]);
                    check[j
++]=input[i];
                    cfst
=false;
                }

                
else if (input[i]!='.'&&input[i]!=','&&input[i]!='?')
                
{
                    printf(
"%c",input[i]);
                    check[j
++]=input[i];
                }

                
else
                
{
                    printf(
"%c",input[i]);
                }

                fst
=false;
                cfst
=false;
            }

        }
 
            
    }

}


posted @ 2008-11-22 17:49 hunter 阅读(287) | 评论 (0) | 编辑 收藏
 
ACM 1942 Paths on a Grid T路的计算
据说是组合数学中的T路问题C(m,m+n)

经同学解释,理解了,居然没反应过来。。。
一共走n+m步,只要从中间找出n步向上的组合,剩下的都是向右了。。。
#include < iostream >
using namespace std;
double com (double n,double m)
{
    
double i;
    
double result(1);
    
if ( n >= m - n ){ n = m-n; }
    
for ( i = m;i >= m-n+1;i-=1 )
    
{
        result 
*= (i / (i - (m - n)));
    }

    
return result;
}

int main()
{
    
double n,m;
    
while (1)
    
{
        scanf(
"%lf%lf",&n,&m);
        
if ( n==0 && m==0 ){ break; }
        printf(
"%.0lf\n",com (n,m+n));
    }

    
return 0;
}


posted @ 2008-11-22 15:37 hunter 阅读(672) | 评论 (0) | 编辑 收藏
 
二叉树实例
     摘要: 帮同学妹妹弄的作业。。。以后可以参考 #include <iostream>#include <vector>#include <string>#include <math.h>#include <iomanip>#include <stdlib.h>#includ...  阅读全文
posted @ 2008-11-22 13:52 hunter 阅读(1738) | 评论 (1) | 编辑 收藏
 
二叉树前序、中序、后序三种遍历的非递归算法
1.先序遍历非递归算法
void PreOrderUnrec(Bitree *t)
{
    Stack s;
    StackInit(s);
    Bitree *p=t;
   
    while (p!=NULL || !StackEmpty(s))
    {
        while (p!=NULL)             //遍历左子树
        {
            visite(p->data);
            push(s,p);
            p=p->lchild;  
        }
        
        if (!StackEmpty(s))         //通过下一次循环中的内嵌while实现右子树遍历
        {
            p=pop(s);
            p=p->rchild;        
        }//endif
               
    }//endwhile 
}

2.中序遍历非递归算法
void InOrderUnrec(Bitree *t)
{
    Stack s;
    StackInit(s);
    Bitree *p=t;

    while (p!=NULL || !StackEmpty(s))
    {
        while (p!=NULL)             //遍历左子树
        {
            push(s,p);
            p=p->lchild;
        }
        
        if (!StackEmpty(s))
        {
            p=pop(s);
            visite(p->data);        //访问根结点
            p=p->rchild;            //通过下一次循环实现右子树遍历
        }//endif   
   
    }//endwhile
}

3.后序遍历非递归算法
typedef enum{L,R} tagtype;
typedef struct
{
    Bitree ptr;
    tagtype tag;
}stacknode;

typedef struct
{
    stacknode Elem[maxsize];
    int top;
}SqStack;

void PostOrderUnrec(Bitree t)
{
    SqStack s;
    stacknode x;
    StackInit(s);
    p=t;
   
    do
    {
        while (p!=null)        //遍历左子树
        {
            x.ptr = p;
            x.tag = L;         //标记为左子树
            push(s,x);
            p=p->lchild;
        }
   
        while (!StackEmpty(s) && s.Elem[s.top].tag==R)  
        {
            x = pop(s);
            p = x.ptr;
            visite(p->data);   //tag为R,表示右子树访问完毕,故访问根结点      
        }
        
        if (!StackEmpty(s))
        {
            s.Elem[s.top].tag =R;     //遍历右子树
            p=s.Elem[s.top].ptr->rchild;        
        }   
    }while (!StackEmpty(s));
}//PostOrderUnrec


二。前序最简洁算法
void PreOrderUnrec(Bitree *t)
{
   Bitree *p;
   Stack s;
   s.push(t);

   while (!s.IsEmpty())
   {
      s.pop(p);
      visit(p->data);
      if (p->rchild != NULL) s.push(p->rchild);
      if (p->lchild != NULL) s.push(p->lchild);
   }
}


三。后序算法之二
void BT_PostOrderNoRec(pTreeT root)
{
stack<treeT *> s;
pTreeT pre=NULL;

while ((NULL != root) || !s.empty())
{
if (NULL != root)
{
s.push(root);
root = root->left;
}
else
{
root = s.top();
if (root->right!=NULL && pre!=root->right){
root=root->right;
}
else{
root=pre=s.top();
visit(root);
s.pop();
root=NULL;
}
}
}
}

posted @ 2008-11-22 00:04 hunter 阅读(1487) | 评论 (0) | 编辑 收藏
 
ACM 1936 All in All

极度恶心的题,貌似判定有问题。。。

   #include <iostream>
#include 
<vector>
#include 
<string>
#include 
<math.h>
#include 
<iomanip>
#include 
<stdlib.h>
#include 
<algorithm>
using namespace std;



int main()
{
    
string str1,str2;
    
int len1,len2,i,j;
    
while(cin>>str1>>str2)
    
{
        j
=0;
        len1 
= str1.length();
        len2 
= str2.length();
        
for(i = 0 ; i < len2 ; i ++)
        
{
            
if( str1[j] == str2[i])
            
{
                j
++;
            }

        }

        
if(j == len1)
            cout
<<"Yes\n";
        
else
            cout
<<"No\n";
    }

    
return 0;
}
posted @ 2008-11-21 14:37 hunter 阅读(260) | 评论 (0) | 编辑 收藏
 
仅列出标题
共8页: 1 2 3 4 5 6 7 8