Life of xhu

About

折腾代码高亮 & HOJ_1599 & HOJ_1623

May 25, 2014

  |   #Frontend   |   #HOJ

前天推上了那篇文章后,我发现文章中代码如果没有行号的话,实在是不容易进行讲解,所以我又想折腾了,想换一个前端高亮工具来实现行号的显示。Google了一阵,首先瞄准了Syntaxhighlighter,看了一下官网的demo,果断是显示行号的,后来又看了一下Google自己的解决方案Pretty,也不错。但是在实际使用的过程中,我发现了一个不可忽视的困难。

这些方案,都是需要指定代码的语言的。也就是说,比如说你文章中嵌入了Ruby,那么你需要在HTML代码中指定这段代码使用Ruby语言的方案来渲染高亮。这在以前HTML普遍使用手写的时代是很正常的,或者如果一个网站上的代码语言比较统一(比如ITeye全用Java的方案来渲染),也是可以理解的。但是我现在的网站使用Markdown,在转换成HTML代码后,只有一个大的pre标签和下面的code标签,如果要我的针对每一段代码都指定语言,很显然是非常不现实的。

所以,我还是只能用Highlightjs了,毕竟这是我发现的唯一一款可以自动推测代码所用语言的工具,官方好像给了一个显示行号的方案,而且的Github上也有这个分支,但是实际使用好像不太行,即使加了hljs.lineNodes = true;这条语句也没有效果,算了,其实目前这个效果我也挺满意的,折腾就到此为止吧。

~~~~~~~~~~~~我是萌萌的昏割线~~~~~~~~~~~~~

今天很累,下午陪着豆豆打了六个小时麻将,简直快睡着了。而且题意也很简单,就不写中文翻译了,直接放代码吧。

1599:

#include <iostream>
using namespace std;

int main(){
    int n, hi[60], sum, aver, res, count = 0;
    while(cin >> n && n != 0){
        sum = res = 0;
        for(int i = 0; i < n; i++){
            cin >> hi[i];
            sum += hi[i];
        }
        aver = sum / n;
        for(int i = 0; i < n; i++){
            if(hi[i] > aver)
                res += hi[i] - aver;
        }
        cout << "Set #" << ++count << endl;
        cout << "The minimum number of moves is " << res << '.' << endl << endl;
    }
}

1623:

#include <iostream>
using namespace std;

int main(){
    int n, m, c, ci[30], max, sum, tmp, count = 0;
    bool blown, on[30];
    while(cin >> n >> m >> c && !(n == 0 && m == 0 && c == 0)){
        memset(on, false, sizeof(on));
        sum = max = blown = 0;
        for(int i = 0; i < n; i++)
            cin >> ci[i + 1];
        for(int i = 0; i < m; i++){
            cin >> tmp;
            on[tmp] = !on[tmp];
            if(on[tmp])
                sum += ci[tmp];
            else
                sum -= ci[tmp];
            if(sum >= max)
                max = sum;
            if(max > c)
                blown = true;
        }
        cout << "Sequence " << ++count << endl;
        if(blown)
            cout << "Fuse was blown." << endl << endl;
        else
            cout << "Fuse was not blown.\nMaximal power consumption was " << max << " amperes." << endl << endl;
    }
}