码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-1247

时间:2015-07-19 20:13:58      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

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

/**
 * return:
 * 		-1:can not find;
 * 		positive:find the first position;
 */
int findPosition(vector<int> v,int size, int requestHalfSum){
	int sum=0;
	for(int i=0;i<size;i++){
		sum+=v[i];
		if(sum>requestHalfSum){
			return -1;
		}else if(sum==requestHalfSum){
			return i+1;
		}
	}

	return -1;
}

int main(int argc, char *argv[]){
	int n;
	while(cin>>n){
		int totalRequest=0;
		if(n==0){
			break;
		}
		vector<int> guestRequest(n);
		for(int i=0;i<n;i++){
			cin>>guestRequest[i];
			totalRequest+=guestRequest[i];
		}

		if(totalRequest%2!=0){
			cout<<"No equal partitioning."<<endl;
			continue;
		}

		int index=findPosition(guestRequest, n, totalRequest/2);

		if(index==-1){
			cout<<"No equal partitioning."<<endl;
		}else{
			cout<<"Sam stops at position "<<index<<" and Ella stops at position "<<index+1<<"."<<endl;
		}
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ-1247

标签:

原文地址:http://blog.csdn.net/eddy_liu/article/details/46956951

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!