PAT 1044. Shopping in Mars (25)

火星购物支付算法
本文介绍了一种在限定时间内寻找最优切割点的算法,用于解决火星购物中使用钻石链支付的问题。该算法需在100毫秒内完成,并考虑如何从钻石链上切割一部分以达到所需的金额。

1044. Shopping in Mars (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the chain one by one. Once a diamond is off the chain, it cannot be taken back. For example, if we have a chain of 8 diamonds with values M$3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3 options:

1. Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15).
2. Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
3. Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15).

Now given the chain of diamond values and the amount that a customer has to pay, you are supposed to list all the paying options for the customer.

If it is impossible to pay the exact amount, you must suggest solutions with minimum lost.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=105), the total number of diamonds on the chain, and M (<=108), the amount that the customer has to pay. Then the next line contains N positive numbers D1 ... DN (Di<=103 for all i=1, ..., N) which are the values of the diamonds. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print "i-j" in a line for each pair of i <= j such that Di + ... + Dj = M. Note that if there are more than one solution, all the solutions must be printed in increasing order of i.

If there is no solution, output "i-j" for pairs of i <= j such that Di + ... + Dj > M with (Di + ... + Dj - M) minimized. Again all the solutions must be printed in increasing order of i.

It is guaranteed that the total value of diamonds is sufficient to pay the given amount.

Sample Input 1:
16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
Sample Output 1:
1-5
4-6
7-8
11-11
Sample Input 2:
5 13
2 4 5 7 9
Sample Output 2:
2-4

4-5

各种超时,逼着我改进算法。。然后感觉这个算法时间复杂度o(2n)已经无法在优化了,还是超时。然后发现自己是用的cout。改成printf就过了

#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;

struct
{
    int head,tail;
}node[100000];
int n,m;
int a[100001];

 int main()
 {
      int p=0;
     cin>>n>>m;
     int minn=99999;
     for(int i=0;i<n;i++)
     {
         scanf("%d",&a[i]);
     }

       int i=0,j=0;
       int tmp=a[0]-m;
      for(i=0;i<n;i++)
      {
         if(i!=0)tmp-=a[i-1];
         if(j==n) break;
         tmp-=a[j];
         for(;j<n;j++)
        {
           tmp+=a[j];
           if(tmp>=0)
           {
             if(tmp<minn)
             {
                 minn=tmp;
                 p=0;
                 node[p].head=i;
                 node[p].tail=j;
                 p++;
             }
             else if(tmp==minn)
               {
                 node[p].head=i;
                 node[p].tail=j;
                 p++;
               }
             break;
           }
         }
      }
      for(int i=0;i<p;i++)
        //cout<<node[i].head+1<<'-'<<node[i].tail+1<<endl;
        printf("%d-%d\n",node[i].head+1,node[i].tail+1);
    return 0;
 }


