啥也别说了

看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 1517 u Calculate e

传说中的水题。。。
按公式计算。。。

 

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

int jie(int n)
{
    
int mul=1;
    
if (n==0)
    
{return 1;
    }

    
else
    
{for (;n>0;n--)
        
{
            mul
*=n;
        }

    }

    
return mul;
}

int main()
{
    
int n;
    
double sum=0;
    cout
<<"n e"<<endl;
    cout
<<"- -----------"<<endl;
    
for (n=0;n<=9;n++)
    
{
         sum
+=1/(double)jie(n);
         cout
<<n<<" "<<setprecision(10)<<sum<<endl;
    }

}
posted @ 2008-11-15 15:36 hunter 阅读(658) | 评论 (1) | 编辑 收藏
 
ACM 1491 pi pi的另类算法

求PI

很先进的方法。。。从0~32767个随即数中取出N个数,每2个为一对,共C2n对
找出其中除1以外没其他公约数的对子,m对

6/pi^2=m/C2n

还是暴力搜索。。。用时比较大

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


int main()
{
    
int n,ran;
    vector
<int> num;
    
while (1)
    
{
        cin
>>n;
        
if (n==0)
        
{break;    }
        num.clear();
        
int sum=n*(n-1)/2;
        
while (n>0)
        
{
            cin
>>ran;
            num.push_back(ran);
            n
--;
        }

        
bool match;
        
int count=0;
        
for (size_t i=0;i<num.size()-1;i++)
        
{
            
for (size_t j=i+1;j<num.size();j++)
            
{
                match
=true;
                
for (int k=2;k<=(num[i]>num[j]?num[j]:num[i]);k++)
                
{
                    
if (num[i]%k==0&&num[j]%k==0)
                    
{
                        match
=false;
                        
break;
                    }

                }

                
if (match==true)
                
{count++;
                }

            }

        }

        
if (count==0)
        
{
            cout
<<"No estimate for this data set."<<endl;
        }

        
else
        
{
           
double pi=6*sum/(double)count;
           cout
<<setprecision(7)<<sqrt(pi)<<endl;
        }

    }

}

PS:貌似也可以用求2数的公约数来做,当不为1时,就不要了
int gys(int a, int b)
{ int r; //用于ab互换
if(a<b){r=a; a=b; b=r;} //如果a比b小就互换,使a比b大
while (b!=0)
{
r=a%b;
a=b;
b=r;
}
/*这个while就是这个程序的核心,它不断的使a和b做除然后求除余数然后最大的数a就没有用了,因为他们的公因数与现在的r和b的公因数相同。而这时,b的值会比r大,为了保证b为小的那个数,所以需要将b的值给a将r的值给b。然后继续做除,直到b=0时表示。已经被整除掉了。这时的a就为他们的最大公约数。*/
return a; //将最大公约数返回。
}

posted @ 2008-11-15 15:22 hunter 阅读(428) | 评论 (0) | 编辑 收藏
 
ACM 1477 Box of Bricks
这道题其实没想通。。。为什么说是最小数呢

我觉得就是大于平均数的去减平均数,然后相加就是了,还有其他更复杂的情况吗?

简单题终于AC了。。。
#include <iostream>
#include 
<vector>
#include 
<string>
#include 
<math.h>
using namespace std;


int main()
{
    
int n;
    vector
<int> number;
    vector
<int> setcount;
    
while (1)
    
{
        cin
>>n;
        
if (n==0)
        
{
            
break;
        }

        
int bricks;
        number.clear();
        
while (n>0)
        
{
           cin
>>bricks;
           number.push_back(bricks);
           n
--;
        }

        
int sum=0;
        
for (size_t i=0;i<number.size();i++)
        
{
            sum
+=number[i];
        }

        
int aver=sum/(int)number.size();
        
if (sum%(int)number.size()!=0)
        
{
            
break;
        }

        
int count=0;
        
for (size_t j=0;j<number.size();j++)
        
{
            
if (number[j]>aver)
            
{
                count
+=number[j]-aver;
            }

        }

        setcount.push_back(count);        
    }

    
for (int i=0;i<(int)setcount.size();i++)
    
{
        cout
<<"Set #"<<i+1<<endl;
        cout
<<"The minimum number of moves is "<<setcount[i]<<".\n"<<endl;
    }

}
posted @ 2008-11-15 14:06 hunter 阅读(218) | 评论 (0) | 编辑 收藏
 
ACM 1468 rectangles
传说中的暴力搜索。。。

直接双重循环。。。

#include"stdio.h"
struct rec{
 
int    x1,x2,y1,y2;
 }
rec [5001];
 
 
bool cover(int i,int j)
 
