• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
煎蛋啊
博客园    首页    新随笔    联系   管理    订阅  订阅
平衡数问题

牛牛在研究他自己独创的平衡数,平衡数的定义是:将一个数分成左右两部分,分别成为两个新的数。
左右部分必须满足以下两点:
1,左边和右边至少存在一位。
2,左边的数每一位相乘如果等于右边的数每一位相乘,则这个数称为平衡数。
例如:1221这个数,分成12和21的话,1*2=2*1,则称1221为平衡数,再例如:1236这个数,可以分成123和1*2*3=6,所以1236也是平衡数。而1234无论怎样分也不满足平衡数。

注意:测试用例:420306这种含有2个及以上0的数。

 

#include <iostream>
#include <algorithm>
#include "string.h"
#include "stdio.h"
#include <vector>
#include <deque>
#include <stack>
using namespace std;

class Sort {
public:
  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; } }; int main() { int num; int count = 0; cin>>num; if(num<=10) { cout<<"NO"<<endl; return 0; } vector<int> res; while(num) {   res.push_back(num%10); num/=10; } Sort solution; bool result = solution.findcount(res,res.size()); if(result == 1) cout<<"YES"<<endl; if(result == 0) cout<<"NO"<<endl; return 0; }

 

posted on 2017-03-25 14:37  煎蛋啊  阅读(8757)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3