#include <stdio.h> #include <math.h> typedef struct dot { int no,x,y; }DOT; float calculate(DOT a,DOT b,DOT c) { return 1.0*fabs((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x))/2; } void seek(DOT *p,DOT *s,DOT *q,float *m,int n) { float B=0; if(p->no<n-1) if(s->no<n) if(q->no) { B=calculate(*p,*s,*q); if(*m<B) *m=B;q++;seek(p,s,q,m,n); } else { s++;seek(p,s,s+1,m,n);} else { p++;seek(p,p+1,p+2,m,n);} } main() { int t,n,i;static DOT d[100];float A; scanf("%d",&t); while(t--) { A=0;scanf("%d",&n); for(i=0;i<n;i++) { d[i].no=i+1;scanf("%d %d",&d[i].x,&d[i].y);} seek(d,&d[1],&d[2],&A,n); printf("%.1f\n",A); } }
时间: 2023-06-24 12:06:07 浏览: 135
这是一个计算凸多边形面积的程序,使用的是求三角形面积的公式。程序的主要思路是通过递归的方式,不断寻找凸多边形的顶点,然后计算以该顶点为顶点的所有三角形的面积,最终得到凸多边形的总面积。
具体实现过程如下:
1. 定义结构体 DOT,表示一个点的坐标和编号。
2. 定义函数 calculate,计算三角形面积。公式为:S = 1/2 * |(x2-x1)*(y3-y1)-(y2-y1)*(x3-x1)|。
3. 定义函数 seek,递归寻找凸多边形的顶点。首先计算当前三角形的面积,如果大于之前计算出的最大面积,则更新最大面积。然后,如果 q 不为NULL,则将 q 后移一位,继续递归;否则,如果 s 不为NULL,则将 s 后移一位,并将 q 指向 s+1,继续递归;否则,如果 p 不为NULL,则将 p 后移一位,并将 s 指向 p+1,将 q 指向 p+2,继续递归。
4. 在主函数中,读入测试数据,并调用 seek 函数计算凸多边形的面积。
需要注意的是,程序中的结构体和函数名都是小写字母,这在 C 语言中是不规范的。正确的命名应该是 Dot 和 calculate,以便与其他变量和函数区分开来。
相关问题
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <conio.h> double deal_exp(char *exp) { } int main() { char expression[1024]; double result; printf("请输入一个算术表达式:(e退出)\n"); gets(expression); while(expression[0] != 'e') { result = deal_exp(expression); printf("%f\n", result); printf("请输入一个算术表达式:(e退出)\n"); gets(expression); } return 0; } 编写double deal_exp(char *exp)函数实现算术表达式的求值
这里提供一种基于栈的算法来实现算术表达式的求值。
具体实现步骤如下:
1. 定义一个栈来存储操作数和运算符,以及一个指针用来指向栈顶。
2. 遍历表达式中的每个字符,如果是数字则将其转换成实数并入栈;如果是运算符则将其与栈顶的运算符进行比较,如果优先级更高则入栈,否则弹出栈顶的运算符和两个操作数进行计算,并将结果入栈。
3. 最终栈中只剩下一个元素,即为表达式的计算结果。
下面是代码实现:
```c
typedef struct {
double num; // 操作数
char op; // 运算符
} Element;
Element stack[1024]; // 栈
int top = -1; // 栈顶指针
void push(Element e) {
stack[++top] = e;
}
Element pop() {
return stack[top--];
}
Element top_element() {
return stack[top];
}
int is_empty() {
return top == -1;
}
int is_operator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int priority(char op) {
if (op == '+' || op == '-') {
return 1;
} else if (op == '*' || op == '/') {
return 2;
} else {
return 0;
}
}
double calculate(double a, double b, char op) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
default: return 0.0;
}
}
double deal_exp(char *exp) {
top = -1;
int i = 0;
while (exp[i] != '\0') {
if (exp[i] == ' ') {
i++;
continue;
}
if (isdigit(exp[i])) {
double num = 0.0;
while (isdigit(exp[i])) {
num = num * 10 + (exp[i] - '0');
i++;
}
if (exp[i] == '.') {
i++;
double frac = 0.1;
while (isdigit(exp[i])) {
num += frac * (exp[i] - '0');
frac /= 10;
i++;
}
}
Element e = {num, '\0'};
push(e);
} else if (is_operator(exp[i])) {
while (!is_empty() && priority(top_element().op) >= priority(exp[i])) {
Element e2 = pop();
Element e1 = pop();
double res = calculate(e1.num, e2.num, e2.op);
Element e = {res, '\0'};
push(e);
}
Element e = {0.0, exp[i]};
push(e);
i++;
} else {
printf("表达式中含有非法字符!\n");
return 0.0;
}
}
while (!is_empty()) {
Element e2 = pop();
if (e2.op != '\0') {
Element e1 = pop();
double res = calculate(e1.num, e2.num, e2.op);
Element e = {res, '\0'};
push(e);
} else {
return e2.num;
}
}
return 0.0;
}
```
注意,这里实现的算法只支持数字、四则运算和小数点,如果表达式中含有其他字符则会提示非法字符。同时,由于浮点数运算存在误差,可能会出现计算结果不精确的情况。
#include <stdio.h> #include <stdbool.h> #include <limits.h> #define MAX_JOBS 10 // 定义作业结构 typedef struct { int id; // 作业ID int duration; // 作业持续时间 } Job; int bestSchedule[MAX_JOBS]; // 存储最佳调度顺序 int bestTime = INT_MAX; // 最佳调度时间 Job currentSchedule[MAX_JOBS]; // 当前调度顺序 int numJobs; // 作业数量 // 计算当前调度时间 int calculateTime() { int time = 0; for (int i = 0; i < numJobs; i++) { time += currentSchedule[i].duration; } return time; } // 回溯函数 void backtrack(int level, Job jobs[]) { if (level == numJobs) { int time = calculateTime(); if (time < bestTime) { bestTime = time; for (int i = 0; i < numJobs; i++) { bestSchedule[i] = currentSchedule[i].id; } } return; } for (int i = 0; i < numJobs; i++) { if (currentSchedule[i].id == 0) { currentSchedule[i] = jobs[level]; backtrack(level + 1, jobs); currentSchedule[i].id = 0; } } } int main() { printf("Enter the number of jobs: "); scanf("%d", &numJobs); Job jobs[MAX_JOBS]; printf("Enter the duration of each job:\n"); for (int i = 0; i < numJobs; i++) { printf("Job %d: ", i + 1); scanf("%d", &jobs[i].duration); jobs[i].id = i + 1; } backtrack(0, jobs); printf("\nBest Schedule: "); for (int i = 0;
这段代码是一个批处理作业调度的程序。它回溯算法来计算最佳的作业调度顺序,以使总执行时间最小。
代码中定义了一个作业结构体,包含作业ID和持续时间。还定义了一个全局变量bestSchedule用于存储最佳调度顺序,bestTime用于存储最佳调度时间,currentSchedule用于存储当前调度顺序,numJobs用于记录作业数量。
主要的回溯函数是backtrack,它采用递归的方式进行调用。在每一层递归中,它尝试将下一个作业安排到当前可用的位置,并继续递归调用下一层。当递归到达叶子节点时,即所有作业都安排完毕,会计算当前调度的总执行时间,并与最佳调度进行比较更新。
在主函数中,首先读取输入的作业数量和每个作业的持续时间。然后调用backtrack函数进行计算,并输出最佳调度顺序。
请问有什么问题我可以帮助您解答的吗?
阅读全文
相关推荐

















