Small algorithm: put an integer in an array in reverse order (recursive implementation is required), and an array in reverse order

Source: Internet
Author: User

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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.