第一题
#include <stdio.h>
#include <time.h>
#include <math.h>
clock_t start, stop;
double duration;
#define MAXN 10
#define MAXK 1e7
double f1(int n, double a[], double x) {
int i;
double p = a[0];
for (i = 1; i <= n; i++)
p += (a[i] * pow(x, i));
return p;
}
double f2(int n, double a[], double x) {
int i;
double p = a[n];
for(i = n; i > 0; i--)
p = a[i - 1] + x * p;
return p;
}
void run(double(*f)(int, double *, double), double a[], int case_n) {
int i;
start = clock();
for(i = 0; i < MAXK; i++)
(*f)(MAXN - 1, a, 1.1);
stop = clock();
duration = ((double)(stop - start)) / CLK_TCK / MAXK;
printf("ticks%d=%f\n", case_n, (double)(stop - start));
printf("duration%d=%6.2e\n", case_n, duration);
}
int main() {
int i;
double a[MAXN];
for(i = 0; i < MAXN; i++)
a[i] = (double)i;
run(f1, a, 1);
run(f2, a, 2);
return 0;
}
第二题
/*
* ==================================================================================
* Author: north_h
* Time: 2023-09-10 21:22:37
*
* Problem: P1115 最大子段和
* Contest: Luogu
* URL: https://www.luogu.com.cn/problem/P1115
* MemoryL: 125 MB
* TimeL: 1000 ms
* ==================================================================================
*/
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
int sum = 0;
int ans = -2e9;
for(int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
sum += x;
// printf("%d-%d\n", sum, ans);
if(ans < sum)ans = sum;
// printf("%d-%d\n", sum, ans);
if(sum < 0) {
sum = 0;
}
}
if(ans < sum && sum != 0)ans = sum;
printf("%d", ans);
return 0;
}
标签:第一周,int,double,sum,printf,start,实验,ans
From: https://www.cnblogs.com/north-h/p/17692201.html