首页 > 其他 > 详细

Stripe

时间:2015-08-08 17:52:24      阅读:155      评论:0      收藏:0      [点我收藏+]

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=22889
题意:

         给定n个数值,问有多少种方法可以将这n个数分为相等的两部分,这两部分长度不一定相等,但和一定相等。
案例:

        Input
    9

1 5 -6 7 9 -16 0 -2 2
       Output
    3
       Input
    3

1 1 1
       Output
    0
       Input
    2

0 0
       Output
    1

分析:
可以依次取两两数值中间进行分割,判断左右数值和是否相等即可。注意不要使用for语句分开单独求一边数值,那样会超时,要细心发现左右数值和之间的关系。
源代码:
 1 #include<cstdio>
 2 int n,a[100005],ant,sum=0;
 3 void cut()
 4 {
 5     int left=0,right;
 6     for(int i=0;i<n-1;i++)
 7     {
 8         left+=a[i];//左半边数值和
 9         right=sum-left;//右半边数值和
10         if(left==right) //左右相等案例成
11             ant++;
12     }
13 }
14 int main()
15 {
16     scanf("%d",&n);
17     for(int i=0;i<n;i++)
18     {
19         scanf("%d",&a[i]);
20         sum+=a[i];//计算总值
21     }
22     ant=0;//ant种分法
23     cut();
24     printf("%d\n",ant);
25     return 0;
26 }

 

Stripe

原文:http://www.cnblogs.com/huaszjh/p/4713492.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!