啥也别说了

看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 1929 Calories from Fat
为什么是runtime error?
网上也找不到相关的代码。。。

郁闷 高手看了回复下哈
#include <iostream>
#include 
<vector>
#include 
<string>
#include 
<math.h>
#include 
<iomanip>
#include 
<stdlib.h>
#include 
<algorithm>
using namespace std;

int pround(float f)
{
    
float eps=0.49;
    
int i=(int)f;
    
if((f-(float)i)<eps)
        
return i;
    
else 
        
return i+1;
}


int main()
{
    
float sumall=0,fat=0;
    
int i=0,percent=0;float sum=0;
    
char input[1000][1000];bool end=false;
    
while (1)
    
{
        
int temp,len;
        scanf(
"%s",input[i]);
        
if (strcmp(input[i],"-")==0&&end==true)
             
break;
        end
=false;
        
if (strcmp(input[i],"-")==0)
        
{
            
float per=fat*100/sumall;
            printf(
"%d",pround(per));
            printf(
"%%\n");
            sumall
=0;fat=0;
            end
=true;
            
continue;
        }

        len
=strlen(input[i]);
        sscanf(input[i],
"%d",&temp);        
        
if (input[i][len-1]=='c')
        
{
            sum
+=temp;
            
if (i%5==0)
            
{
               fat
+=temp;
            }

        }

        
else if (input[i][len-1]=='g')
        
{
            
if (i%5==0)
            
{
                sum
+=temp*9;
                fat
+=temp*9;
            }

            
else if (i%5==4)
            
{
                sum
+=temp*7;
            }

            
else
            
{
                sum
+=temp*4;
            }

        }

        
else if (input[i][len-1]=='%')
        
{
            
if (temp>100)
               
break;
            percent
+=temp;
        }

        
if (i%5==4)
        
{
            
if (percent!=0)
            
{
                sum
=sum*100/(float)(100-percent);
                len
=strlen(input[i-4]);
                
if(input[i-4][len-1]=='%')
                
{
                    sscanf(input[i
-4],"%d",&temp);
                    fat
+=temp*sum/100.0;
                }

            }

            sumall
+=sum;
            sum
=0;percent=0;
        }

        i
++;
    }

}
posted @ 2008-11-20 23:51 hunter 阅读(421) | 评论 (2) | 编辑 收藏
 
ACM 1922 Ride to School 换个角度想
自己想了很复杂,还考虑了追击再追击的问题,结果做到最后还给了个WA。。faint

查了资料,换个角度想问题居然那么简单。。。。

事实上,只要问自己一个问题就能解决这道题,Charley是和哪一位同时到达?

没错,答案是和最早到达的那个人一起到达的(当然,出发时间<0的不算了)。也就是把问题转化为求出每个人到达的时间即可。


#include<stdio.h>
#define N 100
int main()
{
    
int arr_time,last;
    
int num;
    
int ii,jj,kk;
    
int speed;
    
float tmp;
    
for(;scanf("%d",&num);)
    
{
        
if(!num)
            
return 1;
        
for(ii=0,last=10000;ii<num;ii++)
        
{
            scanf(
"%d%d",&speed,&kk);
            
if(kk>=0)
            
{
                tmp
=3600*4.5/speed+kk;
                
/**//*printf("tmp:%f  %d",tmp,(int)tmp);*/
                
if(tmp-(int)tmp<.001)
                    arr_time
=(int )tmp;
                
else
                    arr_time
=1+(int )tmp;
                last
= ( arr_time<last)?arr_time:last;
                
/**//* printf("last%d",last);*/
            }

        }

        printf(
"%d\n",last);
    }

}
posted @ 2008-11-20 01:46 hunter 阅读(362) | 评论 (0) | 编辑 收藏
 
ACM 1833 排序
STL的排序应用

#include <stdio.h>
#include 
<algorithm> 

using namespace std; 

int main(){
    
long m,n,k,i;
    
long a[1024];
    scanf(
"%ld",&m);
    
while (m--) {
        scanf(
"%ld%ld",&n,&k);
        
for (i=0;i<n;i++) scanf("%ld",&a[i]);
        
for (i=1;i<=k;i++) 
            
if (!(next_permutation(a,a+n))) 
                sort(a,a
+n);
        
for (i=0;i<n;i++) printf("%ld ",a[i]);
        printf(
"\n");
    }
;
    
return 0;
}
;
posted @ 2008-11-19 18:52 hunter 阅读(165) | 评论 (0) | 编辑 收藏
 
