One algorithm question per day-simple-recursion-calculation A + AA + AAA +... + AAAA... Value of

Source: Internet
Author: User

Example taken from <C language example development Daquan>

Question: calculate a + AA + AAA +... + Aaaa... A (x A), where the values of a and X are input from the keyboard, for example, input a = 1, x = 3, equivalent to 1 + 11 + 111

Note:

1. Find the implementation rule of AAAA.

2. Find the Implementation of A + AA + AAA.

Package COM. jue. addition; public class testaddition {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubsystem. out. println (getdatasum (2, 5);}/*** calculation result, for example: result of 2 + 22 + 222 + 2222 + 22222 */static int getdatasum (int A, int X) {int sum = 0; For (INT I = 1; I <= x; I ++) {sum + = getdata (A, I) ;}return sum ;}/ *** calculates a number composed of X lengths of, for example, the number composed of five 2 is 22222 */Private Static int getdata (int A, int X) {int sum = 0; For (INT I = 1; I <= X; I ++) {sum + = A * getmultiplier (I);} return sum;}/*** get a multiple, for example, in 2*1000 = 2000, 1000 is the multiple ** @ Param x * @ return multiple */Private Static int getmultiplier (int x) {If (x = 0) {return 0 ;}if (x = 1) {return 1 ;}return 10 * getmultiplier (X-1 );}}

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.