数的分解 时间限制:1000 ms | 内存限制:65535 KB 难度:1

数的分解

时间限制:1000 ms  |  内存限制:65535 KB

难度:1

描述

你的任务是找到一个最小的正整数Q,使Q的各位数的乘积等于N。

输入
最多450组测试数据。数据以EOF结尾。
输入一个整数N(0 ≤ N ≤ 400)。
输出
输出Q,如果Q不存在则输出−1。
样例输入
10
5
样例输出
25
5
来源
ural

 1 #include<stdio.h>
 2 #include<iostream>
 3
 4 using namespace std;
 5
 6 int main(){
 7     int n;
 8     while(scanf("%d",&n)!=EOF){
 9
10        int i = 0,ge,shi,bai,qi;
11        for(;i <= 10000 && n;i++){
12              if(i < 10){
13                 if(i == n)
14                    break;
15           }
16           else if(i < 100){
17                ge = i % 10;
18                shi = i / 10;
19                if(ge * shi == n)
20                   break;
21           }
22           else if(i < 1000){
23              ge = i % 10;
24                shi = (i / 10)%10;
25                bai = i / 100;
26                if(ge * shi * bai == n)
27                   break;
28           }
29           else {
30                ge = i % 10;
31                shi = (i / 10)%10;
32                bai = (i / 100)%10;
33                qi = i / 1000;
34                if(ge * shi * bai * qi== n)
35                   break;
36           }
37        }
38        if(i==10001)
39           cout<<-1<<endl;
40        else if(n==0)
41           cout<<10<<endl;
42        else
43              cout<<i<<endl;
44     }
45
46     return 0;
47 }

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 int res[10];
 8
 9 int main()
10 {
11     int n;
12     while(scanf("%d", &n) !=    EOF)
13     {
14         memset(res, 0, sizeof(res));
15         if(n == 0)
16         {printf("10\n"); continue;} //易错
17         else if(n < 10)
18         {printf("%d\n", n); continue;}
19         else
20         {
21             int tmp = n;
22             for(int i = 9; i >= 2; ) // 统计2-9的因子个数(倒序是关键~)
23             {
24                 if(tmp % i == 0)
25                 {
26                     res[i]++;
27                     tmp /= i;
28                 }
29                 else
30                     i--;
31             }
32             if(tmp > 10) {printf("-1\n"); continue;}
33             for(int i = 2; i <= 9; ++i) //从小到大输出每个因子即可
34                 for(int j = 1; j <= res[i]; ++j)
35                     printf("%d", i);
36             printf("\n");
37         }
38     }
39     return 0;
40 }

第二种方法比较普遍

如果一个数 N (0<=N<=1000000000),那么就要用第二种方法

时间: 2024-08-29 16:34:52

数的分解 时间限制:1000 ms | 内存限制:65535 KB 难度:1的相关文章

A+B Problem(V) 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 做了A+B Problem之后,Yougth感觉太简单了,于是他想让你求出两个数反

#include<stdio.h> int main() { int i,j,a,b,n,m; while(1) { scanf("%d%d",&m,&n); if(m==0 && n==0) { break; } i=0; while(m!=0) { a=m%10; i=i*10+a; m=(m-a)/10; } j=0; while(n!=0) { b=n%10; j=j*10+b; n=(n-b)/10; } printf("

【异常】The channel is full or unexpected failure. The source will try again after 1000 ms

Flume采集的控制台提示:The channel is full or unexpected failure. The source will try again after 1000 ms 这个是因为当前被采集的文件过大,深层的原因是文件采集的速度和sink的速度没有匹配好.所以应该可以通过增大keep-alive的值解决:#channel中最多缓存多少a1.channels.c1.capacity = 5000#channel一次最多吐给sink多少a1.channels.c1.trans

Linux下获取代码文件名、代码所在行数及日期时间的C程序实现

一.概述 在实际的软件开发项目中,为了方便排查程序问题,要求在日志文件中输出日志信息所在的程序文件名及日志代码所在的行数.此外,某些软件还会要求将程序启动时的日期时间输出到日志文件中,方便跟踪软件运行状况. 本文介绍了Linux下获取代码文件名.代码行数及日期时间的C代码实现. 二.几个标准预定义宏简介 在C语言中,使用几个标准预定义宏,便可轻松实现获取代码文件名.代码行数及日期时间的功能. 这几个宏的定义如下(注意:前后都是两条连续的下划线): __FILE__:在源文件中插入当前源文件名.

LeetCode 29. 两数相除 时间击败【100.00%】 内存击败【76.25%】

不禁让我想起了计算机是怎样进行除法运算的,单独考虑溢出以及边界情况,单独考虑符号,其他过程和我们小学除法是一模一样的:左移除数(十进制就是扩大十倍,二进制扩大两倍),直到正好比被除数小,一边累加商(在我的代码里就是33行)一边减小被除数,直到被除数不能减小,右移除数,重复上述过程. 1 public int divide(int dividend, int divisor) { 2 if (dividend == Integer.MIN_VALUE && divisor == -1) re

shell 外部传入jmeter脚本线程数,rampUp时间,持续运行时间

jmeter参数化部分参考上一篇 shell参数说明:$1线程数,$2:全部并发数rampup时间,$3:脚本持续运行时间,$4:每次脚本循环持续时间 $5:所以循环持续时间 #!/bin/bash REPORT_TIME=`date '+%Y%m%d%H%M%S'` jmeter_basedir=/Users/lyh/Desktop/dubbo-performance-testing/apache-jmeter-2.13 report_icon=$jmeter_basedir/extras/c

剑指offer-面试题49-丑数-空间换时间

/* 题目: 求从1开始的第n个丑数. */ /* 思路: 按顺序列出各个丑数. */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using namespace std; int GetUglyNumber_Solution(int index) { if(index <= 0) return 0; int

iOS 秒数转换成时间,时,分,秒

//转换成时分秒 - (NSString *)timeFormatted:(int)totalSeconds{ int seconds = totalSeconds % 60;     int minutes = (totalSeconds / 60) % 60;     int hours = totalSeconds / 3600; return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds

把视频秒数转换成时间格式输出

function secondsToTime(time,forceHours){     var hours = Math.floor(time / 3600) % 24, minutes = Math.floor(time / 60) % 60, seconds = Math.floor(time % 60), result = ( (forceHours || hours > 0) ? (hours < 10 ? '0' + hours                           

hystrix 线程数,超时时间设置测试

拜读了大拿的文章,收藏起来 https://mp.weixin.qq.com/s?__biz=MzU0OTk3ODQ3Ng==&mid=2247483791&idx=1&sn=c22fc2a6aedc15eadc819a87fe411401&chksm=fba6e98cccd1609a59fee3ba67494288f58e22dd01c3a024bbc005de1c8f79a76e391e24a991&mpshare=1&scene=1&srcid