cpp: two cups waters

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*****************************************************************//**
 * \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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/// <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 @   ®Geovin Du Dream Park™  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2013-05-20 Csharp: jmail組件發送郵件
2011-05-20 Repeater嵌套DataList
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示