计蒜客T1666 顺丰快递 (C语言实现)

【题目描述】顺丰快递的原理就是利用每个时刻的风向来运送货物,这样可以做到节能减排。现在已知起点和终点的坐标,以及接下来n个时刻的风向(东南西北),每次可以选择顺风偏移1各单位或者停在原地。求到达终点的最少时间。
【输入格式】第一行两个正整数x1,y1,表示小明所在位置。第二行两个正整数x2,y2,表示小明想去的位置。第三行一个整数n,表示n个时刻。第四行第第n+3行,每行一个字符,表示风向,即东南西北的英文单词的首字母。东:East,南:South,西:West,北:North。
【输出格式】一个整数,最少走多少步,如果无法偏移至终点,输出-1。
【C语言实现】

# include <stdio.h>
int main()
{
    int x1, y1, x2, y2, n;
    scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n); // 按题意要求输入
    getchar(); // 读走换行符
    int dig = 0; // 变量dig记录走了多少步
    for (int i = 0; i < n; ++i) { // n个时刻循环
        char ch;
        ch = getchar(); // 读入风向
        if (x1 > x2 && ch == 'W') { // 西风,终点位置的横坐标在当前位置的左侧
            ++dig; // 步数加1
            --x1; // 向左移动x1
        } else if (x1 < x2 && ch == 'E') { // 东风,终点位置的横坐标在当前位置的右侧
            ++dig; // 步数加1
            ++x1; // 向右移动x1
        } else if (y1 < y2 && ch == 'N') { // 北风,终点位置的纵坐标在当前位置的上方
            ++dig; // 步数加1
            ++y1; // 向上移动y1
        } else if (y1 > y2 && ch == 'S') { // 南风,终点位置的纵坐标在当前位置的下方
            ++dig; // 步数加1
            --y1; // 向下移动y1
        }
        getchar(); // 读走换行符
    }
    if (x1 == x2 && y1 == y2) { // 到达终点位置,输出步数
        printf("%d", dig);
    } else { // 未到终点位置,输出-1
        printf("-1");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现快递面单自动填写,需要用到C语言的字符串处理、文件读写、网络通信等技术。下面是一个简单的示例代码,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> // 定义收件人信息结构体 typedef struct { char name[50]; char address[100]; char phone[20]; } Recipient; // 根据收件人地址确定快递公司和快递方式 void get_shipping_info(const char* address, char* company, char* method) { // TODO: 根据地址解析出快递公司和快递方式 strcpy(company, "顺丰速运"); strcpy(method, "顺丰标快"); } // 将收件人信息填写到面单中 void fill_out_form(const Recipient* recipient, const char* company, const char* method, const char* filename) { // 打开面单文件 FILE* fp = fopen(filename, "w"); if (fp == NULL) { fprintf(stderr, "Failed to open file %s\n", filename); return; } // 将收件人信息填写到面单中 fprintf(fp, "收件人姓名:%s\n", recipient->name); fprintf(fp, "收件人地址:%s\n", recipient->address); fprintf(fp, "收件人电话:%s\n", recipient->phone); fprintf(fp, "快递公司:%s\n", company); fprintf(fp, "快递方式:%s\n", method); // 关闭文件 fclose(fp); } // 发送面单信息到快递公司 void send_form_to_shipping_company(const char* filename, const char* url) { // 打开面单文件 FILE* fp = fopen(filename, "r"); if (fp == NULL) { fprintf(stderr, "Failed to open file %s\n", filename); return; } // 读取面单内容 char form_data[1024]; fgets(form_data, sizeof(form_data), fp); // 关闭文件 fclose(fp); // 发送POST请求到快递公司 CURL* curl = curl_easy_init(); if (curl == NULL) { fprintf(stderr, "Failed to initialize curl\n"); return; } curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, form_data); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "Failed to send form data\n"); } curl_easy_cleanup(curl); } int main() { // 定义收件人信息 Recipient recipient = { .name = "张三", .address = "广东省深圳市南山区xxx街道xxx号", .phone = "13812345678" }; // 确定快递公司和快递方式 char company[50], method[50]; get_shipping_info(recipient.address, company, method); // 将收件人信息填写到面单中 fill_out_form(&recipient, company, method, "shipping_form.txt"); // 发送面单信息到快递公司 send_form_to_shipping_company("shipping_form.txt", "http://example.com/shipping"); return 0; } ``` 上述代码中,我们首先定义了一个收件人信息结构体,包括姓名、地址和电话。然后通过`get_shipping_info`函数确定快递公司和快递方式,将收件人信息和快递信息填写到面单中,最后通过`send_form_to_shipping_company`函数将面单信息发送到快递公司。需要注意的是,这里使用了libcurl库实现了网络通信。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值