UVa725 Division(除法)

news/2024/5/18 23:39:26 标签: 枚举

1、题目

Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N N N, where 2 ≤ N ≤ 79 2 ≤ N ≤ 79 2N79. That is,
a b c d e f g h i j = N \frac{abcde}{fghij} = N fghijabcde=N
where each letter represents a different digit. The first digit of one of the numerals is allowed to be
zero.

Input

Each line of the input file consists of a valid integer N N N. An input of zero is to terminate the program.

Output

Your program have to display ALL qualifying pairs of numerals, sorted by increasing numerator (and,
of course, denominator).

Your output should be in the following general form:

xxxxx / xxxxx = N
xxxxx / xxxxx = N
.
.

In case there are no pairs of numerals satisfying the condition, you must write ‘There are no
solutions for N.
’. Separate the output for two different values of N N N by a blank line.

Sample Input

61
62
0

Sample Output

There are no solutions for 61.

79546 / 01283 = 62
94736 / 01528 = 62

725 - Division

2、题意

输入正整数 n n n,按从小到大的顺序输出所有形如 a b c d e / f g h i j = n abcde/fghij = n abcde/fghij=n 的表达式,其中 a a a ~ j j j 恰好为数字 0 ~ 9 的一个排列(可以有前导0), 2 ≤ n ≤ 79 2 \le n \le 79 2n79

3、分析

暴力枚举,但是没有必要枚举 0 ~ 9 的所有排列,只需要枚举 f g h i j fghij fghij 就可以算出 a b c d e abcde abcde,然后判断是否所有数字的偶不相同即可。枚举量从 10! - 3628800 降低至不到1万,且当 a b c d e abcde abcde f g h i j fghij fghij 加起来超过 10 位时可以终止枚举

4、代码实现

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main() {
  int n, kase = 0;
  char buf[99];
  while(scanf("%d", &n) == 1 && n) {
    int cnt = 0;
    if(kase++) printf("\n");
    for(int fghij = 1234; ; fghij++) {
      int abcde = fghij * n;
      sprintf(buf, "%05d%05d", abcde, fghij);
      if(strlen(buf) > 10) break;
      sort(buf, buf+10);
      bool ok = true;
      for(int i = 0; i < 10; i++)
        if(buf[i] != '0' + i) ok = false;
      if(ok) {
        cnt++;
        printf("%05d / %05d = %d\n", abcde, fghij, n);
      }
    }
    if(!cnt) printf("There are no solutions for %d.\n", n);
  }
  return 0;
}

http://www.niftyadmin.cn/n/5129681.html

相关文章

嵌入式软件开发工程师应该关注芯片数据手册中的哪些信息

1. 芯片的架构和处理器类型&#xff1a;了解芯片的架构和处理器类型可以帮助开发人员选择合适的开发工具和编程语言。 2. 芯片的时钟频率和电源要求&#xff1a;了解芯片的时钟频率和电源要求可以帮助开发人员设计合适的电路和电源系统。 3. 芯片的存储器类型和容量&#xff…

分布估计算法(Estimation of distribution algorithm,EDA)

概论 分布估计算法&#xff08;Estimation of distribution algorithm&#xff0c;EDA&#xff09;是一种新兴的基于统计学原理的随机优化算法。 为什么要叫这个名字呢&#xff1f; 首先&#xff0c;“分布”指的就是概率分布。 其次&#xff0c;“估计”指的是这个概率分布…

json格式存储b64编码的rgb raw数据

1.rgb raw数据准备 利用python将jpg里面的rgb raw数据提取出来。 import cv2# 读取 JPG 图像 image_path 1.jpg image cv2.imread(image_path)#imread读出来的顺序是BGR print("image shape:",image.shape)# 将图像由BGR转换为 RGB 数据 rgb_data cv2.cvtColor(im…

C语言:杨氏矩阵、杨氏三角、单身狗1与单身狗2

下面介绍四道题目和解法 1.杨氏矩阵 算法&#xff1a;右上角计算 题目&#xff1a;有一个数字矩阵&#xff0c;矩阵的每行从左到右是递增的&#xff0c;矩阵从上到下是递增的&#xff0c;请编写程序在这样的矩阵中查找某个数字是否存在。 要求&#xff1a;时间复杂度小于O(N…

c语言基础:L1-059 敲笨钟

微博上有个自称“大笨钟V”的家伙&#xff0c;每天敲钟催促码农们爱惜身体早点睡觉。为了增加敲钟的趣味性&#xff0c;还会糟改几句古诗词。其糟改的方法为&#xff1a;去网上搜寻压“ong”韵的古诗词&#xff0c;把句尾的三个字换成“敲笨钟”。例如唐代诗人李贺有名句曰&…

linux arm64 Debian12移植操作手册

linux arm64 Debian12移植操作步骤 1、前言2、开发环境3、Debian12 基础包下载下载所需构建工具debootstrap获取系统准备挂载debian文件系统替换文件系统的下载源编写挂载脚本编写卸载脚本安装日常工具添加主机名host添加用户密码打包成ext4格式的镜像包,烧写进去烧写完毕的效…

视频号创作分成计划揭秘!300+收益轻松拿,幻术视频成爆款!

今年有一个项目特别火&#xff0c;就是视频号。 有多火呢&#xff1f;很多社群的人都在说&#xff0c;现在视频号约等于捡钱红利期。 那到底是什么让他们发出这样的感慨&#xff1f;就是视频号的创作分成计划&#xff01; 这个创作分成计划就等于公众号的流量主收益&#xff0c…

SpringCloud之Sentinel概述和安装及简单整合

目录 Sentinel概述 基本介绍 Sentinel 基本核心概念 Sentinel安装 简单安装启动 启动配置项 SpringCloud简单整合 实战架构 父工程pom文件 teacher-service服务 student-service服务 测试 整合Sentinel Sentinel概述 基本介绍 Sentinel是阿里巴巴开源的一款微…