PAT-A1129 Recommendation System 题目内容及题解

Recommendation system predicts the preference that a user would give to an item. Now you are asked to program a very simple recommendation system that rates the user's preference by the number of times that an item has been accessed by this user.

Input Specification:

Each input file contains one test case. For each test case, the first line contains two positive integers: N (≤ 50,000), the total number of queries, and K (≤ 10), the maximum number of recommendations the system must show to the user. Then given in the second line are the indices of items that the user is accessing -- for the sake of simplicity, all the items are indexed from 1 to N. All the numbers in a line are separated by a space.

Output Specification:

For each case, process the queries one by one. Output the recommendations for each query in a line in the format:

query: rec[1] rec[2] ... rec[K]

where query is the item that the user is accessing, and rec[i] (i=1, ... K) is the i-th item that the system recommends to the user. The first K items that have been accessed most frequently are supposed to be recommended in non-increasing order of their frequencies. If there is a tie, the items will be ordered by their indices in increasing order.

Note: there is no output for the first item since it is impossible to give any recommendation at the time. It is guaranteed to have the output for at least one query.

Sample Input:

12 3
3 5 7 5 5 3 2 1 8 3 8 12

Sample Output:

5: 3
7: 3 5
5: 3 5 7
5: 5 3 7
3: 5 3 7
2: 5 3 7
1: 5 3 2
8: 5 3 1
3: 5 3 1
8: 3 5 1
12: 3 5 8

题目大意

题目给定一个访问序列,并要求根据序列和推荐信息编写一个非常简单的推荐系统,该系统根据该用户访问某个条目的次数从第二个选项开始对该用户的待选项进行排序,并在每次搜索后输出待选项信息。

解题思路

  1. 用结构体储存节点和推荐列表;
  2. 用book和inq数组记录信息;
  3. 读入第一个记录并将其加入队列;
  4. 从第二个记录开始执行循环:输出当前队列信息,更新book数组,判断该新纪录是否在推荐列表内后用不同方式通过book数组的内容更新推荐列表内的信息,并重新排序;如果当前推荐列表内容大于规定值,则末尾元素出队并标记;
  5. 执行结束后返回零值。

代码

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define maxn 50010

struct Node{
    int data,frequency;
};

bool cmp(Node a,Node b){
    if(a.frequency!=b.frequency){
        return a.frequency>b.frequency;
    }else{
        return a.data<b.data;
    }
}
vector<Node> seq;
int N,K;
int book[maxn],inq[maxn];

void Insert(int v){
    int i,num=0;
    Node temp;
    printf("%d:",v);
    for(i=0;i<seq.size()&&i<K;i++){
        printf(" %d",seq[i].data);
    }
    printf("\n");
    //print
    book[v]++;
    if(inq[v]==0){//不在队内 
        temp.data=v;
        temp.frequency=book[v];
        seq.push_back(temp);
        inq[v]=1;
    }else{
        for(i=0;i<seq.size();i++){
            if(seq[i].data==v){
                seq[i].frequency=book[v];
            }
        }
    }
    sort(seq.begin(),seq.end(),cmp);
    if(seq.size()>K){
        inq[seq[K].data]=0;
        seq.pop_back();
    }
}

int main(){
    int a;
    Node temp;
    scanf("%d%d",&N,&K);
    N--;
    scanf("%d",&a);
    temp.data=a;
    book[a]++;
    temp.frequency=book[a];
    seq.push_back(temp);
    inq[a]=1;
    while(N--){
        scanf("%d",&a);
        Insert(a);
    }
    return 0;
}

运行结果

 

