算法编程实战

发布时间:2024-12-24 17:06

项目实战能提升编程实战经验 #生活知识# #生活经验# #编程#

eg2.写一个函数printN,打印出从1到N

#include<stdio.h>

void printN1(int n);

void printN2(int n);

void printN2(int n){

if(n){

printN2(n-1);

printf("%-4d ", n);

}

}

void printN1(int n){

int i;

for(i = 0; i < n; i++){

printf("%-4d ", i+1);

}

}

void main(void){

int n;

printf("请输入一个正整数:");

scanf("%d", &n);

printN1(n);

printN2(n);

}

eg3.写程序计算给定多项式在给定点x处的值

#include<stdio.h>

#include<math.h>

#define N 3

double fun1(double a[], int n, double x);

double fun2(double a[], int n, double x);

double fun2(double a[], int n, double x){

double y = a[n];

int i;

for(i = n; i > 0; i--){

y = a[i-1]+ y*x;

}

return y;

}

double fun1(double a[], int n, double x){

double y = 0;

int i;

for(i = 0; i <= n; i++){

y += a[i]*pow(x, i);

}

return y;

}

void main(void){

double a[N+1];

int i;

double x;

printf("请输入多项式的系数(次幂逐渐增大,最高次幂为%d):", N);

for(i = 0; i < N+1; i++){

scanf("%lf", &a[i]);

}

printf("请输入x的值:");

scanf("%lf", &x);

printf("fun1函数在点%lf处的值为:%lf\n", x, fun1(a, N, x));

printf("fun2函数在点%lf处的值为:%lf\n", x, fun2(a, N, x));

}

最大子列和问题(遗留,分而治之)

#include<stdio.h>

#define N 8

void inputData(int a[], int n);

int maxSubseqSum1(int a[], int n);

int maxSubseqSum2(int a[], int n);

int maxSubseqSum2(int a[], int n);

int maxSubseqSum2(int a[], int n){

int i;

int j;

int k;

int sum;

int max = 0;

for(i = 0; i < n; i++){

sum = 0;

for(j = i; j < n; j++){

sum+=a[j];

if(sum > max){

max = sum;

}

}

}

return max;

}

int maxSubseqSum1(int a[], int n){

int i;

int j;

int k;

int sum;

int max = 0;

for(i = 0; i < n; i++){

for(j = i; j < n; j++){

sum = 0;

for(k = i; k < j; k++){

sum+=a[k];

}

if(sum > max){

max = sum;

}

}

}

return max;

}

void inputData(int a[], int n){

int i;

printf("请输入%d个整型数据:", N);

for(i = 0; i < n; i++){

scanf("%d", &a[i]);

}

}

void main(void){

int a[N];

inputData(a, N);

printf("最大子列和为:%d\n", maxSubseqSum1(a, N));

printf("最大子列和为:%d\n", maxSubseqSum2(a, N));

}

网址:算法编程实战 https://www.yuejiaxmz.com/news/view/554529

相关内容

解锁编程高手秘籍:掌握高效算法,轻松解决编程难题
PHP算法实战
BAT大牛亲授 个性化推荐算法实战
Python实现简单算法乘法:提升编程效率与逻辑思维
揭秘高效学习:验算法背后的奥秘与实战技巧
Python实现经典还钱问题算法:优化财务管理的编程技巧
深度解码:Android源码与驱动编程实战揭秘
PIC16F887 实战编程 单片机编程 基础实验教程
机器学习实战之数回归,CART算法
Golang实现高效个性化推荐系统:从算法到实战应用

随便看看