pat a1033

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C
​max
​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D
​avg
​​ (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P
​i
​​ , the unit gas price, and D
​i
​​ (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:

749.17
Sample Input 2:

50 1300 12 2
7.10 0
7.00 600
Sample Output 2:

The maximum travel distance = 1200.00

代码
#include<stdio.h>
#include
using namespace std;
struct sta
{
double dis;
double p;
}s[500];
bool cmp(struct sta a,struct sta b)
{
return a.dis<b.dis;
}
int main()
{
bool cmp(struct sta a,struct sta b);
double c,d,v;
int n;
int tmp;
double sum=0;
double you=0;
int chufa=0;
int min_;
double min=100;
int flag=false;
scanf("%lf%lf%lf%d",&c,&d,&v,&n);
double d1=d;
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&s[i].p,&s[i].dis);
}
sort(s,s+n,cmp);
s[n].dis=d;s[n].p=0;
if(s[0].dis!=0)
{
printf(“The maximum travel distance = 0.00”);
return 0;
}
tmp=cv;
while(chufa<n)
{
min=100;
for(int i=chufa+1;i<=n&&s[i].dis-s[chufa].dis<=tmp;i++)
{
if(s[i].p<min)
{
min_=i;
min=s[i].p;
if(min<s[chufa].p)
break;
}
}
if(min==100)
break;
if(s[min_].p<=s[chufa].p)
{
if(you
v<s[min_].dis-s[chufa].dis)
{sum=sum+s[chufa].p*(s[min_].dis-s[chufa].dis-youv)/v;
chufa=min_;
you=0;
continue;}
else
{
chufa=min_;
you=you-(s[min_].dis-s[chufa].dis)/v;
continue;
}
}
if(s[min_].p>s[chufa].p)
{
sum=sum+s[chufa].p
(c-you);
you=c-(s[min_].dis-s[chufa].dis)/v;
chufa=min_;
continue;
}
}
if(chufa==n)
{
sum=sum+(d1-s[chufa].dis)/v*s[chufa].p;
printf("%.2f",sum);
}
else
printf(“The maximum travel distance = %.2f”,s[chufa].dis+tmp);
}

### 关于 PAT 乙级 1033 题目的 C 语言解决方案 #### 问题概述 PAT 乙级 1033 的题目通常涉及字符串处理和简单的算法设计。这类题目旨在帮助练习者巩固基础编程技能并提高解决实际问题的能力。 #### 解决思路 对于此类题目,核心在于理解输入输出的要求以及如何高效地实现功能需求。具体到本题,假设目标是对一段文字中的单词进行特定方式的排序或统计,则需注意以下几点: - **读入数据**:利用标准库函数`scanf()`或其他适合的方式接收用户输入的数据。 - **分割字符串**:可以采用字符数组来保存整个句子,并通过遍历找到每个单词之间的分隔符(通常是空格)。也可以考虑使用`strtok()`这样的辅助工具简化工作[^1]。 - **处理逻辑**:根据具体的业务场景决定是要对这些词项执行何种操作——比如按字典序排列、计算频率分布等。 - **输出结果**:最后按照指定格式打印最终答案。 下面给出一个基于上述流程框架下的简单实例代码用于说明可能遇到的情况之一即统计不同词语出现频次的任务: ```c #include <stdio.h> #include <string.h> #define MAX_WORDS 100 // 假设最多有100个不同的单词 #define WORD_LEN 20 // 单词长度不超过20个字母 int main() { char sentence[1000]; // 输入的一句话 scanf("%[^\n]", sentence); int wordCount[MAX_WORDS]; memset(wordCount, 0, sizeof(int)*MAX_WORDS); char words[MAX_WORDS][WORD_LEN+1]; int uniqueWords = 0; char *token; token = strtok(sentence, " "); while(token != NULL){ int found = 0; for(int i=0;i<uniqueWords && !found;++i){ if(strcmp(words[i], token)==0){ ++wordCount[i]; found = 1; } } if(!found){ strcpy(words[uniqueWords++], token); wordCount[uniqueWords-1]++; } token = strtok(NULL," "); } printf("Word Frequency:\n"); for(int j=0;j<uniqueWords;++j){ printf("\"%s\" appears %d time(s).\n",words[j],wordCount[j]); } return 0; } ``` 此段代码实现了基本的功能模块,包括但不限于初始化必要的结构体成员变量;解析命令行参数获取待分析文本串;逐一遍历该序列识别独立单元并将它们记录下来以便后续查询其重复度数;最后汇总展示所得统计数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值