• 北理工软件学院2016程序设计方法与实践


      突然发现网教还能上,就打了打酱油,先贴前几题,没抄题目,只有源程序,适合软院的学弟学妹们参考,后续更不更看心情和水平罢……

      都写在一个文件里了,去掉对应的注释,添加需要的头文件(例如string.h、math.h)即可提交。

      文章末尾陆续更新……

    已有题目:球体问题,修剪草坪,扫雷,合并果子,传送带,贪婪的你,四则运算之加减法,一夜白发《千字文》,自由落体,倒数问题,小浣熊干脆面

    贪婪的你,此题要点在于,截止日期最长的若干道题中,分数最高的那道必须放在最后一天做,再向前倒推,每天选择没有截止且没有完成的题目中分数最高的,直到回到第一天,完成一道d>=1且分数最高的题目。

    #include <stdio.h>
    #include <stdlib.h>
    
    
    //6、贪婪的你
    #define MAXDAY 10001
    
    typedef struct
    {
        int p, d;
    }P;
    P item[10001];
    int last=0;
    int cmp(const void * a, const void * b)
    {
        return (*(P *)b).d - (*(P *)a).d;
    }
    int findMax(int i)
    {
        int index = -1, j, maxp = 0;
        for(j=0; j<last, item[j].d >= i; j++)
        {
            if (item[j].d == MAXDAY || item[j].d < i) continue;
            if (item[j].p > maxp)
            {
                maxp = item[j].p;
                index = j;
            }
        }
        if (index>=0) item[index].d = MAXDAY;//该题目被选中
        return index;
    }
    int getScore(int n)
    {
        int index = 0;
        if (n==1) return item[findMax(1)].p;//寻找d>=1中分数最高且没做的题
        index = findMax(n);
        if (index != -1)
            return getScore(n-1) +     item[index].p;
        else return getScore(n-1);
    }
    int main()
    {
        int i, n;
        scanf("%d",&n);
        for (i=0; i<n; i++)
        {
            scanf("%d",&item[i].p);
        }
        for (i=0; i<n; i++)
        {
            scanf("%d",&item[i].d);
            last = last>item[i].d ? last : item[i].d;//记录最大期限
        }
        qsort(item, n, sizeof(P), cmp);//快排不稳定,令序列按截止日期降序排列
        item[n].d = -1;//防止冗余搜索
        printf("%d
    ",getScore(last));
        return 0;
    }
    
    
    /*5、机场传送带
    typedef struct{
        double s,v;
    }S;
    S a[1000005];
    
    int cmp(const void * a, const void * b)
    {
        return (*(S *)a).v - (*(S *)b).v;
    }
    
    int main()
    {
        int n, m, i, Bi, Ei, Vi;
        double time = 0, t, x, v, r;
        scanf("%d",&n);
        while (n--)
        {
            time = 0;
            scanf("%lf%lf%lf%lf%d",&x,&v,&r,&t,&m);
            
            //memset(a, 0, 2*(m+1)*sizeof(a[0][0]));
            a[0].s = a[0].v = 0;
            for(i=1; i<=m; i++)
            {
                scanf("%d%d%d",&Bi,&Ei,&Vi);
                a[i].s = Ei - Bi;
                a[i].v = Vi;
                x -= Ei - Bi;
            }
            a[0].s = x;//视为速度为0的传送带
            a[0].v = 0;
            qsort(a, m+1, sizeof(S), cmp);
            for (i=0; i<=m; i++)
            {
                //printf("len:%d v:%d
    ",a[i][0],a[i][1]);
                if (t>0)
                {
                    x = (r+a[i].v) * t;
                    if (x >= a[i].s)
                    {
                        t -= a[i].s/(r+a[i].v);
                        time += a[i].s/(r+a[i].v);
                        continue;
                    }
                    else
                    {
                        time += t;
                        t = 0;
                        a[i].s -= x;
                        //i--;
                    }
                }
                time += a[i].s / (a[i].v+v);
            }
            printf("%.6f
    ",time);
        }
        return 0;
    }
    */
    
    /*4、合并果子
    //直接插入排序
    int insertSort(int t, int * a, int n)
    {
        int j;
        if (t == -1)
        {
            t = a[0];
            for (j=0; j<n-1; j++)
            {
                a[j] = a[j+1];
                if (t<a[j])//insert t before a[j]
                {
                    a[j] = t;
                    return 0;
                }
            }
            a[j] = t;
        }
        else
        {
            for (j=n-1; j>0; j--)
            {
                a[j] = a[j-1];
                if (t>a[j])//insert t before a[j]
                {
                    a[j] = t;
                    return 0;
                }
            }
            a[0] = t;
        }
        return 0;
    }
    
    int main()
    {
        int n = 0, i, tmp, a[10000]={0}, *p, cost = 0;
        scanf("%d",&n);
    
        p = a;
        for (i=1; i<=n; i++)
        {
            scanf("%d",&tmp);
            insertSort(tmp, a, i);
        }
        for (i=0; i<n-1; i++)
        {
            p[1] += p[0];
            cost += p[1];
            insertSort(-1, ++p, n-i-1);
        }
        printf("%d
    ",cost);
        return 0;
    }
    */
    
    /*3、扫雷
    int main()
    {
        int n, m, i, j, k, l, No=1;
        char a[102][102];//, short * b[102];
        while (1)
        {
            scanf("%d%d",&n,&m);
            if (n*m == 0) break;
            getchar();
            //memset(a, '0', sizeof(a[0][0])*(m+2)*(n+2));
            memset(a, '0', sizeof(a[0][0])*10404);
            for (i=1; i<=n; i++)
            {
                for (j=1; j<=m; j++)
                {
                    scanf("%c",&a[i][j]);
                }
                getchar();
            }
            for (i=1; i<=n; i++)
            {
                for (j=1; j<=m; j++)
                {
                    if (a[i][j] == '.')
                    {
                        a[i][j] = '0';
                        for (k=i-1; k<=i+1; k++)
                        {
                            for (l=j-1; l<=j+1; l++)
                                if (a[k][l] == '*')
                                    a[i][j] += 1;
                        }
                    }
                }
            }
            if (No>1) printf("
    ");
            printf("Field #%d:
    ",No++);
            for (i=1; i<=n; i++)
            {
                for (j=1; j<=m; j++)
                {
                    printf("%c",a[i][j]);
                }
                printf("
    ");
            }
        }
        return 0;
    }
    */
    
    
    /*2、修剪草坪
    int main()
    {
        int i, j, k, l, n, m, * a[100], flag=0;
        scanf("%d%d",&n,&m);
        for (i=0; i<n; i++)
        {
            a[i] = (int *)malloc(m*sizeof(int));
            for (j=0; j<m; j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        for (i=0; i<n; i++)
        {
            for (j=0; j<m; j++)
            {
                flag = 0;
                for (k=0; k<n; k++)
                {
                    if (a[k][j] > a[i][j])
                    {
                        flag = 1;
                        break;
                    }
                }
                for (l=0; l<m; l++)
                {
                    if (a[i][l] > a[i][j] && flag == 1)
                    {
                        puts("NO");
                        goto END;
                        break;
                    }
                }
            }
        }
        puts("YES");
    END:    
        for (i=0; i<n; i++)
        {
            free(a[i]);
        }
        return 0;
    }*/
    
    /* 1、球体问题
    #define PI 2*acos(0.0)
    
    int main()
    {
        int n=1,  r1, r2, d, w;
        double p, h, s, d1, d2, V, S, density;
        //printf("PI = %f
    ",PI);
        scanf("%d",&n);
        while (n--)
        {
            scanf("%d%d%d%d%lf", &r1, &r2, &d, &w, &s);
            
            d1 = r1 - ((r1*r1 - r2*r2 + d*d)/(2.0*d));
            d2 = r2 - ((r2*r2 - r1*r1 + d*d)/(2.0*d));
            V= PI * ((3.0*r1-d1)* (d1*d1) + (3.0*r2-d2)*(d2*d2)) /3.0;
            V = PI * (4.0*((r1*r1*r1) + (r2*r2*r2))/3.0)- V;
    
            p = (r1+r2+d)/2.0;
            h = 2*sqrt(p*(p-r1)*(p-r2)*(p-d))/d;
    
            S = 2*PI * (d1*r1+d2*r2);
            S = 4*PI*(r1*r1+r2*r2) - S;
    
            density = w/S;
            printf("%.4f %.4f
    ", V, S);
            if (density>s)
                printf("The Paired-Sphere Sinks.
    ");
            else
                printf("The Paired-Sphere Floats.
    ");
        }
        return 0;
    }
    */
    #include <stdio.h>
    #include <string.h>
    
    /*
    千字文
    */
    /*
       |  Unicode符号范围      |  UTF-8编码方式
     n |  (十六进制)           | (二进制)
    ---+-----------------------+------------------------------------------------------
     1 | 0000 0000 - 0000 007F |                                              0xxxxxxx
     2 | 0000 0080 - 0000 07FF |                                     110xxxxx 10xxxxxx
     3 | 0000 0800 - 0000 FFFF |                            1110xxxx 10xxxxxx 10xxxxxx
     4 | 0001 0000 - 0010 FFFF |                   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
     5 | 0020 0000 - 03FF FFFF |          111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
     6 | 0400 0000 - 7FFF FFFF | 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
    
                        表 1. UTF-8的编码规则
    */
    unsigned int unicode_to_utf8(unsigned int unicode, char * utf8)
    {
        char * p;
        if (unicode <0x80)
        {
            utf8[0] = unicode;
            return 0;
        }
        else if (unicode < 0x800)
        {
            utf8[0] = 0xc0 + ((unicode>>6)&0x1f);
            p = &utf8[1];
        }
        else if (unicode < 0x10000)
        {
            utf8[0] = 0xe0 + ((unicode>>12)&0xf);
            p = &utf8[2];
        }
        else if (unicode < 0x110000)
        {
            utf8[0] = 0xf0 + ((unicode>>18)&0x7);
            p = &utf8[3];
        }
        else if (unicode < 0x4000000)
        {
            utf8[0] = 0xf8 + ((unicode>>24)&0x3);
            p = &utf8[4];
        }
        else
        {
            utf8[0] = 0xfc + ((unicode>>30)&0x1);
            p = &utf8[5];
        }
        do
        {
            *p-- = 0x80 + (unicode & 0x3f);
            unicode >>= 6;
        }
        while (p != &utf8[0]);
        return 0;
    }
    
    unsigned int utf8_to_unicode(unsigned char * utf8, int inLen)
    {
        unsigned int tmp = 0;
        if (inLen == 6)
        {
            tmp += (utf8[5] & 0x3f) + ((utf8[4] & 0x3f)<<6) + ((utf8[3] & 0x3f)<<12) + ((utf8[2] & 0x3f)<<18) + ((utf8[1] & 0x3f)<<24);
            tmp +=  ((utf8[1] & 0x3C)<<24) + ((utf8[0] & 0xf)<<30);
        }
        if (inLen == 5)
        {
            tmp += (utf8[4] & 0x3f) + ((utf8[3] & 0x3f)<<6) + ((utf8[2] & 0x3f)<<12) + ((utf8[1] & 0x3f)<<18);
            tmp += ((utf8[0] & 0xf)<<24);
        }
        if (inLen == 4)
        {
            tmp += (utf8[3] & 0x3f) + ((utf8[2] & 0x3f)<<6) + ((utf8[1] & 0x3f)<<12);
            tmp += ((utf8[0] & 0xf)<<18);
        }
        else if (inLen == 3)
        {
            tmp += (utf8[2] & 0x3f) + ((utf8[1] & 0x3f)<<6);
            tmp += ((utf8[0] & 0xf)<<12);
        }
        else if (inLen == 2)
        {
            tmp += (utf8[1] & 0x3f);
            tmp += ((utf8[0] & 0x1f)<<6);
        }
        else 
        {
            tmp = utf8[0] & 0x7f;
        }
        return tmp;
    }
    
    #define BUFFSIZE 5000
    #define HASHSIZE 0x10000
    int HashTable[HASHSIZE] = {0};
    
    int main()
    {
        int inLen = 0, i = 0, flag = 1;
        unsigned char text[BUFFSIZE] = {0};
        char tmp[8] = {0};
        unsigned int unicode = 0;
    
        while (scanf("%s",text) != EOF)//(size > 0)
        {
            inLen = strlen((const char *)text);
            for (i=0; i<inLen;)
            {
                memset(tmp, 0, 8);
                if ((text[i] & 0xFC) == 0xFC)
                {
                    unicode = utf8_to_unicode(&text[i], 6);
                    i += 6;
                }
                else if ((text[i] & 0xF8) == 0xF8)
                {
                    unicode = utf8_to_unicode(&text[i], 5);
                    i += 5;
                }
                else if ((text[i] & 0xF0) == 0xF0)
                {
                    unicode = utf8_to_unicode(&text[i], 4);
                    i += 4;
                }
                else if ((text[i] & 0xE0) == 0xE0)
                {
                    unicode = utf8_to_unicode(&text[i], 3);
                    i += 3;
                }
                else if ((text[i] & 0xC0) == 0xC0)
                {
                    unicode = utf8_to_unicode(&text[i], 2);
                    i += 2;
                }
                else//ascii
                {
                    unicode = utf8_to_unicode(&text[i], 1);
                    i++;
                }
                HashTable[unicode] ++;
            }
        }
        for (i=128; i<HASHSIZE; i++)
        {
            if (HashTable[i]>1)
            {
                unicode_to_utf8(i, tmp);
                printf("%s ",tmp);
                printf("0x%04x %d
    ", i, HashTable[i]);
                memset(tmp, 0, 8);
                flag = 0;
            }
        }
        if (flag)
            printf("No repeat!
    ");
        //fclose(f);
        return 0;    
    }

    下文中自由落体的代码其实可以优化时间,没必要走n次循环

    //自由落体
    #include <stdio.h>
    #include <math.h>
    #define EDGE 0.00001
    int main()
    {//H,S1,V,L,K,n (l<=H,S1,V,L,K,n <=100000)
        double h, s1, v, l, k, t1 = 0, t2 = 0, dY1 = 0, dY2 = 0;
        int n, count = 0;
        scanf("%lf%lf%lf%lf%lf%d",&h,&s1,&v,&l,&k,&n);//n是小球个数,实际序号为0 - n-1
        while (n--)//以每个球为坐标原点,求抛物线轨迹
        {
            t1 = (s1 - n - EDGE) / v;
            t2 = (s1 - n + l + EDGE) / v;
            dY1 = 5 * t1 * t1;
            dY2 = 5 * t2 * t2;
            if (dY1 >= h || dY2<=(h-k-EDGE))
                continue;
            count ++;
        }
        printf("%d
    ",count);
        return 0;
    }

     倒数问题写的逻辑很混乱,没什么参考价值,用到了四则运算题目中的减法子函数

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    //倒数问题
    void traverse(char * a, int n)
    {
        int i = 0, flag = 0;
        char * tmp = (char *)malloc(sizeof(char)*n);
        memcpy(tmp, a, n*sizeof(a[0]));
        while (n--)
        {
            if (!flag && tmp[n]=='0')
            {
                continue;
            }
            else flag = 1;
            if(flag) a[i++] = tmp[n];
        }
        a[i] = '';
        free(tmp);
    }
    
    void minus_d(char a, char b, char * c, int * carry)
    {
        int tmp = 0;
        tmp = (a-'0') - (b-'0') - *carry;
        *carry = tmp>=0 ? 0 : 1;//向上一位借位
        *c = (tmp + 10)%10 + '0';
    }
    void minus(char *a, char *b, char *c)
    {//c = a - b
        int len1, len2, carry=0, i, j, k=0, cmp;
        len1 = strlen(a); 
        len2 = strlen(b);
        cmp = strcmp(a, b);
        if ((len1==len2 && cmp< 0) || len1<len2)
        {
            c[0] = '-';
            minus(b, a, &c[1]);
            return;
        }
        else if (cmp == 0)
        {
            c[0] = '0';
            c[1] = '';
            return;
        }
        for (i=len1-1,j=len2-1; i>=0 && j>=0; i--,j--)
        {
            minus_d(a[i], b[j], &c[k++], &carry);
        }
        for (;i>=0; i--)
        {
            minus_d(a[i], '0', &c[k++], &carry);
        }
        for (;j>=0; j--)
        {
            minus_d('0', b[j], &c[k++], &carry);
        }
        traverse(c, k);
    }
    int equal_zero(char * a)
    {
        int i = strlen(a);
        while (i--)
            if (a[i] != '0' && a[i] != '.')
                return 0;
        return 1;
    }
    #define N 75
    int main()
    {
        int len = 0, i = 1, time = 0, tmp, m;
        char a[N] = {0}, b[5*N] = {'1'}, c[N] = {0};
        scanf("%d", &m);
        //getchar();//吸收回车符
        while(m--)
        {
            memset(a, 0, N);
            memset(b, 0, 5*N);
            i=1;
            time = 0;
            
            b[0] = '1';
            scanf("%s",a);
            len = strlen(a);//a的位数
            //minus(b, a, c);
            //if (c[0]=='-')
            //    printf("0.");
            if (strcmp(a,"1") == 0)
            {
                printf("1
    ");
                continue;
            }
            printf("0.");
            while (i<=len)
            {
                b[i++] = '0';
                minus(b, a, c);
                if (c[0]=='-')
                {
                    printf("0");
                }
                else if (equal_zero(c))
                {
                    break;
                }
            }
            
                //b[i] = '0';
        
            while (1)
            {
                minus(b, a, c);
                if (c[0] == '-')
                {
                    tmp = strlen(b);
                    b[tmp] = '0';// b = b * 10
                    memset(&b[tmp+1], 0, 5*N-tmp-1);
                    printf("%c",time+'0');
                    time = 0;
                    continue;
                }
                memcpy(b,c,len+1);
                time ++;
                if (equal_zero(c))
                {
                    printf("%d",time);
                    break;
                }
            }
            printf("
    ");
        }
        return 0;
    }

     四则运算提供的对外接口:void plus(char * a, char * b, char * c) 和void minus(char * a, char * b, char * c),其余子函数不要直接调用。另外赠送一个乘法函数void times(char * a, char * b, char * c)。

    //7、四则运算之加减法
    
    #include <stdio.h>
    #include <string.h>
    
    void traverse(char * a, int n)
    {
        int i = 0, flag = 0;
        char * tmp = (char *)malloc(sizeof(char)*n);
        memcpy(tmp, a, n*sizeof(a[0]));
        while (n--)
        {
            if (!flag && tmp[n]=='0')
            {
                continue;
            }
            else flag = 1;
            if(flag) a[i++] = tmp[n];
        }
        a[i] = '';
        free(tmp);
    }
    
    void plus_d(char a, char b, char * c, int * carry)
    {
        int tmp = 0;
        tmp = (a-'0') + (b-'0') + *carry;
        *carry = tmp>9 ? 1 : 0;
        *c = (tmp % 10) + '0';
    }
    
    void plus(char *a, char *b, char *c)
    {//按字节运算
        int len1, len2, carry=0, i, j, k=0;
        len1 = strlen(a); 
        len2 = strlen(b);
        for (i=len1-1,j=len2-1; i>=0 && j>=0; i--,j--)
        {
            plus_d(a[i], b[j], &c[k++], &carry);
        }
        for (;i>=0; i--)
        {
            plus_d(a[i], '0', &c[k++], &carry);
        }
        for (;j>=0; j--)
        {
            plus_d('0', b[j], &c[k++], &carry);
        }
        if (carry) c[k++] = carry+'0';
        traverse(c, k);
    }
    
    void minus_d(char a, char b, char * c, int * carry)
    {
        int tmp = 0;
        tmp = (a-'0') - (b-'0') - *carry;
        *carry = tmp>=0 ? 0 : 1;//向上一位借位
        *c = (tmp + 10)%10 + '0';
    }
    void minus(char *a, char *b, char *c)
    {
        int len1, len2, carry=0, i, j, k=0, cmp;
        len1 = strlen(a); 
        len2 = strlen(b);
        cmp = strcmp(a, b);
        if ((len1==len2 && cmp< 0) || len1<len2)
        {
            c[0] = '-';
            minus(b, a, &c[1]);
            return;
        }
        else if (cmp == 0)
        {
            c[0] = '0';
            c[1] = '';
            return;
        }
        for (i=len1-1,j=len2-1; i>=0 && j>=0; i--,j--)
        {
            minus_d(a[i], b[j], &c[k++], &carry);
        }
        for (;i>=0; i--)
        {
            minus_d(a[i], '0', &c[k++], &carry);
        }
        for (;j>=0; j--)
        {
            minus_d('0', b[j], &c[k++], &carry);
        }
        traverse(c, k);
    }
    void times(char *a, char *b, char *c)
    {
        int len, shift = 0, i, j;
        char tmp[2001];
        len = strlen(b);
        shift = strlen(a);
        c[0] = '0';
        c[1] = 0;
        for (i=0; i<len; i++)
        {
            for (j=0; j<b[i]-'0'; j++)
            {
                plus(c, a, tmp);
                memcpy(c, tmp, strlen(tmp));
                c[strlen(tmp)] = 0;
            }
            if (i < len-1)
                c[shift++] = '0';
            c[shift] = 0;
        }
    }
    
    int main()
    {       
        char a[1000];
        char b[1000];
        char c[2001];
        char s[2];
        scanf("%s",a);
        getchar();
        scanf("%s",s);
        getchar();
        scanf("%s",b);
        getchar();
       // while (scanf("%s %s %s
    ", a, s, b) == 3) {
            if (s[0] == '+') {
                plus(a, b, c);
            } else if (s[0] == '-') {
                minus(a, b, c);
            }
            else if (s[0] == '*') {
                times(a, b, c);
            }
            printf("%s
    ", c);
       // }
    
        return 0;
    }
    //小浣熊干脆面
    #include <stdio.h> #include <string.h> #define N 1000000 unsigned short L[N] = {0}; int n, m, record = N; char hash[2001] = {0}; int main() { int i, tmp, count = 1, end = 0; scanf("%d%d",&n,&m); end = n-1; memset(L, N, n*sizeof(L[0])); for (i=0; i<n; i++) { scanf("%d",&L[i]); } hash[L[n-1]] = 1; for (i=n-1; i>0; i--) { if (hash[L[i-1]] == 0) count ++; hash[L[i-1]]++; if (L[i-1] == L[end]) { while (hash[L[end]] > 1) { hash[L[end]]--; end --; } } if (count == m) { tmp = end - i +2; record = record < tmp ? record : tmp;
           if (record == m) break; } } printf(
    "%d ", record); return 0; }
  • 相关阅读:
    使用java连接数据库以后显示“ Establishing SSL connection without server's identity verification is not recommended”的警告如何解决
    使用java发送QQ邮件
    编写java的时候出现“编码GBK的不可映射字符”
    java如何调用另一个包里面的类
    postman断言的方法
    postman参数化的方法
    postman批量执行 要给请求加断言,批量执行的时候才会去统计,成功和失败的条数
    python查找字符串 函数find() 用法
    postman请求ajax失败的解决方法
    jmeter 模拟ajax/ https请求 失败的解决方法
  • 原文地址:https://www.cnblogs.com/weir007/p/5897275.html
Copyright © 2020-2023  润新知