Timus 1014. The Product of Digits

Timus 1014. The Product of Digits 要求给出一个最小整数,其各位数字的乘积等于指定的数。

1014. The Product of Digits

Time Limit: 1.0 second
Memory Limit: 16 MB
Your task is to find the minimal positive integer number Q so that the product of digits of Q is exactly equal to N.

Input

The input contains the single integer number N (0 ≤ N ≤ 10 9).

Output

Your program should print to the output the only number Q. If such a number does not exist print −1.

Sample

inputoutput
10 25
Problem Source: Ural State University Internal Contest '99 #2
解答如下:
 1   using  System;
 2  using  System.IO;
 3 
 4  namespace  Skyiv.Ben.Timus
 5  {
 6     //   http://acm.timus.ru/problem.aspx?space=1 &num=1014
 7     class  T1014
 8    {
 9       static   void  Main()
10      {
11         new  T1014().Run(Console.In, Console.Out);
12      }
13 
14       void  Run(TextReader reader, TextWriter writer)
15      {
16        Out(writer, GetDigits( int .Parse(reader.ReadLine())));
17      }
18 
19       int [] GetDigits( int  n)
20      {
21         int [] digits  =   new   int [ 11 ];
22         if  (n  ==   0 ) digits[ 10 =   1 ;
23         else   if  (n  ==   1 ) digits[ 1 =   1 ;
24         else
25        {
26           for  ( int  i  =   9 ; i  >   1 ; i -- )
27          {
28             while  (n  >   1   &&  n  %  i  ==   0 )
29            {
30              n  /=  i;
31              digits[i] ++ ;
32            }
33          }
34           if  (n  !=   1 ) digits  =   null ;
35        }
36         return  digits;
37      }
38 
39       void  Out(TextWriter writer,  int [] digits)
40      {
41         if  (digits  ==   null ) writer.WriteLine( - 1 );
42         else
43        {
44           for  ( int  i  =   0 ; i  <  digits.Length; i ++ )
45          {
46             int  n  =  digits[i];
47             while  (n --   >   0 ) writer.Write(i);
48          }
49          writer.WriteLine();
50        }
51      }
52    }
53  }
54 

这题的解法还是比较直接的。但是要注意当输入 N = 0 时,输出的答案应该是 10,而不是 -1。
返回目录
.include "io32.inc" .intel_syntax noprefix .data msg_prompt: .asciz "Enter a decimal digit string (max 20 digits): " msg_max: .asciz "Maximum digit: " msg_min: .asciz "Minimum digit: " msg_sum: .asciz "Sum of digits: " msg_product: .asciz "Product of max and min: " newline: .byte 10, 0 input_buf: .space 22 # 20 digits + newline + null max_digit: .byte 0 min_digit: .byte 0 digit_sum: .long 0 product: .long 0 format_char: .asciz "%c" format_int: .asciz "%d" .text .global _main .extern _printf .extern _fgets .extern _exit _main: # 提示输入 push offset msg_prompt call _printf add esp, 4 # 读取输入 (stdin = 0) push 0 push 21 # 20+1 个字符 push offset input_buf call _fgets add esp, 12 # 处理数字串 push offset input_buf call process_digits add esp, 4 # 输出结果 call output_results # 退出程序 push 0 call _exit # 移除换行符 remove_newline: push ebp mov ebp, esp mov esi, [ebp+8] # 输入缓冲区地址 remove_loop: lodsb test al, al jz end_remove cmp al, 10 # 换行符 jne remove_loop mov byte ptr [esi-1], 0 end_remove: mov esp, ebp pop ebp ret # 处理数字串 (核心算法) process_digits: push ebp mov ebp, esp push esi push ebx mov esi, [ebp+8] # 输入字符串地址 call remove_newline # 清理换行符 # 初始化最小值和最大值 mov byte ptr [min_digit], '9' mov byte ptr [max_digit], '0' mov dword ptr [digit_sum], 0 xor ecx, ecx # 数字计数器 process_loop: mov al, [esi] test al, al jz end_process # 检查数字有效性 (ASCII 48-57) cmp al, '0' jb skip_char cmp al, '9' ja skip_char # 更新最大值: $max = \max(max, current)$ cmp al, [max_digit] jbe check_min mov [max_digit], al check_min: # 更新最小值: $min = \min(min, current)$ cmp al, [min_digit] jae calc_sum mov [min_digit], al calc_sum: # 计算数字和: $\sum digit_i$ sub al, '0' movzx eax, al add [digit_sum], eax inc ecx cmp ecx, 20 # 最多处理20个数字 skip_char: inc esi jmp process_loop end_process: # 计算乘积: $prod = (max - '0') \times (min - '0')$ mov al, [max_digit] sub al, '0' mov bl, [min_digit] sub bl, '0' mul bl mov [product], eax pop ebx pop esi mov esp, ebp pop ebp ret # 输出结果集 output_results: # 最大值 push offset msg_max call _printf add esp, 4 movzx eax, byte ptr [max_digit] push eax call print_char add esp, 4 call print_newline # 最小值 push offset msg_min call _printf add esp, 4 movzx eax, byte ptr [min_digit] push eax call print_char add esp, 4 call print_newline # 数字和 push offset msg_sum call _printf add esp, 4 push dword ptr [digit_sum] call print_number add esp, 4 call print_newline # 乘积 push offset msg_product call _printf add esp, 4 push dword ptr [product] call print_number add esp, 4 jmp print_newline # 链式调用 # 打印字符 print_char: push ebp mov ebp, esp push dword ptr [ebp+8] push offset format_char call _printf add esp, 8 pop ebp ret # 打印整数 print_number: push ebp mov ebp, esp push dword ptr [ebp+8] push offset format_int call _printf add esp, 8 pop ebp ret # 打印换行 print_newline: push offset newline call _printf add esp, 4 ret 加上从键盘输入字符串
06-11
内容概要:本文深入探讨了Kotlin语言在函数式编程和跨平台开发方面的特性和优势,结合详细的代码案例,展示了Kotlin的核心技巧和应用场景。文章首先介绍了高阶函数和Lambda表达式的使用,解释了它们如何简化集合操作和回调函数处理。接着,详细讲解了Kotlin Multiplatform(KMP)的实现方式,包括共享模块的创建和平台特定模块的配置,展示了如何通过共享业务逻辑代码提高开发效率。最后,文章总结了Kotlin在Android开发、跨平台移动开发、后端开发和Web开发中的应用场景,并展望了其未来发展趋势,指出Kotlin将继续在函数式编程和跨平台开发领域不断完善和发展。; 适合人群:对函数式编程和跨平台开发感兴趣的开发者,尤其是有一定编程基础的Kotlin初学者和中级开发者。; 使用场景及目标:①理解Kotlin中高阶函数和Lambda表达式的使用方法及其在实际开发中的应用场景;②掌握Kotlin Multiplatform的实现方式,能够在多个平台上共享业务逻辑代码,提高开发效率;③了解Kotlin在不同开发领域的应用场景,为选择合适的技术栈提供参考。; 其他说明:本文不仅提供了理论知识,还结合了大量代码案例,帮助读者更好地理解和实践Kotlin的函数式编程特性和跨平台开发能力。建议读者在学习过程中动手实践代码案例,以加深理解和掌握。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值