cpp: two cups waters

 

/*****************************************************************//**
 * \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)
	{
		float ba = 0;
		float bb = 0;
		float ver = (a + b) / 2; //平均值
		if (a > b)
		{
			ba = a - ver;
			bb = ver - b;
			cout << "第一杯水量大于第二杯水:" <<a<<">"<<b << endl;
			cout << "第一杯倒去" << ba << "升水给第二杯,两杯水相同" << endl;
		}
		if (a < b)
		{
			ba = ver - a;
			bb = b - ver;
			cout << "第一杯水量小于第二杯水:" << a << "<" << b << endl;
			cout << "第二杯倒去" << bb << "升水给第一杯,两杯水相同" << endl;
		}
		if (a == b)
		{
			cout << "第一杯水量等于第二杯水:" << a << "=" << b << endl;
		}

		return ver;

	}

	/// <summary>
	/// 
	/// </summary>
	/// <param name="a"></param>
	/// <param name="b"></param>
	/// <returns></returns>
	float bacancePoint(int* a, int* b)
	{
		float ba = 0;
		float bb = 0;
		float ver = (*a + *b) / 2; //平均值

		if (*a > *b)
		{
			ba = *a- ver;
			bb = ver -*b  ;
				
			cout << "第一杯水量大于第二杯水:" << *a << ">" << *b << endl;
			cout << "第一杯倒去" << ba << "升水给第二杯,两杯水相同"<< endl;
			
		}
		if (*a < *b)
		{
			ba = ver- *a;
			bb = *b-ver  ;
			cout << "第一杯水量小于第二杯水:" << *a << "<" << *b << endl;
			cout << "第二杯倒去" << bb << "升水给第一杯,两杯水相同" << 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;
		float offset = 0;
		if (a > b)
		{
			offset = (*a - *b) / 2;

		} 
		else
		{
			offset = (*b - *a) / 2;
		}
		//int ad = *a + offset;
		//int bd = *b +offset;

		*a = *a + offset;
		*b = *b + offset;

		if (*a == *b)
		{

			cout << "两杯水水位平衡" << endl;
			int* p = a;
			int* p2 = b;
			//*p = &a;
			//*p2 = &b;
			cout << "两杯水正互相交换" << endl;
			swap(p, p2);
			di = offset;
		}
		else
		{
			cout << "两杯水水位不平衡" << endl;
			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

  

 /// <summary>
    /// 两杯水
    /// </summary>
    void GeovinDu::dispalyTowCups()
    {

        int onecup = 0;
        int twocup = 0;
        try
        {

               /* cin.clear();*/
                std::cout << "请输入第一杯水(整数(0-100)):" << endl;

                while (true)
                {
                    //if (onecup > 0)
                    //    break;
                    std::cin >> onecup;
                    //规换成:
                   // onecup = getchar();
                    //while (!(cin >> onecup))
                    //{
                    //    std::cout << "输入的数据类型错误,请重新输入第一杯水:";//输入整数的需要两次
                    //    cin.clear();		//这一句很总要,将cin重新标记为正确,以达到重新输入的目的
                    //    while (cin.get() != '\n')		//这里清空之前cin缓冲区的数据
                    //        continue;
                    //   // while (getchar() != '\n');
                    //  
                    //}  
                    //if (typeid(onecup) == typeid(int)) {
                    //    cout << "a的数据类型是:整型" << endl;
                    //}
                    //if (!(cin >> twocup))
                    //{
                    //    cout << "输入的数据类型错误,请重新输入第一杯水:";
                    //    cin.clear();		//这一句很总要,将cin重新标记为正确,以达到重新输入的目的
                    //    while (cin.get() != '\n')		//这里清空之前cin缓冲区的数据
                    //        continue;
                    //    //while (getchar() != '\n')
                    //    //     continue;
                    //}
                    //else
                    //{
                    
                    //if (cin.rdstate())  ||  if (cin.rdstate() != ios::goodbit) 这两个也判断cin是否出错
                    ios_base::iostate flag = cin.rdstate();
                    //cout << flag << endl;
                    while (cin.fail())
                    {
                        cerr << "输入错误!请重新输入第一杯水:";
                        cin.clear();
                        //cin.ignore();
                        //cin.sync();
                        //cin.ignore(1, EOF);
                        //std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
                        cin.ignore(100, '\n');
                        cin >> onecup;
                    }
             


                    if (onecup > 0 && onecup <= 100)
                    {
                        break;
                    }
                    else
                    {
                        cin.clear();
                        std::cout << "输入的数据无效!重新输入第一杯水" << endl; //输入字符型,显示这个死循环
                        continue;
                    }
                    //}

                  
            
               } //while (onecup > 0 && onecup <= 100);
                //while (getchar() != '\n')
                //    continue;

                //do
                //{
                //     cin >> onecup;
                //     while (!(cin >> onecup))
                //     {
                //         cout << "输入的数据类型错误,请重新输入第一杯水:";
                //         cin.clear();		//这一句很总要,将cin重新标记为正确,以达到重新输入的目的
                //         while (cin.get() != '\n')		//这里清空之前cin缓冲区的数据
                //             continue;
                //         //break;
                //     }  
                //     if (onecup > 0 && onecup <= 100)
                //     {
                //         //cin.clear();
                //         //while (cin.get() != '\n')		//这里清空之前cin缓冲区的数据
                //         //    continue;
                //         break;
                //     }
                //     else
                //     {
                //         cout << "输入的数据无效!重新输入第一杯水" << endl;
                //         continue;
                //     }
      
                //         

                //} while (onecup > 0 && onecup <= 100);
                cin.clear();
                std::cout << "请输入第二杯水(整数(0-100))" << endl;
                while (true)
                {
                    cin >> twocup;
                    while (cin.fail())
                    {
                        cerr << "输入错误!请重新输入第二杯水::";
                        cin.clear();
                        //cin.ignore();
                        //cin.sync();
                        //cin.ignore(1, EOF);
                        //std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
                        cin.ignore(100, '\n');
                        cin >> twocup;
                    }
                    //twocup = getchar();
                    //while (!(cin >> twocup))
                    //{
                    //    std::cout << "输入的数据类型错误,请重新输入第二杯水:";
                    //    cin.clear();		//这一句很总要,将cin重新标记为正确,以达到重新输入的目的
                    //    while (cin.get() != '\n')		//这里清空之前cin缓冲区的数据
                    //        continue;
                    //}
                    //if (!(cin >> twocup))
                    //{
                    //    std::cout << "输入的数据类型错误,请重新输入第二杯水:";
                    //         cin.clear();		//这一句很总要,将cin重新标记为正确,以达到重新输入的目的
                    //        while (cin.get() != '\n')		//这里清空之前cin缓冲区的数据
                    //            continue;
                    //}


                    if (twocup > 0 && twocup <= 100)
                    {
                        //cin.clear();
                        //while (cin.get() != '\n')		//这里清空之前cin缓冲区的数据
                        //    continue;
                        break;
                    }
                    else
                    {
                        cin.clear();
                        std::cout << "输入的数据无效!重新输入第二杯水" << endl;
                        continue;
                    }

                }
                std::cout << "原值" << endl;
                std::cout <<"\t第一杯水"<< "\t第二杯水" << endl;
                std::cout << "\t" << onecup << "\t\t" << twocup << endl;
                int cal[2] = { onecup,twocup };
                int* sd = cal;
                int ba = calcBalance(sd, 2);
                std::cout <<"平衡数:"<< ba << endl;
                //
                bacance(onecup, twocup);

                int* p = &onecup;
                int* p2 = &twocup;
                difference(p, p2);
                std::cout << "交换的值" << endl;
                std::cout <<"\t 第一杯水"<< "\t第二杯水" << endl;
                std::cout << "\t" << *p << "\t\t" << *p2 << endl;
                bacancePoint(p,p2);


                std::cout << "第一杯水:" << isduBalanc(onecup) << endl;
                std::cout << "第二杯水:" << isduBalanc(twocup) << endl;


        }
        catch (const std::exception&)
        {
            std::cout << "输入错误"<< endl;
        }

    }

  

posted @ 2023-05-20 07:42  ®Geovin Du Dream Park™  阅读(11)  评论(0编辑  收藏  举报