zoukankan      html  css  js  c++  java
  • ACM

    Description

    Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into two pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece, and each piece contains positive integer amount of squares. Would you help Bob solve this problem?

    Input

    The first input line contains integer n (1 ≤ n ≤ 105) — amount of squares in the stripe. The second line contains n space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value.

    Output

    Output the amount of ways to cut the stripe into two non-empty pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.

    Sample Input

    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
    解题思路:
    这个题目的大意是输入一串数字要我们分割这串数字成为两部分,求有几种分割方法可以使这串数字的两边相加变成的相等的。我们可以想要使这串数字的两边相等也就是使他们等于这串数字总和的一半。所以我们就可以先求出这串数字的总和,当然这个和必须要是一个偶数,否则我们就找不到一个分割点使他们两边相等了,这时我们就输出一个0.我们将一串数字存在一个数组里面,从左往右加数字,当此时数字的总和等于这串数字的总和时我们就记录一下。(我们要注意的是如果这串数字的总和是等于0的话,我们就要将记录数减去一个1,因为此时我们已经加到数组的最左边了)。
    程序代码:
    #include <iostream>
    using namespace std;
    int a[100005];
    int main()
    {
        int n,s=0,w=0,v=0;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            s=s+a[i];
        }
        if(s%2)
            cout<<"0"<<endl;
        else
        {
            for(int u=0;u<n;u++)
            {
                v=v+a[u];
                if(v==s/2)
                    w++;
            }
            if(s)
                cout<<w<<endl;
            else 
                cout<<w-1<<endl;
        }
        return 0;
    }
    
    
    #include <iostream>
    using namespace std;
    int a[100005];
    int main()
    {
    	int n,s=0,w=0,v=0;
    	cin>>n;
    	for(int i=0;i<n;i++)
    	{
    		cin>>a[i];
    		s=s+a[i];
    	}
    	for(int u=0;u<n;u++)
    	{
    		v=v+a[u];
    		if(v==s-v)
    			w++;
    	}
    	if(s)
    		cout<<w<<endl;
    	else 
    		cout<<w-1<<endl;
    	return 0;
    }
    

      

    
    
  • 相关阅读:
    v-date
    文字在图片上
    v-生命周期
    彭博接口分类
    如何指定vim 的查找是从上往下还是从下往上[z]
    查看linux版本
    git web找不到new project解决方法
    比特币运行原理[z]
    [Z]haproxy+keepalived高可用群集
    blockchain good article
  • 原文地址:https://www.cnblogs.com/xinxiangqing/p/4711747.html
Copyright © 2011-2022 走看看