{
    
if((rec[i].x1>=rec[j].x1)&&(rec[i].x2<=rec[j].x2)&&(rec[i].y1>=rec[j].y1)&&(rec[i].y2<=rec[j].y2))return true;
    
else return false;
}


int main()
{
    
int n,i,k,j;
    
while(scanf("%d",&n)!=EOF)
    
{
        k
=0;
        
for(i=1;i<=n;i++)
            scanf(
"%d%d%d%d",&rec[i].x1,&rec[i].x2,&rec[i].y1,&rec[i].y2);
        
for(i=1;i<=n;i++)
            
for(j=1;j<=n;j++)
                
if(i!=j&&cover(i,j)){k++;break; }
        printf(
"%d\n",k);
        
               
    }


}
posted @ 2008-11-15 11:58 hunter 阅读(208) | 评论 (0) | 编辑 收藏
 
ACM 1451 T9
     摘要: 类似手机智能英文输入法我的算法很复杂,虽然在自己电脑上实现了,但是在OD上居然出现了runtime error。。。先把说有输入的单词按使用频率排序,然后根据输入数字逐个查找。。用了n个循环。。。 #include <iostream>#include <vector>#include <string>#include ...  阅读全文
posted @ 2008-11-14 13:26 hunter 阅读(250) | 评论 (0) | 编辑 收藏
 
12个球找不同的智力题解题思路

2个球,大小同,其中一个重量不同。现有一个天平,要用这个天平称3次找出这个不同重量的球,如何称?

将所有球编号为1-12;
第一步,将1-4号放左边,5-8号放右边:
        若倾斜,假设右重(左重雷同):
        第二步[0],将2-4移走,6-8移入左边,9-11移入右边:
                若仍倾斜,则不同的那个为编号1或5号:
                        第三步[0][0],将1号与2号比较,若相等,则5号偏重,反之,2号球偏轻;
                若不倾斜,则不同的那个在编号2-4中,且偏轻;
                        第三步[0][1],任取2-4种两球可得结果;
        若不倾斜,则不同的在9-12中:
        第二步[1],移走6-8球,移入9-11球:
                若倾斜,假设右重(左重雷同):
                第三步[1][0],任取9-11种两球可得结果;
                若不倾斜,则12号球异常:
                第三步[1][1],将12号球与其它任意球比较可知是轻是重;

posted @ 2008-11-14 11:55 hunter 阅读(487) | 评论 (0) | 编辑 收藏
 
ACM 1455 Crazy tea party 循环逆序
类似冒泡程序
如果所有人是线性排列,那我们的工作就是类似冒泡程序做的工作:1,2,3,4,5变为5,4,3,2,1 ,耗时n(n-1)/2
但是出现了环,也就是说1,2,3,4,5变为3,2,1,5,4也可满足条件 
我们可以把这个环等分成两个部分 ,每个部分看成是线性的,再把它们花的时间加起来.
当n是偶数时, 每份人数n/2 ,即 2*(n/2 )*(n/2 -1)/2;
当n是奇数时,两份的人数分别是n/2和n/2+1,即(n/2)*(n/2 -1)/2 + (n/2 +1)*(n/2)/2 ;

这思路太强了。。。
#include <iostream>
#include 
<vector>
#include 
<string>
#include 
<math.h>
using namespace std;


int main()
{
    
int n,num,rnum;
    vector
<int> re;
    cin
>>n;
    
while (n>0)
    
{
        cin
>>num;
        
if (num%2==0)
        
{
            rnum
=(num/2-1)*(num/2)*2/2;
            re.push_back(rnum);
        }

        
if (num%2==1)
        
{
            rnum
=(num/2-1)*(num/2)/2+ (num/2+1)*(num/2)/2;
            re.push_back(rnum);
        }

        n
--;
    }

    
for (size_t i=0;i<re.size();i++)
    
{
        cout
<<re[i]<<endl;
    }

    
}
posted @ 2008-11-13 15:23 hunter 阅读(307) | 评论 (0) | 编辑 收藏
 
ACM PKU 1013 Counterfeit Dollar

这是我同学的思路,比我的好多了。。。

一次判断各球的重量,如假设轻,若在重的一边或平衡端出现则假设错

Source Code

Problem: 
1013  User: lnmm 
Memory: 64K  Time: 0MS 
Language: C
++  Result: Accepted 

Source Code 
#include
"stdio.h"
#include
"string.h"
char left[3][7],right[3][7],result[3][5];

bool isHeavy(char x )
{
    
int i;
    
for(i=0;i<3;i++)
    
{
        
switch(result[i][0])
        
{
        
case 'u':if(strchr(left[i],x)==NULL)return false;break;
        
case 'e':if(strchr(left[i],x)!=NULL||strchr(right[i],x)!=NULL)return false;break;
        
case 'd':if(strchr(right[i],x)==NULL)return false;break;
        }

    }

    
return true;
}


