程序设计实践课

今天的程序设计实践课,一佳学长现场演示了上次huffman的问题,写的是个简洁、飘逸啊,指针用的是个灵活。。。同步敲的代码如下,由于一些地方没跟上,所以可能略有些不同:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int N = 10050;
struct node{
    int value;
    node *left,*right;
 
    bool operator< (const node b)const
    {
        return value < b.value;
    }
};
node leaves[ N ], inner[ N ];                            //名字起的很直白,一看就懂
 
int readString(){                                            //读入的处理,额,一般我习惯scanf %s这么处理。。。
    int ret = 0;
    char ch;
    while( (ch = getchar()) !=' ' ) ret++;
    return ret - 1;
}
int preOrder( node *now, int depth ){                //将树遍历一遍,求出答案
    if( now == NULL )
        return 0;
    else {
        if( now -> left == NULL && now -> right ==NULL ) {
            return depth * now -> value;
        }
        else {
            return preOrder( now -> right, depth + 1) + preOrder( now -> left, depth + 1);
        }
    }
}
int main()
{
    int n;
    while( scanf( "%d", &n ) == 1){
        int ans1 = 0;
        for( int i = 0; i < n; i ++){
            int len = readString();
            scanf( "%d", &leaves[ i ].value);
            leaves[ i ].left = NULL;
            leaves[ i ].right = NULL;
            ans1 = ans1 + len * leaves[ i ].value;
        }
        if( n == 1 ) {
            printf( "%d %d\n", ans1,leaves[ 0 ].value);
            continue;
        }
        sort( leaves , leaves + n);
        int now = 0, head = 0, tail = 0;
        for( int i = 1; i < n; i++){
            node *minValue, *secMinValue;
            if( head == tail || (now < n && leaves[ now ] < inner[ head ] )) {
                minValue = leaves + now;
                now ++;
            }
            else {
                minValue = inner + head;
                head ++;
            }
            if( head == tail || (now < n && leaves[ now ] < inner[ head ] )) {
                secMinValue = leaves + now;
                now ++;
            }
            else {
                secMinValue = inner + head;
                head ++;
            }
            inner[ tail ].value = minValue -> value + secMinValue -> value;
            inner[ tail ].left = minValue;
            inner[ tail ].right = secMinValue;
            tail ++;
        }
        node * root = inner + head;
        printf("%d %d\n",ans1, preOrder( root, 0));
    }
    return 0;
}


然后呢,我又模仿这个样子,写了stoneI,虽然题很水,但是模仿着1+学长写的很是有条有理,哈哈,贴代码:

#include <iostream>
 
using namespace std;
const int N = 100010;
int a[N],b[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,ans=0,i;
        scanf("%d",&n);
        for(i=0;i<n ;i++)
            scanf("%d",a+i);
        sort(a,a+n);
        int now = 0, head = 0, tail = 0, minVal, secMinVal;
        for(i = 0; i < n - 1; i ++)
        {
            if( head == tail || ( now < n && a[ now ]< b[ head ]))
            {
                minVal = a[ now ];
                now ++;
            }
            else
            {
                minVal = b[ head ];
                head++;
            }
            if( head == tail || ( now < n && a[ now ]< b[ head ]))
            {
                secMinVal = a[ now ];
                now ++;
            }
            else
            {
                secMinVal = b[ head ];
                head++;
            }
            b[ tail++ ] = minVal + secMinVal;
            ans += minVal + secMinVal;
        }
        printf("%d\n",ans);
    }
    return 0;
}

PS:又是件奇特的事,我照着1+学长抄的代码竟然在statistic里排第一,哈哈哈。。。
PS2:这个选修课我本来没有选,完全是蹭课去了,貌似我选的选修课都木有去听,类似于书非借不能读也。。。。

你可能感兴趣的:(struct,null,iostream)