首页 > 其他分享 >除法(Division)

除法(Division)

时间:2022-11-28 23:35:57浏览次数:63  
标签:count Division 10 int System input 除法 first



​Division​


Time Limit:3000MS

 

Memory Limit:Unknown

 

64bit IO Format:%lld & %llu


​Submit  ​​​​Status​

Description


除法(Division)_UVa

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, where

. That is,

abcde / fghij =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. 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 by a blank line.

​Sample Input​​ 


6162 0


​Sample Output​​ 


There are no solutions for 61.79546 / 01283 = 62 94736 / 01528 = 62

【分析】

       枚举abcde,然后通过N算出fghij,最后判断是否所有数字不相同(统计0到9的个数,如果个数均为1,说明所有数字均不相同)。

用java语言编写程序,代码如下:


import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(new BufferedInputStream(System.in));
boolean first = true;
while(input.hasNext()) {
int n = input.nextInt();
if(n == 0)
break;

if(first)
first = !first;
else
System.out.println();

boolean hasResult = false;
for(int i = 123; i <= 98765; i++) {
if(i % n == 0) {
int m = i / n;
if(count(i, m)) {
hasResult = true;

System.out.printf("%05d / %05d = " + n + "\n", i, m);
}
}
}

if(!hasResult)
System.out.println("There are no solutions for " + n + ".");
}
}

public static boolean count(int n, int m) {
int[] count = new int[10];
for(int i = 0; i < 5; i++) {
count[n % 10]++;
count[m % 10]++;

if(count[n % 10] > 1 || count[m % 10] > 1)
return false;

n = n / 10;
m = m / 10;
}

for(int i = 0; i < 10; i++) {
if(count[i] != 1)
return false;
}
return true;
}
}






标签:count,Division,10,int,System,input,除法,first
From: https://blog.51cto.com/u_15894233/5893791

相关文章

  • 『题解』Codeforces 808D Array Division
    题目传送门解题思路首先统计\(n\)个数字和为\(sum\),那么一半就是\(sum=sum\div2\)从\(1\)到\(n\)枚举,累计进前缀和\(ans\)中,如果发现第\(i\)个数字累......
  • 提供2个正数,要求不能用除法,请计算出商和余数
    publicstaticint[]reDiv(inta,intb){//不能用除法,要求计算出商和余数int[]arr=newint[2];intquotient=0;intremainder=0;whil......
  • 399.除法求值
    给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i]=[Ai,Bi] 和 values[i] 共同表示等式 Ai/Bi=values[i] 。每个 A......
  • 欧几里得(辗转相除法)求两个数最大公约数
    #include<stdio.h>intEA(inta,intb)//欧几里得算法{intremainder;intmiddle;if(a<b)//a,b交换值{b=a+b;a......
  • 基础循环:短除法—“倒”数
    题目描述:输入一个正整数N(0<N<2147483647),将这个数倒着合成一个新数后输出。比如:543,倒过来是345(请注意:34500,倒过来是543,不是00543)!思路解析:引入两个变量n,t(注意t要定......
  • 计算机运算方法————除法
    原码除法符号位单独计算操作数的绝对值的原码以及绝对值的补码参与运算运算的次数位参与运算的被除数的尾数的个数n次(1)恢复余数法(2)不恢复余数法(加减交替......
  • P4349 [CERC2015]Digit Division
    题目传送门思路以下纯考场思路。今天模拟赛考到了这题的加强版,然后预处理写炸了,\(100\)变成\(70\),当是给CSP攒rp了。首先一眼看到题目可能会没有思路,没什么关系,......
  • 试除法
    #define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>intmain(){ inti=0;  for(i=100;i<=200;i++) { ints=0; for(s=2;s<i;s++) {......
  • BigDecimal的加减乘除(包含除法保留两位小数,并不要四舍五入)
    同于经常要用到BigDecimal作数据运算,这里记录一下,总是忘记写法,懒得每次用再去百度了 BigDecimal的加法:BigDecimala=newBigDecimal("1");BigDecimalb=newBigD......
  • D. Array Division
    ​​http://codeforces.com/contest/808/problem/D​​一开始是没什么想法的,然后回顾下自己想题的思路,慢慢就想出来了。首先要找到是否有这样的一个位置使得:前缀和==后缀......