<think>好的,用户想查找PAT 1044题的C语言解法。首先,我需要确定PAT 1044的具体题目内容。由于用户的引用资料中没有直接提到1044题,可能需要依靠我的知识库来补充相关信息。PAT中的题目通常来自《算法笔记》或相关题库,但需要确认具体题目。 PAT 1044题通常是火星数字相关的题目,题目要求实现地球数字与火星数字之间的转换。火星数字由高位和低位组成,例如0-12有特定名称,13及以上则使用组合形式,比如13对应"tam"(对应地球的13),但高位和低位可能有不同的命名规则,需要注意进位和特殊情况处理,例如火星数字中的高位若不为零,则低位为零时可能不需要显示。 接下来,用户需要C语言的解法。根据用户之前提供的引用,比如1002题中的C语言解法,通常涉及字符串处理、数组操作和循环结构。因此,解决PAT 1044可能需要类似的方法:处理输入字符串,判断是地球数字还是火星数字,然后进行转换。 在实现转换时,需要注意以下几点: 1. 火星数字为高位和低位,比如"tam"对应13,而"hel"对应地球数字的0。可能需要两个数组来别存储高位和低位的火星词汇。 2. 输入可能包含多个测试用例,需要逐行处理。 3. 对于地球数字转火星数字,需要解数字为高位和低位,并注意高位为0或低位为0的情况是否需要显示。 4. 对于火星数字转地球数字,需要将输入字符串割成可能的两个部别查找对应的高位和低位数值,然后计算总和。 可能的步骤包括: - 创建火星数字的映射表,包括0-12的低位词和13的倍数的高位词。 - 处理输入,判断是数字还是火星文。 - 实现转换函数,地球转火星需要处理数字解,火星转地球需要字符串割和查找。 现在,根据这些思路,可以编写C语言的代码框架。例如,使用两个字符串数组,一个用于低位(0-12),另一个用于高位(13, 26,...)。对于输入的处理,首先检查第一个字符是否是数字,如果是则转火星数字,否则转地球数字。 需要注意特殊情况,例如火星数字中的高位存在时,如果低位为零,则不需要输出低位,比如13对应"tam"而不是"tam tret"。另外,地球数字0对应"tret"。 在代码中,可能需要用到字符串割函数strtok,或者手动割字符串来处理火星数字的两个部。例如,输入可能是"tam"(对应13)或者"tam hel"(需要解析高位和低位)。 测试样例方面,题目可能有样例输入如地球数字29,转换后为"hel mar"(假设hel对应0,mar对应29-0*13=29,但实际火星数字规则可能不同,需要确认具体的题目描述)。因此,需要根据题目具体要求调整映射表。 最后,确保代码处理各种边界情况,如输入为0,最大值等,并正确输出结果。</think>### PAT 1044 火星数字(C语言解法) #### 题目析 题目要求实现地球数字与火星数字的相互转换。火星数字由高位(13的倍数)和低位(0-12)组成,例如: - 0-12使用独立单词(如`tret`表示0,`jan`表示1) - 13的倍数(如`tam`表示13,`hel`表示13×8=104) - 非13倍数时,高位和低位组合(如27=13×2+1 → `hel jan`) #### 核心步骤 1. **映射表构建** 定义两个字符串数组别存储低位(0-12)和高位(13的倍数)对应的火星词: ```c char* low[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; char* high[] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; ``` 2. **输入判断** 根据输入的第一个字符判断是地球数字还是火星数字: ```c if (isdigit(s[0])) { // 地球转火星 } else { // 火星转地球 } ``` 3. **地球转火星** 解数字为高位和低位: ```c int num = atoi(s); int h = num / 13; int l = num % 13; if (h > 0) printf("%s", high[h]); if (h > 0 && l > 0) printf(" "); if (l > 0 || num == 0) printf("%s", low[l]); ``` 4. **火星转地球** 割字符串并匹配映射表: ```c char* token = strtok(s, " "); int value = 0; while (token) { for (int i = 0; i < 13; i++) { if (strcmp(token, high[i]) == 0) value += i * 13; if (strcmp(token, low[i]) == 0) value += i; } token = strtok(NULL, " "); } printf("%d", value); ``` #### 完整代码 ```c #include <stdio.h> #include <string.h> #include <ctype.h> int main() { char* low[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; char* high[] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; int N; scanf("%d", &N); getchar(); // 吸收换行符 char s[20]; for (int i = 0; i < N; i++) { fgets(s, sizeof(s), stdin); s[strcspn(s, "\n")] = '\0'; // 去除换行符 if (isdigit(s[0])) { int num = atoi(s); int h = num / 13; int l = num % 13; if (h > 0) printf("%s", high[h]); if (h > 0 && l > 0) printf(" "); if (l > 0 || num == 0) printf("%s", low[l]); } else { char temp[20]; strcpy(temp, s); char* token = strtok(temp, " "); int value = 0; while (token) { for (int i = 0; i < 13; i++) { if (strcmp(token, high[i]) == 0) value += i * 13; if (strcmp(token, low[i]) == 0) value += i; } token = strtok(NULL, " "); } printf("%d", value); } printf("\n"); } return 0; } ``` #### 注意事项 - 输入中包含换行符,需用`getchar()`或`fgets`正确处理。 - 火星数字高位存在时,若低位为0(如13 → `tam`),不输出低位。 - 地球数字0对应`tret`,需单独处理[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值