ACM 1775 Sum of Factorials 阶乘的和

1)设i<=n-1,有i!<=(n-1)!,就有0~n-1的阶乘和sum(i!)<=n(n-1)!=n!
2)所以i从大到小,如果i!<n,i!必须用来表示n,因为如果放弃了当前的i!,后面小于i的阶乘的和 <=i! <n(上面公式得出),所以i!必须用来表示n~~
3)n给出范围为0~1000000,经计算,10!>1000000,所以只需判断i从0~9

PS:输入0时要输出NO!,0!=1

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

long long store[10];
void calculate()
{
    
int i,mul=1;
    
for(i=1;i<10;i++)
    
{
        mul
*=i;
        store[i]
=mul;
    }

    store[
0]=1;
}





bool workout(int n)
{
    
int i;
    
if(n==0)return false;
    
for(i=9;i>=0;i--)
    
{
        
if(store[i]<=n)
            n
-=store[i];
        
if(n==0)return true;
    }

    
return false;
}





int main()
{
    
int n;
    calculate();
    
while(1)
    
{
        scanf(
"%d",&n);
        
if(n<0)break;
        
if(workout(n))printf("YES\n");
        
else printf("NO\n");
    }

    
return 0 ;
}
posted @ 2008-11-18 23:37 hunter 阅读(369) | 评论 (0) | 编辑 收藏
 
ACM 1731 Orders 全排列
自己写的递归,几经修改终于AC

判断重复的语句很关键原来我的判断是如果之前的值与现在的一样且用过,则跳过,进行下一次循环
但这样会使之后的循环也跳过,导致不必要的查询,从而导致了超时。。

优化后是判断之后的值是否与现在的一样,如果没用过就把之后的值赋值给输出,这样就没问题了

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

char input[200],output[200];
int used[200];
int group=0;
int cmp(const void* a,const void* b)
{
    
return *(char*)a-*(char*)b;
}


void orderfun(int current,int length)   //current代表当前打印第几个
{
    
for (int i=0;i<length;i++)
    
{    
        
if (i<length-1&&input[i]==input[i+1]&&used[i+1]==0)    //重复则运行下一个,判断下一个是否用过很关键,若判断上一个则超时了。。。会走很多弯路
             continue;
        
if (used[i]==0)
        
{
            
            output[current]
=input[i];
            used[i]
=1;
            
if (current==length-1)      //打印一种排列
            {
                printf(
"%s\n",output);
            }

            
else
                orderfun(current
+1,length);
            used[i]
=0;
        }

    }

}

int main()
{ 
    cin
>>input;
    
int len=strlen(input);
    qsort(input,len,
sizeof(input[0]),cmp);
    memset(used,
0,sizeof(used));
    orderfun(
0,len);
}


这里还有一个用STL的方法做的很简单(STL类库太强大了。。。)
#include <iostream>
#include 
<string>
#include 
<algorithm>
 
using namespace std;

int main()
{
    
string str;
    
while(cin>>str)
    
{
        sort(
&str[0],&str[0]+str.length());//先排序
        cout<<str<<endl;//输出排序后的
        while(next_permutation(&str[0],&str[0]+str.length()))
        
{
            cout
<<str<<endl;
        }

    }

    
return 0;
}

 

 下面有关next_permutation的介绍
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int a[10]={1,2,2,3,3,3,4,5,6,7};
    int cnt=0;
    do{
        cnt++;
    }while(next_permutation(a,a+10));    
    printf("%d\n",cnt);//输出302400
    scanf("pause");
}

next_permutation的返回值如下:如果变换后序列是非减序的则返回0,否则返回1。
所以如果想用do{...}while(next_permutation(...));的方式生成一个集合的全排列,必须先做sort。
即 便做了sort,从上面的例子我们也可以看出,当集合存在重复元素时,循环的次数并不是10!=3628800,302400是什么呢,恰是10!/ (2!*3!),也就是这个多重集的全排列数。可见在处理有重复元素的"集合"时,它是正确的且高效的,只要记住一定要先sort

posted @ 2008-11-18 13:49 hunter 阅读(846) | 评论 (0) | 编辑 收藏
 
