Loop-10. Calculate the sum of the First N items of the sequence,-10

Source: Internet
Author: User

Loop-10. Calculate the sum of the First N items of the sequence,-10

1/* 2 * Main. c 3 * C10-loop-10. evaluate the N items before the sequence and 4 * Created on: July 30, 2014 5 * Author: in the Boomkeeper 6 ******** section, ******** 7 */8 9 # include <stdio. h> 10 11 int main (void) {12 13 double sum = 0.0; // records the first N items and 14 int numerator = 2, denominator = 1; // molecular denominator 15 int N; // N16 int I in the question; // use 17 int tem = 0; 18 19 scanf ("% d", & N) cyclically ); 20 21 if (N = 1) 22 printf ("%. 2f \ n ", (double) numerator/denominator); 23 else {24 sum = (double) numerator/denominator; 25 for (I = N; I> 1; I --) {26 tem = denominator; 27 denominator = numerator; 28 numerator = numerator + tem; 29 sum + = (double) numerator/denominator; 30} 31 printf ("%. 2f \ n ", sum); 32} 33 34 return 0; 35}

Not perfect...

 

Question link:

Http://pat.zju.edu.cn/contests/basic-programming/%E5%BE%AA%E7%8E%AF-10

,

,

,


Sequence and -- Recursive C Language

# Include <stdio. h>

Float xulie (int x );

Void main ()
{
Int n;
Scanf ("% d", & n );
Printf ("% f", xulie (n ));

}

Float xulie (int x)
{
If (x = 1)
Return 1;
Else
Return (x % 2 = 0 )? Xulie (x-1) + 1.0/x: xulie (x-1)-1.0/x;
}

The first 10 items of the Series: 2/1, 3/2, 5/3, 8/5, And. Use C language to write programs.

Is it necessary to use a large array ~
In the next version: Correct the version ~

# Include <stdio. h>

Void main ()
{
Int a = 1, B = 2, c, I;
Double sum = 2;
For (I = 1; I <10; I ++)
{
C = a + B, a = B, B = c;
Sum + = double (B)/;
}
Printf ("the top 10 columns are: % f \ n", sum );
}
Result: 16.479906 ~

Will no array overflow error occur in the first answer ?~ D [10] what is it? It is used without initialization, and a division by zero error occurs ~

The second obvious misunderstanding of the meaning of the landlord ~ In addition, the number of columns must be accumulated to the tenth item, and the integer column must be counted as the fifth item ~

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.