Section 35 Iterative method to solve problems

1. Find the first 40 numbers in the Fibonacci sequence

求Fibonacci数列前40个数:1 1 2 3 5 8 13 21 34 ...
#include <stdio.h>
int main()
{
    
    
	long f1, f2, fn;
	int i;
	f1 = f2 = 1;
	printf("%-12ld\t%-12ld\t", f1, f2);
	for (i = 3; i <= 40; ++i)//可改成i=2,i<=39输出完全对齐;
	{
    
    
		fn = f1 + f2;//等于前两个数之和;
		f1 = f2;     //第二个数变成下一个数的数一;
		f2 = fn;     //新得的数变成下一个数的数二;
		if (i % 5 == 0) printf("\n"); //i是5的整数时,换行;
		printf("%-12ld\t", fn);//%-12ld表示输出12位左对齐;
	}
}

2. Seeking 1!+2!+3!+…+20!

#include <stdio.h>
int main()
{
    
    
	int i,f=1,sum=0;
	for (i = 1; i <= 20; ++i)
	{
    
    
		f *= i;  //迭代计算阶乘
		sum += f;
	}
	printf("%d\n", sum);
}

3. Find the series formula (accurate to 5 decimal places)

#include <stdio.h>
#include <math.h>
int main()
{
    
    
	double x, sum = 1, term = 1,i=1;
	scanf_s("%lf", &x);
	while (fabs(term)>=1e-5)//fabs()取绝对值;
	{
    
    
		term = term * (-1) * x * x / ((2 * i) * (2 * i - 1));
		//找出递增规律,利用迭代计算
		sum = sum + term;
		++i;
	}
	printf("%f\n", sum);
}

4. Practical project
[Project 1: Alternative summation]
Find the value of Sn=a+aa+aaa+...+aa...a, where a is a number.
For example, 2+22+222+2222 (at this time a=2, n=4), both a and n are input by the keyboard.

#include <stdio.h>
int main()
{
    
    
	int a, n, i = 1, Sn = 0, Tn = 0;
	printf("请输入 a 和 n 的值:\n");
	scanf_s("%d %d", &a, &n);
	while (i <= n)
	{
    
    
		Tn = Tn * 10 + a;
		Sn = Sn+Tn;
		++i;
	}
	printf("a+aa+aaa+...=%d\n", Sn);
	return 0;
}

[Item 2: Rebounding ball]
A ball falls freely from the height of 100 meters. After each landing, it bounces back to half of its original height, and then falls again. How many meters does it travel when it hits the ground for the tenth time? How high is the tenth rebound?

#include <stdio.h>
int main()
{
    
    
	float sn = 100.0, hn = sn/2;
	int n;
	for (n = 2; n <= 10; ++n)
	{
    
    
		sn = sn + hn * 2;
		hn = hn / 2;
	}
	printf("第10次落地时经过: %g 米\n", sn);
	printf("第10次落地时反弹: %g 米\n", hn);
}

[Item 3: Bessel function] The
Bessel function Jn(X) has the following recurrence relationship:
Insert picture description here
write a program, use the recurrence relationship, and find Jn(X) from any n and x≠0.

 #include <stdio.h>
#include <math.h>
int main()
{
    
    
	double jn=0, j0, j1, x;
	int n, i;
	const double PI = 3.1415926;
	scanf_s("%d %lf", &n, &x);
	j0 = sin(x * PI / 180) / x;
	j1 = sin(x * PI / 180) / (x * x) - cos(x * PI / 180) / x;
	for (i = 2; i <= n; i++)
	{
    
    
		jn = (2 * (double)i - 1) * j1 - j0;//公式中为i+1项值,转换为第i项值;
		j0 = j1;
		j1 = jn;
	}
	printf("%f\n", jn);
}

[Project 4: Rich man who is greedy for money]
A rich man meets a stranger, and the stranger talks to him about a plan to exchange money. The plan is as follows: I will give you 100,000 yuan a day, and you only need to give me one point on the first day Money, the next day I still give you one hundred thousand yuan, you give me two cents, and on the third day I still give you one hundred thousand yuan, you give me four cents..., the money you give me every day is the day before Twice, until a full month (30 days), the tycoon was very happy and gladly accepted the contract. Please program to output daily transaction records.

#include <stdio.h>
int main()
{
    
    
	double a = 0.005, b = 100000, suma = 0, sumb = 0;
	for (int i = 1; i <= 30; ++i)
	{
    
    
		a = a * 2;
		suma = suma + a;
		sumb = sumb + 100000;
		printf("第%02d天 大富翁支出%10.2f元,累计支出%10.2f\n\n", i, a, suma);
		printf("第%02d天 陌生人支出%10.2f元,累计支出%10.2f\n", i, b, sumb);
	}
}

[Project 5:
Buying a house in Beijing] Now there is a house in Beijing with a price of 2 million yuan. Assuming that housing prices increase by 10% every year, a software engineer makes a fixed net profit of 400,000 yuan per year. Output housing prices and engineer deposits for the next 10 years

#include <stdio.h>
int main()
{
    
    
	double 房价 = 200/1.1, 存款 = -40;
	int i;
	for (i = 1; i <= 10;++i )
	{
    
    
		房价 = 房价 * 1.1;
		存款 = 存款 + 40;
		printf("第%02d年 房价:%.2f万元 ,存款: %.2f万元\n", i, 房价, 存款);
	}
}

Guess you like

Origin blog.csdn.net/m0_51439429/article/details/115256395