cpp: two cups waters

发布时间 2023-05-20 07:48:53作者: ®Geovin Du Dream Park™

 

/*****************************************************************//**
 * \file   TwoCupsOfWaters.h
 * \brief  
 * 平衡数的定义:将一个数分成左右两部分,分别成为2个新数。左右不分必须满足:
 * 1、左边和右边至少存在一位
 * 2、左边数的每一位相乘如果等于右边数每一位相乘
 * 则这个数称为平衡数。
 * \author geovindu
 * \date 20  May 2023
 *********************************************************************/


#pragma once
#ifndef TWOCUPSOFWATERS_H 
#define TWOCUPSOFWATERS_H 

#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>



using namespace std;




namespace DuStructSimple
{

	/// <summary>
	/// 
	/// </summary>
	struct TwoWater
	{
		/// <summary>
		/// 第一杯
		/// </summary>
		int OneCups;
		/// <summary>
		/// 第二杯
		/// </summary>
		int TwoCups;


	};
	/// <summary>
	/// 
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="a"></param>
	/// <param name="b"></param>
	template<typename T>
	void duswap(T& a, T& b)
	{
		T temp(a);
		a = b;
		b = temp;

	}
	/// <summary>
	/// 两值交换
	/// </summary>
	/// <param name="ptr_a"></param>
	/// <param name="ptr_b"></param>
	void swap(int* ptr_a, int* ptr_b)
	{
		int temp = *ptr_a;
		*ptr_a = *ptr_b;
		*ptr_b = temp;
	}
	/// <summary>
	/// 两值交换
	/// </summary>
	/// <param name="a"></param>
	/// <param name="b"></param>
	void swap1(int* a, int* b)
	{
		*a = *a + *b;
		*b = *a - *b;
		*a = *a - *b;

	}
	/// <summary>
	/// 两值交换
	/// </summary>
	/// <param name="a"></param>
	/// <param name="b"></param>
	void swap2(int* a, int* b)
	{
		*a = *a * *b;
		*b = *a / *b;
		*a = *a / *b;
	}
	/// <summary>
	/// 两值交换
	/// </summary>
	/// <param name="a"></param>
	/// <param name="b"></param>
	void swap3(int* a, int* b)
	{
		*a = *a ^ *b;
		*b = *a ^ *b;
		*a = *a ^ *b;
	}

	/// <summary>
	/// 
	/// </summary>
	/// <param name="arr"></param>
	/// <param name="length"></param>
	/// <returns></returns>
	int calcBalance(int arr[], int length)
	{
		int* left = new int[length]; //left[i]为从第0个到第i-1个的和
		int* right = new int[length]; //right[i]为从第i+1个到第len-1个的和
		int b = length - 1;
		for (int i = 0; i < length; i++)
		{
			if (i == 0)
				left[i] = 0;
			else
				left[i] = left[i - 1] + arr[i - 1];
		}
		for (; b >= 0; b--)
		{
			if (b == length - 1)
				right[b] = 0;
			else
				right[b] = right[b + 1] + arr[b + 1];
			if (left[b] == right[b])
			{
				delete[] left;
				delete[] right;
				return b;
			}
		}

		delete[] left;
		delete[] right;
		return -1;
	}
	/// <summary>
	/// 
	/// </summary>
	/// <param name="a"></param>
	/// <param name="b"></param>
	/// <returns></returns>
	int bacance(int a, int b)
	{
		int ba=0;
		float ver = (a + b) / 2; //平均值
		if (a > b)
		{
			cout << "第一杯水量大于第二杯水:" <<a<<">"<<b << endl;
		}
		if (a < b)
		{
			cout << "第一杯水量小于第二杯水:" << a << "<" << b << endl;
		}
		if (a == b)
		{
			cout << "第一杯水量等于第二杯水:" << a << "=" << b << endl;
		}

		return ver;

	}

