Code:
- /* Give a positive integer with the following requirements:
- 1. Find the number of digits;
- 2. output each digit separately;
- 3. output the numbers in reverse order,
- */
- # Include <stdio. h>
- # Include <math. h>
- Void main ()
- {
- Int A, B, C, D, I = 0;
- Int sum = 0;
- Int * P = new int [];
- Printf ("enter a number :");
- Scanf ("% d", & );
- // Determine the number of digits;
- Do {
- I ++;
- P [I] = A % 10;
- A = A/10;
- } While (! = 0 );
- // Output the value in the dynamic array
- D = I;
- While (D! = 0 ){
- Printf ("% d,", P [d]);
- D --;
- }
- Printf ("the number of digits is % d/N", I );
- // Print this value using the reverse string. Use the POW function POW (x, y) --> Y of X;
- For (C = 1; C <= I; C ++ ){
- B = POW (10, C-1 );
- Sum = B * P [c] + sum;
- Printf ("% d/N", sum );
- }
- Delete [] P;
- }
Here I think of the dynamic array method to save the result of each remainder operation. It seems like a pointer also works. I don't have to remember it for a long time!
The method for attaching a dynamic array is as follows:
Int main ()
{
// Use the pointer P to point to the new dynamically allocated memory space with the length Len * sizeof (INT)
Int * P = new int []; // if it is C ++, the system will warn that no array length is specified.
...........
Delete [] P;
Return 0;
}