UVA993 Product of digits【水题+字符串排序+不排序】

78 篇文章 1 订阅
70 篇文章 2 订阅

For a given non-negative integer number N, find the minimal natural Q such that the product of alldigits of Q is equal N.

Input

The first line of input contains one positive integer number, which is the number of data sets. Eachsubsequent line contains one data set which consists of one non-negative integer number N (0 ≤ N ≤109).

Output

For each data set, write one line containing the corresponding natural number Q or ‘-1’ if Q does notexist.

Sample Input

3

1

10

123456789

Sample Output

1

25

-1


问题链接UVA993 Product of digits

问题简述

  给出一个数n,找出另外一个数,其各位数字的成绩等于n。

问题分析

  对于数n,从9开始到2按照递减顺序试探能不能整除n。能整除n的话是一个可能的数字。

  如果最后,n能够被这些数字整除,那么n除以各个数字之后就变为1。

  各个数字还需要排序一下,小的在前,大的在后。

程序说明

  字符串连接运算时,稍微改变一下就不需要排序了!增加另外一个版本的程序。

题记:(略)

参考链接:(略)


AC的C++语言程序如下(无排序版本):

/* UVA993 Product of digits */

#include <iostream>

using namespace std;

int main()
{
    int t, n;

    cin >> t;
    while(t--) {
        cin >> n;

        if(n <= 9)
            cout << n << endl;
        else {
            string ans = "";

            for(int i=9; i>=1; i--)
                if(n % i == 0 && n > 1) {
                    n /= i;
                    ans = (char) (i + '0') + ans;
                }

            if(n > 1)
                cout << -1 << endl;
            else
                cout << ans << endl;
        }
    }

    return 0;
}

 

AC的C++语言程序如下:

/* UVA993 Product of digits */

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int t, n;

    cin >> t;
    while(t--) {
        cin >> n;

        if(n <= 9)
            cout << n << endl;
        else {
            string ans = "";

            for(int i=9; i>=1; i--)
                if(n % i == 0 && n > 1) {
                    n /= i;
                    ans += (char) (i + '0');
                }

            if(n > 1)
                cout << -1 << endl;
            else {
                sort(ans.begin(), ans.end());

                cout << ans << endl;
            }
        }
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值