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;
        }

    }

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章