本文概述
- C ++
- Java
- Python3
- C#
- 的PHP
【算法题(如何用一个数字替换另一个数字())】例子:
Input : x = 645, d1 = 6, d2 = 5
Output : 545
We replace digit 6 with 5 in number 645.Input: x = 746, d1 = 7, d2 = 8
Output : 846
我们遍历x的所有数字。对于每个数字, 我们检查它是否为d1, 我们相应地更新结果。
C ++
//CPP program to replace a digit with other
//in a given number.
#include <
bits/stdc++.h>
using namespace std;
int replaceDigit( int x, int d1, int d2)
{
int result = 0, multiply = 1;
while (x /10>
0)
{
//Take remainder of number starting from
//the unit place digit
int remainder = x % 10;
//check whether it is equal to the digit
//to be replaced.if yes then replace
if (remainder == d1)
result = result + d2 * multiply;
else //else remain as such
result = result + remainder * multiply;
//Update and move forward from unit place
//to hundred place and so on.
multiply *= 10;
x = x /10;
//update the value
}
//check whether it is equal to the digit
//to be replaced.if yes then replace
if (x == d1)
result = result + d2 * multiply;
else //else remain as such
result = result + x * multiply;
return result;
}
//Driver code
int main()
{
int x = 645, d1 = 6, d2 = 5;
cout <
<
replaceDigit(x, d1, d2) <
<
endl;
return 0;
}
Java
//Java program to replace a digit
//with other in a given number.
class GFG
{
static int replaceDigit( int x, int d1, int d2)
{
int result = 0 , multiply = 1 ;
while (x /10>
0 )
{
//Take remainder of number
//starting from the unit
//place digit
int remainder = x % 10 ;
//check whether it is equal
//to the digit to be replaced.
//if yes then replace
if (remainder == d1)
result = result + d2 * multiply;
else //else remain as such
result = result + remainder * multiply;
//Update and move forward
//from unit place to
//hundred place and so on.
multiply *= 10 ;
x = x /10 ;
//update the value
}
//check whether it is equal to the digit
//to be replaced.if yes then replace
if (x == d1)
result = result + d2 * multiply;
else //else remain as such
result = result + x * multiply;
return result;
}
//Driver code
public static void main(String[] args)
{
int x = 645 , d1 = 6 , d2 = 5 ;
System.out.println(replaceDigit(x, d1, d2));
}
}
//This Code is Contributed by mits
Python3
# Python3 program to replace
# a digit with other
# in a given number.
def replaceDigit(x, d1, d2):
result = 0
multiply = 1
while (x //10>
0 ):
# Take remainder of number
# starting from the unit
# place digit
remainder = x % 10
# check whether it is
# equal to the digit
# to be replaced.if yes
# then replace
if (remainder = = d1):
result = (result + d2 *
multiply)
else :# else remain as such
result = (result + remainder *
multiply)
# Update and move forward
# from unit place to hundred
# place and so on.
multiply * = 10
x = int (x /10 )# update the value
# check whether it is equal to the digit
# to be replaced.if yes then replace
if (x = = d1):
result = result + d2 * multiply
else :# else remain as such
result = result + x * multiply
return result
# Driver code
x = 645
d1 = 6
d2 = 5
print (replaceDigit(x, d1, d2))
# This Code is contributed
# by mits
C#
//C# program to replace a digit
//with other in a given number
using System;
class GFG
{
static int replaceDigit( int x, int d1, int d2)
{
int result = 0, multiply = 1;
while (x /10>
0)
{
//Take remainder of number
//starting from the unit
//place digit
int remainder = x % 10;
//check whether it is equal
//to the digit to be replaced.
//if yes then replace
if (remainder == d1)
result = result + d2 * multiply;
else //else remain as such
result = result + remainder * multiply;
//Update and move forward
//from unit place to
//hundred place and so on.
multiply *= 10;
x = x /10;
//update the value
}
//check whether it is equal to the digit
//to be replaced.if yes then replace
if (x == d1)
result = result + d2 * multiply;
else //else remain as such
result = result + x * multiply;
return result;
}
//Driver code
public static void Main()
{
int x = 645, d1 = 6, d2 = 5;
Console.WriteLine(replaceDigit(x, d1, d2));
}
}
//This Code is contributed
//by inder_verma
的PHP
<
?php
//PHP program to replace
//a digit with other
//in a given number.
function replaceDigit( $x , $d1 , $d2 )
{
$result = 0;
$multiply = 1;
while ( $x /10>
0)
{
//Take remainder of number
//starting from the unit
//place digit
$remainder = $x % 10;
//check whether it is
//equal to the digit
//to be replaced.if yes
//then replace
if ( $remainder == $d1 )
$result = $result + $d2 *
$multiply ;
else //else remain as such
$result = $result + $remainder *
$multiply ;
//Update and move forward
//from unit place to hundred
//place and so on.
$multiply *= 10;
$x = $x /10;
//update the value
}//check whether it is equal to the digit
//to be replaced.if yes then replace
if ( $x == $d1 )
$result = $result + $d2 * $multiply ;
else //else remain as such
$result = $result + $x * $multiply ;
return $result ;
}
//Driver code
$x = 645;
$d1 = 6;
$d2 = 5;
echo replaceDigit( $x , $d1 , $d2 );
//This Code is contributed
//by inder_verma
?>
输出如下:
545
推荐阅读
- 如何在不使用循环的情况下打印1到100之间的数字(|S2)
- Floyd Warshall算法原理和实现|DP-16
- 用Python打印列表(4种不同方式)
- 8086微处理器中的寻址模式
- 让老系统换新装 WindowsXP用上Windows7的任务栏
- XP提高网速的隐秘武器:只需要一个协议
- 健康运用U盘的一些建议
- 对于Windows下JSP开发环境的设置方案
- 与Windows服务的首次亲密接触