	/// <summary>
	/// 判断是否平衡数
	/// https ://blog.csdn.net/sinat_30440627/article/details/65448970
	/// 
	/// </summary>
	/// <param name="n"></param>
	/// <returns></returns>
	bool isBalance(int n) {
		if (n < 10) {
			return false;
		}
		int count = 0;
		int temp = n;   //计算位数
		while (temp != 0) {
			temp /= 10;
			count++;
		}
		vector<int> ret;   //由低位到高位放入容器内
		while (n != 0) {
			ret.push_back(n % 10);
			n /= 10;
		}

		int flag = false;
		int mult1 = 1;
		for (int i = 0; i < count - 1; i++) {   //循环相乘判断是否相等
			mult1 *= ret[i];   //右边相乘结果
			int mult2 = 1;
			for (int j = i + 1; j < count; j++) {
				mult2 *= ret[j];  //左边相乘结果
			}
			if (mult1 == mult2) {
				flag = true;
				break;
			}
		}
		if (flag) {
			return true;
		}
		else {
			return false;
		}
	}
	/// <summary>
	/// 判断是否平衡数
	/// https://www.cnblogs.com/omelet/p/6617086.html
	/// </summary>
	/// <param name="arr"></param>
	/// <param name="n"></param>
	/// <returns></returns>
	bool findCount(vector<int> arr, int n) {
		// write code here
		if (n < 2 || n>50)
			return false;

		int begin = 0;
		int end = n - 1;
		long long res1 = 1;
		long long res2 = 1;
		int count = 0;
		for (int i = 0; i < n; i++)
		{
			if (arr[i] == 0)
				count++;
		}
		if (count >= 2)//处理含有多个0的情况
			return true;

		while (begin <= end)
		{
			if (res1 <= res2)
			{
				res1 *= arr[begin];
				begin++;
			}
			else
			{
				res2 *= arr[end];
				end--;
			}
		}
		if (res1 == res2)
			return true;
		else
			return false;
	}
	/// <summary>
	/// 
	/// </summary>
	/// <param name="onecup"></param>
	/// <returns></returns>
	bool isduBalanc(int onecup)
	{
	
		if (onecup <= 10)
		{
			cout << "不是平衡数" << endl;
			return false;

		}
		vector<int> res;
		while (onecup)
		{
			res.push_back(onecup % 10);
			onecup /= 10;
		}
		bool result = findCount(res, res.size());
		if (result == 1)
		{
			cout << "是平衡数" << endl;
			return true;
		}
			
		if (result == 0)
		{
			cout << "不是平衡数" << endl;
			return false;
		}
			
	
	}
	/// <summary>
	/// 
	/// </summary>
	/// <param name="a"></param>
	/// <param name="b"></param>
	/// <returns></returns>
	int difference(int *a, int *b)
	{
		int di = 0;
		int ad = *a - *b;
		int bd = *b - *a;
		if (ad == bd)
		{

			cout << "两杯水水位平衡" << endl;
			int* p = a;
			int* p2 = b;
			//*p = &a;
			//*p2 = &b;
			cout << "两杯水正互相交换" << endl;
			swap(p, p2);
			di = ad;
		}
		else
		{
			int* p = a;
			int* p2 = b;
			//*p = &a;
			//*p2 = &b;
			cout << "两杯水正互相交换" << endl;
			swap(p, p2);
		}

		return di;


	}
	/// <summary>
	/// 
	/// </summary>
	/// <param name="n"></param>
	/// <returns></returns>
	bool match(int n) {   //平衡数匹配
		//int to string
		/*ostringstream oss;
		oss << n;
		string s = oss.str();*/
		string s = to_string(n);  //c++11

		int len = s.size(), mid = len / 2;
		bool isEven = (len % 2 == 0);
		int frontNum = 0, tailNum = 0;

		for (int i = 0; i < len; i++) {
			if (i < mid) frontNum += s[i] - '0';
			//Even: mid ~ len-1,  Odd: mid+1 ~ len-1
			else if ((isEven && i == mid) || i > mid) tailNum += s[i] - '0';
		}
		return frontNum == tailNum;
	}
	/// <summary>
	/// 
	/// </summary>
	/// <param name="n"></param>
	/// <returns></returns>
	int balanceNum(int n) {
		int sum = 0;

		if (n <= 10) { return 0; }
		if (n > 10) {
			int newNum = n;
			if (n >= 100) newNum = 99;
			int count = newNum / 10;
			for (int i = 1, j = 1; i <= count; i++, j++) {
				sum += i * 10 + j;
				cout << i * 10 + j << ", ";
			}
		}
		if (n >= 100) {
			int count = 0;
			for (int i = 101; i <= n; i++) {
				if (match(i)) {
					count++;
					cout << i << ", ";
					if (count % 10 == 0) {
						cout << endl;
					}
					sum += i;
				}
			}
		}
		return sum;
	}




}
#endif