Abbiamo bisogno del tuo supporto per diffondere le nostre sezioni sull'informatica.
Esempio: programma per trovare il massimo numero inserendo 5 in java
usingSystem;namespaceMyApp{classProgram{staticvoidMain(string[] args){string str ="Maximum possible number for {0} after inserting {1} is: {2}";
Console.WriteLine(string.Format(str,276,3,MaximumPossible(276,3)));
Console.WriteLine(string.Format(str,-999,4,MaximumPossible(-999,4)));
Console.WriteLine(string.Format(str,0,3,MaximumPossible(0,3)));
Console.WriteLine(string.Format(str,860,7,MaximumPossible(860,7)));
Console.ReadKey();}/// <summary>/// method to get the maximum possible number obtained by inserting given digit /// at any position in the given number./// summary>/// <paramname="num">given numberparam>/// <paramname="digit">digit to insert in given numberparam>/// <returns>possible maximum number after inserting given digit at any positionreturns>staticintMaximumPossible(int num,int digit){// edge caseif(num ==0){return digit *10;}// -1 if num is negative number else 1int negative = num / Math.Abs(num);// get the absolute value of given number
num = Math.Abs(num);int n = num;// maximum number obtained after inserting digit.int maxVal =int.MinValue;int counter =0;int position =1;// count the number of digits in the given number.while(n >0){
counter++;
n = n /10;}// loop to place digit at every possible position in the number,// and check the obtained value.for(int i =0; i <= counter; i++){int newVal =((num / position)*(position *10))+(digit * position)+(num % position);// if new value is greater the maxValif(newVal * negative > maxVal){
maxVal = newVal * negative;}
position = position *10;}return maxVal;}}}
Alla fine di questo articolo puoi trovare le spiegazioni di altri creatori, puoi anche mostrare il tuo se ti piace.