bool isLight(char x )
{
    
int i;
    
for(i=0;i<3;i++)
    
{
        
switch(result[i][0])
        
{
        
case 'u':if(strchr(right[i],x)==NULL)return false;break;
        
case 'e':if(strchr(left[i],x)!=NULL||strchr(right[i],x)!=NULL)return false;break;
        
case 'd':if(strchr(left[i],x)==NULL)return false;break;
        }

    }

    
return true;
}


void main()
{
    
int n;
    
char c;
    
int i;
    scanf(
"%d",&n);
    
while(n>0)
    
{
        
for( i=0;i<3;i++)
            scanf(
"%s%s%s",left[i],right[i],result[i]);
        
for(c='A';c<='L';c++)
        
{
            
if(isLight(c))
            
{
                printf(
"%c is the counterfeit coin and it is light.\n",c);
                
break;
            }

            
if(isHeavy(c))
            
{
                printf(
"%c is the counterfeit coin and it is heavy.\n",c);
                
break;
            }


        }

        n
--;

    }

}

posted @ 2008-11-12 10:14 hunter 阅读(190) | 评论 (0) | 编辑 收藏
 
ACM Calling Extraterrestrial Intelligence Again
输入三个数m,a,b,求出p,q使得a/b<=p/q<=1且p*q<m时,p,q的最大值

我是先求出sqrt(99999)中的所有质数,然后通过2个循环求出相应条件的极值

我不知道为啥错,反正自己电脑上是可以出来的。。。

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

vector
<int> prime;   //存储质数

void prim()
{
    
bool pr;
    
for (int i=(int)sqrt((double)99999);i>1;i--)
    
{
        pr
=true;
        
for (int j=(int)sqrt((double)i);j>1;j--)
        
{
            
if (i%j==0)
            
{
                pr
=false;
                
break;
            }

        }

        
if (pr==true)
        
{
            prime.push_back(i);
        }

    }

}

int main()
{
    
int m;prim();
    
double a,b;
    
while (scanf("%d %lf %lf",&m,&a,&b)!=EOF)
    
{
        
if (m==0&&a==0&&b==0)
        
{
            
break;
        }

        
if(m<=4||a>b||m>100000||a>1000||b>1000||a<1||b<1)
        
{
            
break;
        }

    
int p,q,pmax=0,qmax=0;
    
double ratio=a/b;
    
for (int i=0;i<prime.size();i++)
    
{
        
if (prime[i]>m/2)
        
{
            
continue;
        }

        
if(prime[i]<=m/2)
        
{
            
for (int j=i;j<prime.size();j++)
            
{
                 p
=prime[j];
                q
=prime[i];

                
double newratio=(double)p/(double)q;
                
if (newratio<ratio||p*q>=m)
                
{
                    
continue;
                }

                
if (p*q>pmax*qmax&&newratio>=ratio)
                
{
                    pmax
=p;
                    qmax
=q;
                }

            }
        
        
        }

        
    }

    cout
<<pmax<<" "<<qmax<<endl;
    }

    
}
posted @ 2008-11-12 01:06 hunter 阅读(362) | 评论 (0) | 编辑 收藏
 
ACM 1338 Ugly Numbers

省题不仔细,说是输入的数小于1500,而这个数是第1500个Ugly Numbers。。。
我以为是1500之内的。。。一直wrong。。。

思路是先算出前面的Ugly Numbers,再通过乘2,3,5算出之后的数
我原先是通过一个数组记录是否为Ugly Numbers,后来发现内存根本不足
5000里面只有143个。。ORZ

看了别人的讨论后,发现人家虽然也是差不多,但是不是全部列出,
而是算出一个放进一个。。。


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

int r[1501];
int Min(int a,int b,int c)    //比较大小
{
        
if(a<b)b=a;
        
if(b<c)c=b;
        
return c;
}
    
    
int main(){
        
int a;
        
int b;
        
int c;
        
int i;
        
int j;

        r[
1]=1;r[2]=2;r[3]=3;r[4]=4;r[5]=5;
        a
=3; b=2;  c=2;  
        
for(i=6;i<=1500;i++){
            r[i]
=Min(r[a]*2,r[b]*3,r[c]*5);   //把第i个之后最小的丑数找出
            for(;r[a]*2<=r[i];a++);   //找出之后相应的最大数
            for(;r[b]*3<=r[i];b++);
            
for(;r[c]*5<=r[i];c++);
        }

        scanf(
"%d",&i);
        
while(i){
            printf(
"%d\n",r[i]);
            scanf(
"%d",&i);
        }

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