倒水,直接判断

题目链接: https://www.nowcoder.com/acm/contest/89/M
来源:牛客网

题目描述

There are N cups of water which are numbered from 1 to N. The ith cup contains Ai liters of water, and the magical value of the ith cup is Bi.
The 1st operation will pour B1 liters of water from the 1st cup to the 2nd cup.

The 2 nd operation will pour B 2 liters of water from the 2 nd cup to the 3 rd cup.
......


The Nth operation will pour BN liters of water from the Nth cup to the 1st cup.
The (N + 1)th operation will pour B1 liters of water from the 1st cup to the 2nd cup.
......
If the water in the cup is not enough to perform the operation, the game will stop immediately. The question is if this game will keep running forever? If not, how many times will it be operated?

输入描述:

 
 

The first line contains an integer T, where T is the number of test cases. T test cases follow. For each test case, the first line contains one integer N, where N is the number of cups.

The second line contains N integers A 1, A 2, ... , A N, where A i is the volume of water in the i th cup.
The third line contains N integers B 1, B 2, ... , B N, where B i is the magical value of the i th cup.

输出描述:

 
 
For each test case, output one line containing “Case #x: y”, where x is the test case number (startingfrom 1). If the game will keep running forever, y is “INF”. Otherwise, y is the operation times.
• 1 ≤ T ≤ 10 2
• 2 ≤ N ≤ 10 2
• 0 ≤ A i ≤ 10 9
• 1 ≤ B i ≤ 10 9.


题目很简单,大意就是轮流到水,第一行是中的每个数是每个水杯中的初始水量,下面的那行数字是每次倒出去的水量,问最多能到多少次,最后一个水杯里的水倒到第一个被子中,如果可以无限循环的话输出inf

很明显,如果他们的倒出和倒入之差等于0的话就是平衡状态,就能无线循环了,剩下的就是判断最少能到多少次水就行了

ac:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h> 

//#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

#define ll long long
#define da    0x3f3f3f3f
#define xiao -0x3f3f3f3f
#define clean(a,b) memset(a,b,sizeof(a))// 雷打不动的头文件
struct ac{
	ll v;	//总量 
	ll out;	//出水量 
	ll in;	//进水量 
	ll ci;	//最多循环多少次 
}shuzu[105];

int main()
{
	int t,k=1;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int i,j;
		for(i=1;i<=n;++i)
			cin>>shuzu[i].v;
		for(i=1;i<=n;++i)
			cin>>shuzu[i].out;
		if(shuzu[1].out>shuzu[1].v)//第一个就不行 
		{
			cout<<"Case #"<<k++<<": "<<0<<endl;
			continue;
		}
		for(i=1;i<=n;++i)//找到每个的每次循环的损失或增加量 
		{
			if(i==n)
				shuzu[1].in=shuzu[i].out-shuzu[1].out;
			else
				shuzu[i+1].in=shuzu[i].out-shuzu[i+1].out;
		}
		int f=0;//如果后面的仍是0,那么说明可以一直循环 
		ll min=1e+15;//初始化最小值为正无穷 
		if(shuzu[1].in<0)	//第一个要特判 
		{
			f=1;
			shuzu[1].ci=(shuzu[1].v-shuzu[1].out)/(shuzu[1].out-shuzu[n].out);
			shuzu[1].ci=(shuzu[1].ci+1)*n;
			if(shuzu[1].ci<min)
				min=shuzu[1].ci;
		}
		for(i=2;i<=n;++i)	//后面的符合这个规律 
		{
			if(shuzu[i].in<0)
				f=1;
			if(shuzu[i].in>=0)
				continue;
			else
			{
				shuzu[i].in=shuzu[i].in*(-1);
				shuzu[i].ci=shuzu[i].v/shuzu[i].in;
				shuzu[i].ci=shuzu[i].ci*n+i-1;
				
			}
			if(shuzu[i].ci<min)
				min=shuzu[i].ci;//最小值刷新 
		}
		if(f)		//不是无线循环 
			cout<<"Case #"<<k++<<": "<<min<<endl;
		else		//是无限循环 
			cout<<"Case #"<<k++<<": INF"<<endl;
		clean(shuzu,0);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值