<think>好的,我现在需要帮助用户解决他们遇到的Vue项目启动错误。首先,用户看到的错误信息是“Cannot find module 'vue-template-compiler'”,这通常意味着这个依赖包没有安装或者版本不匹配。让我仔细分析一下可能的原因和解决方法。 首先,我应该确认用户是否安装了vue-template-compiler。这个包是Vue项目用来编译单文件组件(.vue文件)的,必须和vue的版本保持一致。如果用户是通过npm或yarn安装依赖,可能漏掉了这个包,或者版本不对。 接下来,用户使用的路径中有@vue/vue-loader-v15,这说明他们可能在使用Vue Loader v15,而Vue 2.x对应的vue-template-compiler版本需要与Vue本身版本一致。例如,如果vue是2.6.x,那么vue-template-compiler也必须是2.6.x。如果版本不匹配,就会导致这种错误。 可能的解决步骤包括: 1. 检查package.json中vue和vue-template-compiler的版本是否一致。如果不一致,需要手动调整并重新安装。 2. 如果这两个包没有被安装,需要安装它们。特别是vue-template-compiler可能被遗漏。 3. 清除node_modules和package-lock.json(或yarn.lock),然后重新安装依赖,确保所有包正确安装。 4. 如果用户升级了Vue CLI,可能需要检查Vue Loader的兼容性,或者是否存在全局安装的旧版本CLI导致冲突。 另外,用户的项目路径中有“Java”文件夹,这可能是个误会,因为Vue项目通常使用Node.js。但可能只是项目存放在Java目录下,不影响问题本身。 我应该建议用户先检查package.json中的依赖版本,然后执行安装命令,比如npm install或者yarn add,并确保vue-template-compiler的版本与vue一致。如果问题仍然存在,可能需要清除缓存和重新安装依赖。 最后,可能需要提醒用户注意Vue 2和Vue 3之间的差异,因为Vue 3使用@vue/compiler-sfc而不是vue-template-compiler,但根据错误信息,用户的项目应该是Vue 2,所以应确保使用正确的包。</think>这个错误提示表明你的Vue项目缺少关键依赖模块`vue-template-compiler`。以下是分步排查和解决方法: --- ### **问题根源分析** 1. **依赖缺失**:`vue-template-compiler`是Vue单文件组件(`.vue`)的模板编译器,必须与`vue`主库版本严格匹配。 2. **版本冲突**:如果`vue`和`vue-template-compiler`版本不一致,会触发此类错误。 3. **安装异常**:`node_modules`可能未正确安装或存在损坏。 --- ### **解决方案** #### **步骤1:检查依赖版本** 打开项目根目录的`package.json`,确保`dependencies`或`devDependencies`中包含以下条目且版本一致: ```json { "dependencies": { "vue": "^2.6.14" }, "devDependencies": { "vue-template-compiler": "^2.6.14" } } ``` - 若版本不匹配,需手动修改并保存文件。 --- #### **步骤2:安装/更新依赖** 在项目根目录执行以下命令(根据包管理器选择): ```bash # 使用 npm npm install vue@2 --save npm install vue-template-compiler@2 --save-dev # 使用 yarn yarn add vue@2 yarn add vue-template-compiler@2 --dev ``` - `@2`表示强制安装Vue 2.x的最新稳定版。 --- #### **步骤3:清除缓存并重装依赖** 若步骤2无效,尝试彻底清理并重新安装: ```bash # 删除现有依赖 rm -rf node_modules rm package-lock.json # npm用户 # 或 rm yarn.lock # yarn用户 # 重新安装 npm install # 或 yarn install ``` --- #### **步骤4:验证Vue Loader版本** 检查`vue-loader`版本是否兼容: - Vue 2.x需要`vue-loader@15.x` - Vue 3.x需要`vue-loader@16.x`(但此时错误信息显示你正在使用Vue 2) 如果误装Vue 3的依赖,需回退: ```bash npm uninstall vue-loader @vue/compiler-sfc npm install vue-loader@15 --save-dev ``` --- ### **常见问题补充** - **全局安装冲突**:若全局安装了旧版Vue CLI,尝试卸载并仅使用本地版本: ```bash npm uninstall -g @vue/cli npm install @vue/cli --save-dev ``` - **文件权限问题**:Windows用户请以管理员权限运行命令行。 --- ### **总结** 通过上述步骤,99%的同类问题可被解决。若仍报错,请提供以下信息进一步排查: 1. `vue --version`的输出 2. `package.json`完整内容 3. 是否修改过Webpack配置?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值