Small algorithm: put an integer in an array in reverse order (recursive implementation is required), and an array in reverse order
static void Main(string[] args) { int m = 1236578; int[] ms = new int[m.ToString().Length]; Rev(m.ToString().Length, m, ref ms); for (int i = 0; i < m.ToString().Length; i++) Console.Write("{0}", ms[i]); Console.ReadLine(); } private static void Rev(int len, int m, ref int[] ms) { if (m / 10 > 0) { ms[len - m.ToString().Length] = m % 10; Rev(len, m / 10, ref ms); return; } ms[len - m.ToString().Length] = m % 10; }
Sort a positive integer into a one-character array in reverse order (Recursive Implementation)
# Include <stdio. h>
# Include <string. h>
Void fun (int n, char a [])
{
Static int I = 0;
If (n <10) a [I ++] = n + '0 ';
Else
{A [I ++] = n % 10 + '0 ';
Fun (n/10, );
}
}
Void main ()
{
Int x;
Char a [10];
Memset (a, 0, 10 );
Scanf ("% d", & x );
Fun (x, );
For (int I = 0; I <10; ++ I)
Printf ("% c", a [I]);
}
C language Question 1: write two functions, and input integers are output in reverse order in the form of strings. They must be implemented using Recursive Algorithms and non-recursive algorithms respectively.
Write the first
# Include "stdio. h"
Main ()
{
Int n, m;
Int sum = 0;
Printf ("input a number: \ n ");
Scanf ("% d", & n );
While (n/10! = 0)
{
M = n % 10;
Sum = sum + m;
N = n/10;
}
Sum = sum + n;
Printf ("sum is: % d", sum );
Getch ();
}