double f(double x, int n) {
double result = 0
int sign = 1
for (int i = 0i <ni++) {
result += sign * x
x *= x
sign = -sign
}
return result
}
// 使用递归的 Rf 函数
double Rf(double x, int n) {
if (n == 0) {
return 0
}
return x - Rf(x * x, n - 1)
}
int main() {
double x
int n
for (int i = 0i <3i++) {
printf("请输入 x 和 n 的值:")
scanf("%lf%d", &x, &n)
printf("f(x, n) = %lf\n", f(x, n))
printf("Rf(x, n) = %lf\n", Rf(x, n))
}
return 0
}
这段程序中,f 函数使用了一个循环来计算函数值,而 Rf 函数使用了递归。 main 函数提示用户输入 x 和 n 的值,并调用 f 和 Rf 函数计算函数值,最后将结果输出到屏幕上。
题目1#include "stdio.h"
void main(void)
{
int n=5,m=10,i=1
long sum=1
for(i<=ni++)
{
sum*=i
}
printf("\n5!=%d",sum)
for(i=1i<=10i++)
{
sum*=i
}
printf("\n10!=%d",sum)
}
题目2
#include "stdio.h"
#include "string.h"
struct Student
{
char s_Name[25]
long n_Code
int n_English
int n_Math
int n_Computer
}student_1,student_2
void main(void)
{
printf("\nStudent1:\nName:")
scanf("%s",&student_1.s_Name)
printf("StudentNum:")
scanf("%d",&student_1.n_Code)
printf("English Score:")
scanf("%d",&student_1.n_English)
printf("Math Score:")
scanf("%d",&student_1.n_Math)
printf("Computer Score:")
scanf("%d",&student_1.n_Computer)
printf("\nStudent2:\nName:")
scanf("%s",&student_2.s_Name)
printf("StudentNum:")
scanf("%d",&student_2.n_Code)
printf("English Score:")
scanf("%d",&student_2.n_English)
printf("Math Score:")
scanf("%d",&student_2.n_Math)
printf("Computer Score:")
scanf("%d",&student_2.n_Computer)
printf("\nStudent1:\nName:%s\nStudent Number:%d\nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\n",student_1.s_Name,student_1.n_Code,student_1.n_English,student_1.n_Math,student_1.n_Computer)
printf("\nStudent2:\nName:%s\nStudent Number:%d\nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\n",student_2.s_Name,student_2.n_Code,student_2.n_English,student_2.n_Math,student_2.n_Computer)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)