对于一个数整除到n的算法 [英] An algorithm for a number divisible to n

查看:194
本文介绍了对于一个数整除到n的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前第一个用户给出了一些( N )编程,例如 5 。结果
程序必须找到最小的数字,可以是分为 N (5)。结果
和这个号码只能包含数字0和9不是任何其他数字。



例如,如果用户给出的 5 以程序。
号码,可分成5是:

  5,10,15,20,25,30日。 ..,85%,90%,95 ... 

但90这里是最小数量可以被划分为5,并且还包括数字(0,9)。所以回答5必须90.结果,
和回答9是9,因为它可以被划分至9,并且由位(9)。



我的代码

 字符串= txtNumber.Text; 
的Int64 X = Convert.ToInt64(一);
Int64类型I,J = 1,Y = X;
布尔T = FALSE;
为(I = X + 1;吨==假的,我++)
{
如果(I%9 == 0安培;&安培; I%10 == 0安培;&安培;我%×== 0)
{$ b $ = BJ我;
为((I / = 10)= 0;!)
{
I / = 10;
如果(我== 0)
T =真实的;
继续;
}
}
}
lblAnswer.Text = Convert.ToString(J);


解决方案

我写了控​​制台此代码,我用转到然而,命令它不者优先,但我不能只为它写。

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;

命名空间主要
{
类节目
{
静态无效的主要(字串[] args)
{
控制台。的WriteLine(输入你的号码);
的Int64 X = Convert.ToInt64(到Console.ReadLine());
的Int64 Y,J,I,K,Z = X;
X = X + 5;
循环:
X ++;
为(i = 0,Y = X,Y!= 0;我+ +)
Y / = 10;

为(J = X,K = 1; k = 0;!焦耳/ = 10,K--)
{
如果(!Ĵ%10 = 9)
如果(J%10!= 0)
转到循环;
}
如果
转到环路(X%Z!= 0);
Console.WriteLine(答案:{0},X);
Console.ReadKey();
}
}
}


At first user gives a number (n) to program, for example 5.
the program must find the smallest number that can be divided to n (5).
and this number can only consist of digits 0 and 9 not any other digits.

for example if user gives 5 to program. numbers that can be divided to 5 are:

5, 10, 15, 20, 25, 30, ..., 85, 90, 95, ...

but 90 here is the smallest number that can be divided to 5 and also consist of digits (0 , 9). so answer for 5 must be 90.
and answer for 9 is 9, because it can be divided to 9 and consist of digit (9).

my code

string a = txtNumber.Text;
Int64 x = Convert.ToInt64(a);
Int64 i ,j=1,y=x;
bool t = false;
for (i = x + 1; t == false; i++)
{
    if (i % 9 == 0 && i % 10 == 0 && i % x == 0)
    {
        j = i;
        for (; (i /= 10) != 0; )
        {
            i /= 10;
            if (i == 0)
                t = true;
            continue;
        }
    }
}
lblAnswer.Text = Convert.ToString(j);

解决方案

I wrote this code for console, and i used goto command however it is not prefered but i could not write it with only for.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace main
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter your number");
        Int64 x = Convert.ToInt64(Console.ReadLine());
        Int64 y, j, i, k, z = x;
        x = x + 5;
    loop:
        x++;
        for (i = 0, y = x; y != 0; i++)
            y /= 10;

        for (j = x, k = i; k != 0; j /= 10, k--)
        {
            if (j % 10 != 9)
                if (j % 10 != 0)
                    goto loop;
        }
        if (x % z != 0)
            goto loop;
        Console.WriteLine("answer:{0}",x);
        Console.ReadKey();
    }
}
}

这篇关于对于一个数整除到n的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