ACM 1730 Perfect Pth Powers
#include<iostream>
#include
<math.h>
using namespace std;
int main()
{
    
long long mm,i,n;
    
double m,p;
    
while(1)
    
{
        scanf(
"%I64d",&n);
        
if(!n)break;
        
        
if(n>0)
        
{
            i
=(long long)(log((double)n)/log(2.0000000))+1;
            
for(;i>1;--i)
            
{
                m
=pow((double)n,1.0000000/(double)i);
                mm
=(long long)m;
                
if(m-(double)mm<1e-12||(double)mm+1-m<1e-12)
                
{
                    printf(
"%I64d\n",i);
                    
break;
                }

            }

            
if(i==1)
                printf(
"1\n");
        }

        
else
        
{
            n
=-n;
            i
=(long long)(log((double)n)/log(2.0000000))+1;
            
if(i%2==0)--i;
            
for(;i>1;i-=2)
            
{
                m
=pow((double)n,1.0000000/(double)i);
                mm
=(long long)m;
                
if(m-(double)mm<1e-12||(double)mm+1-m<1e-12)
                
{
                    printf(
"%I64d\n",i);
                    
break;
                }

            }

            
if(i==1)
                printf(
"1\n");
        }

    }

    
return 0;
}
直接枚举。
貌似pow()函数比log()快。之前用log()函数枚举对数的底,超时。
对输入要分正负。枚举的范围是[1,log((double)n)/log(2.0000000)+1]。
当x为正数时,p取以上区间所有整数,在符合的p中取最大。
当x为负数时,将x转化为正数处理,p取以上区间所有奇数(因为只有奇数次幂才可能等于负数),
在符合的p中取最大。

posted @ 2008-11-17 22:54 hunter 阅读(511) | 评论 (3) | 编辑 收藏
 
ACM 1665 Biker's Trip Odometer
啥数据都要float。。。double就不行。。。水题也不水了

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


int main()
{
    
float d,r,t;int n=0;
    
while (scanf("%f %f %f",&d,&r,&t)!=EOF)
    
{
        
if (r==0)
        
{
            
break;
        }

        
++n;
        printf(
"Trip #%d:",n);
        
float dis=pi*d*r/(float)(5280*12);
        
float mph=dis/(t/(float)(60*60));
        printf(
" %.2f %.2f\n",dis,mph);
    }

}
posted @ 2008-11-17 22:07 hunter 阅读(368) | 评论 (0) | 编辑 收藏
 
ACM 1664 放苹果
很典型的动态规划题

很好的算法:
f(m, n) = f(m-n, n) + f(m, n-1)

f(m, n): 把m个苹果放到n个盘子中的方法数
f(m, n-1): 把m个苹果放到n-1个盘子中的方法数(其中至少有一个空盘子)
f(m-n, n): 把m个苹果放到n个盘子中,而且每个盘子中都有苹果(先拿n个出来,等m-n个放好了,然后每个盘子放一个)

一定要牢记!!!

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

int PlaceApple(int m, int  n)
{
    
if(m < 0)
        
return 0;
    
if(m  == 0)   //每个盘子一个
        return 1;
    
if(n == 1)   //只有一个盘子
        return 1;
    
return PlaceApple(m - n, n) + PlaceApple(m, n - 1);
}


int main()
{
    
int num,m,n;
    cin
>>num;
    
while (num>0)
    
{
        cin
>>m>>n;
        cout
<<PlaceApple(m,n)<<endl;
        num
--;
    }

}
posted @ 2008-11-17 01:40 hunter 阅读(1147) | 评论 (1) | 编辑 收藏
 
ACM 1663 Number Steps
传说中的水题,too。。。

如果x为偶数,则数字为x+y
如果x为奇数,则数字为x+y-1

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

int main()
{
    
int n,a,b;
    cin
>>n;
    
while (n>0)
    
{
        cin
>>a>>b;
        
if (a==b||b==a-2)
        
{
            
if (a%2==0)
            
{
                cout
<<a+b<<endl;
            }

            
else
            
{
                cout
<<a+b-1<<endl;
            }

        }

        
else
        
{
            cout
<<"No Number"<<endl;
        }

        n
--;
    }

}

posted @ 2008-11-16 20:41 hunter 阅读(217) | 评论 (0) | 编辑 收藏
 
ACM 1658 Eva's Problem
纯水题。。。。

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

int main()
{
    
long a,b,c,d;
    
int n;
    cin
>>n;
    
while (n>0)
    
{
        scanf(
"%ld%ld%ld%ld",&a,&b,&c,&d);
        printf(
"%ld %ld %ld %ld ",a,b,c,d);
        
if (a+d==b+c)
        
{
           printf(
"%ld\n",d+b-a);
        }

        
else
        
{
            printf(
"%ld\n",d*b/a);
        }

        n
--;
    }

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