009-琶フ唬客機(jī)試題2(通過(guò)測(cè)驗(yàn))

1、IP地址統(tǒng)計(jì)問(wèn)題

注: 有些小細(xì)節(jié)會(huì)導(dǎo)致比較難排查的錯(cuò)誤婿着,例如ip字符串和mask字符串分離是否真確授瘦,即使一個(gè)字母,也會(huì)出錯(cuò)竟宋。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int toNum(char *str_p, unsigned char *out)//需要保證out為大于或等于4個(gè)元素的數(shù)組
{
    int i;
    int pos;
    int cnt;
    char tmp[128] = {0};
    char *tmp_p = tmp;
    i = 0;
    pos = 0;
    cnt = 0;
    while(str_p[i] != '\0')
    {
        if(str_p[i] == '.')
        {
            tmp_p = str_p + pos;
            tmp_p[i - pos] = '\0';
            out[cnt] = atoi(tmp_p);
            if(out[cnt] == 0 && tmp_p[0] != '0')
                return -1;
            pos = i + 1;
            cnt++;
            if(cnt >= 3)
                break;
        }   
        i++;
    }
    if(cnt != 3)
        return -1;
    tmp_p = str_p + pos;
    out[3] = atoi(tmp_p);
    return 0;
}
int maskIsOk(unsigned char *num_mask)
{
    int i, j;
    int mark;
    int one_num;
    mark = 0;
    one_num = 0;        //全部為1或者0的掩碼為非法
    for(i = 0; i < 4; i++)
    {
        for(j = 0;j < 8; j++)
        {
            if((num_mask[i] & (1 << (7 - j))) != 0)
            {
                one_num++;
                if(mark == 1)
                {
                    return -1;
                }
            }       
            else if(mark == 0)
            {
                mark = 1;
                continue;
            }
        
        }
    }
    if(one_num == 0 || one_num >= 32)
    {
        return -1;
    }
    return 0;
}
int isIpPrivate(unsigned char *num_ip)
{
    int ip;
    int type;
    int i;
    ip = num_ip[0] << 24 | num_ip[1] << 16 | num_ip[2] << 8 | num_ip[3];
    const int start[] = 
    {
        10 << 24,
        172 << 24 | 16 << 16,
        192 << 24 | 168 << 16
    };
    const int end[] = 
    {
        10  << 24 | 255 << 16 | 255 << 8 |255,
        172 << 24 | 31 << 16 | 255 << 8 | 255 ,
        192 << 24 | 168 << 16 | 255 << 8 | 255 
    };
    for(i = 0; i < 3;  i++)
    {
        if(ip >= start[i] && ip <= end[i])
        {
            return 0;      
        }
    }
    return -1;
}
int ipType(unsigned char *num_ip)
{
    int ip;
    int type;
    int i;
    ip = num_ip[0] << 24 | num_ip[1] << 16 | num_ip[2] << 8 | num_ip[3];
    if(num_ip[0] == 0 || num_ip[0] == 127)
    {
        return 0;
    }
    
    const int start[] = 
    {
        1 << 24,
        128 << 24,
        192 << 24,
        224 << 24,
        240 << 24
    };
    const int end[] = 
    {
        126 << 24 | 255 << 16 | 255 << 8 |255,
        191 << 24 | 255 << 16 | 255 << 8 |255,
        223 << 24 | 255 << 16 | 255 << 8 |255,
        239 << 24 | 255 << 16 | 255 << 8 |255,
        255 << 24 | 255 << 16 | 255 << 8 |255
    };
    
    for(i = 0; i < 5;  i++)
    {
        if(ip > start[i] && ip < end[i])
        {
           return  (i + 1);     
        }
    }
    return -1;
}

int main(void)
{
    int i, j;
    char str_in[128] = {0};
    char str_ip[128] = {0};
    char str_mask[128] = {0};
    char *str_ip_p;
    char *str_mask_p;
    unsigned char num_ip[4] = {0};
    unsigned char num_mask[4] = {0};
    int a, b, c, d, e, error, pri;
    a = b = c = d = e = error = pri = 0;
    int ret, type;
    while(scanf("%s", str_in) != EOF)
    {
        strcpy(str_ip, str_in);
        strcpy(str_mask, str_in);
        i = 0;
        while(str_in[i] != '\0')
        {
            if(str_in[i] == '~')
            {
                str_ip_p = str_ip;
                str_ip_p[i] = '\0';
                str_mask_p = str_mask + i + 1;
                break;          
            }
            i++;
        }       
        ret = toNum(str_mask_p, num_mask);
        if(ret != 0)
        {
            error++;
            continue;
        }
        else if(maskIsOk(num_mask) != 0)
        {
            error++;
            continue;
        }
        
        ret = toNum(str_ip_p, num_ip);
        if(ret != 0)
        {
            error++;
            continue;
        }   
        else
        {
            type = ipType(num_ip);
            if(type == 1)
                a++;
            else if(type == 2)
                b++;
            else if(type == 3)
                c++;
            else if(type == 4)
                d++;
            else if(type == 5)
                e++;
            else if(type == 0)
                ;
            else
                {error++; continue;}
            if(isIpPrivate(num_ip) == 0)
                pri++;
        }
    }
    
    printf("%d %d %d %d %d %d %d\n", a, b, c, d, e, error, pri);
    return 0;
}

2提完、密碼驗(yàn)證合格程序


#include <stdio.h>
#include <string.h>
int main(void)
{
    char psswd[128] = {0};
    int len;
    int mark[4];
    int brk;
    int i, j;
    while(scanf("%s", psswd) != EOF)
    {
        //printf("----------------------->\n");
        len = strlen(psswd);
         if(len <= 8)
         {
             printf("%s\n", "NG"); 
             continue;
         }
         mark[0] = mark[1] = mark[2] = mark[3] =  0;
         for(i = 0; i < len; i ++)
         {
             if((psswd[i] >= 'a' && psswd[i] <= 'z'))
             {
                 mark[0] = 1;
             }
              else if((psswd[i] >= 'A' && psswd[i] <= 'Z'))
             {
                 mark[1] = 1;
             }
             else if((psswd[i] >= '0' && psswd[i] <= '9'))
             {
                 mark[2] = 1;
             }
             else
             {
                 mark[3] = 1; 
             } 
         }
        if((mark[0] + mark[1] + mark[2] + mark[3])  < 3)
        {
            printf("%s\n", "NG"); 
            continue;
        }   
        brk = 0;
        for(i = 0; i < len - 5; i ++)
        {
            if(brk == 1)
            {
                printf("%s\n", "NG");    
                break;
            }
            for(j = i + 3; j < len - 2; j++)
            {
                if(psswd[i] == psswd[j] && psswd[i + 1] == psswd[j + 1] && psswd[i + 2] == psswd[j + 2])             
                { 
                    brk = 1;
                    break;   
                }
            }
        }     
        if(brk == 1)
        {
            continue;
        }
        printf("%s\n", "OK");       
    }   
    return 0;
}

3、求四則式子的數(shù)值

前提: 輸入的格式是正確的丘侠,不考慮對(duì)格式的判斷
例子: 1+2*10-(-2+3)

/*注:(1)括號(hào)里的式子用遞歸解決徒欣;
*(2)存儲(chǔ)+-符號(hào)用sybol[0],存儲(chǔ)* /符號(hào)用symbol[1]蜗字;存儲(chǔ)第一個(gè)計(jì)數(shù)值用num[0]打肝,第二個(gè)數(shù)值用num[1],主要是為了解決乘除的優(yōu)先級(jí)高于加減這個(gè)規(guī)則挪捕;
*(3)遇到符號(hào)時(shí)粗梭,對(duì)前面的數(shù)值計(jì)算;
*/
#include <stdio.h>
#include <string.h>
#define PRINT_EN 0
int mypow(int n, int cnt)
{
    int i;
    int ret = 1;
    for(i = 0; i < cnt; i++)
    {
        ret *= n;
    }
    return ret;
}
int calc(char *str_p, int set_pos)
{
    int symbol[2] = {0};
    int num[2] = {0};
    int tmp[16] = {0};
    int tmp_value, result;
    char ch;
    tmp_value = result = 0;
    static int pos = 0;  //位置
    if(set_pos == 1)
    {
        pos = 0;
    }
    int mark;
    mark = 0;
    while(1)
    {
        if(mark == 1)
           ch = '+';
        else if(str_p[pos] == '\0')//字符串末尾多個(gè) +级零,執(zhí)行最后操作
            ch = '+';
        else 
            ch = str_p[pos];
        
        if(ch >= '0' && ch <= '9')
        {
            tmp_value *= 10;
            tmp_value += ch - '0';
        }
        else if(ch == '+' || ch == '-')
        {
            if(symbol[1] == 1)//上一個(gè)為乘法
            {
                num[1] *= tmp_value;  
                if(symbol[0] == 1 || symbol[0] == 0)//上上個(gè)為加法
                {
                    num[0] += num[1]; 
                }
                else if(symbol[0] == 2)//上上個(gè)為減法法
                {
                    num[0] -= num[1]; 
                }
            }
            else if(symbol[1] == 2)//上一個(gè)為除法
            {
                if(tmp_value != 0)
                {
                    num[1] /= tmp_value;  
                }                
                if(symbol[0] == 1)//上上個(gè)為加法
                {
                    num[0] += num[1]; 
                }
                else if(symbol[0] == 2)//上上個(gè)為減法法
                {
                    num[0] -= num[1]; 
                }
            }
            else if(symbol[1] == 0 && (symbol[0] == 1 || symbol[0] == 0))
            {
                num[0] += tmp_value;             
            }
             else if(symbol[1] == 0 && symbol[0] == 2)
            {
                num[0] -= tmp_value;             
            }
            symbol[1] = 0;
            if(ch == '+')
            {
                if(PRINT_EN)printf("(+)===>num0=%d   num1=%d\n", num[0], num[1]);
                symbol[0] = 1;    
            }
            else if(ch == '-')
            {
                if(PRINT_EN)printf("(-)===>num0=%d   num1=%d\n", num[0], num[1]);
                symbol[0] = 2;
            }
            tmp_value = 0;          
        }
        else if(ch == '*' || ch == '/')
        {
            if(symbol[1] == 1)
            {
                num[1] *= tmp_value;
            }
            else if(symbol[1] == 2)
            {
                num[1] /= tmp_value;
            }
            else if(symbol[1] == 0 )
            {
                num[1] = tmp_value;
            }           
            if(ch == '*')
            {
                if(PRINT_EN)printf("(*)===>num0=%d   num1=%d\n", num[0], num[1]);
                symbol[1] = 1;    
            }
            else if(ch == '/')
            {
                if(PRINT_EN)printf("(/)===>num0=%d   num1=%d\n", num[0], num[1]);
                symbol[1] = 2;
            } 
            tmp_value = 0;
        }
       else if(ch == '(' || ch == '[' || ch == '{')
        {
            pos++;
            if(PRINT_EN)printf("(digui)------------------\n");
            tmp_value = calc(str_p, 0);
            if(PRINT_EN)printf("(digui)+++++++++++ str_p:%c  pos:%d  tmp_value:%d\n", *str_p, pos, tmp_value);
        }
        else if(ch == ')' || ch == ']' || ch == '}')
        {
            mark = 1;
            continue;
        }
        if(mark == 1)
            break;
         
        if(str_p[pos] == '\0')
            break;
        else
            pos++;       
    }
    return num[0];
}

int main(void)
{
    //char str[] = "-3-(5+6)*(3-1)";
    //char str[] = "3+1";
    
    char str[512] = {0};
    scanf("%s", str);
    printf("%d\n", calc(str, 1));
    
    return 0;
}

參考

#include <stdio.h>
#include <ctype.h>
int pos;
int compute(char* data)
{
    int len = strlen(data);
    int stack[1000];
    int top = -1;
    char flag = '+';
    int num = 0;
    
    while(pos < len)
    {
        if(data[pos] == '{' || data[pos] == '[' || data[pos] == '(')
        {
            pos++;
            num = compute(data);
        }
        
        while(pos < len && isdigit(data[pos]))
        {
            num = 10 * num + data[pos] - '0';
            pos++;
        }
        
        switch(flag)
        {
            case '+':
                stack[++top] = num;
                break;
            case '-':
                stack[++top] = -num;
                break;
            case '*':
                stack[top] *= num;
                break;
            case '/':
                stack[top] /= num;
                break;
        }
        
        num = 0;
        flag = data[pos];
        
        if(data[pos] == '}' || data[pos] == ']' || data[pos] == ')')
        {
            pos++;
            break;
        }
        pos++;
    }
    
    int res = 0;
    for(int i = 0; i <= top; i++)
    {
        res += stack[i];
    }
    return res;
}


int main()
{
    char data[1000];
    while(scanf("%s", data) != EOF)
    {
        pos = 0;
        int res = compute(data);
        printf("%d\n",res);
    }
    return 0;
}

4断医、圖片整理

注:大小排序

//冒泡法
#include <stdio.h>
#include <stdio.h>
int main(void)
{
    char buf[1024] = {0};
    
    int len;
    int i, j;
    while(scanf("%s", buf) != EOF)
    {
        len = strlen(buf);
        for(i = 0; i < len - 1; i++)
        {
            for(j = 0; j < (len - i - 1); j++)
            {
                if(buf[j] > buf[j + 1])
                {
                    buf[j] ^= buf[j + 1];
                    buf[j + 1] ^= buf[j];
                    buf[j] ^= buf[j + 1];
                }
            }              
        }
        printf("%s\n", buf);    
    } 
    return 0;
}

快排法移植

#include <stdio.h>
#include <string.h>
void swap(char *x, char *y) {
    char t = *x;
    *x = *y;
    *y = t;
}
void quick_sort_recursive(char arr[], int start, int end) {
    if (start >= end)
        return;
    int mid = arr[end];
    int left = start, right = end - 1;
    while (left < right) {
        while (arr[left] < mid && left < right)
            left++;
        while (arr[right] >= mid && left < right)
            right--;
        swap(&arr[left], &arr[right]);
    }
    if (arr[left] >= arr[end])
        swap(&arr[left], &arr[end]);
    else
        left++;
    if (left)
        quick_sort_recursive(arr, start, left - 1);
    quick_sort_recursive(arr, left + 1, end);
}
void quick_sort(char arr[], int len) {
    quick_sort_recursive(arr, 0, len - 1);
}
int main(void)
{
    char buf[1024] = {0};  
    int len;
    int i, j;
    while(scanf("%s", buf) != EOF)
    {
        len = strlen(buf);
        quick_sort(buf, len);
        printf("%s\n", buf);    
    } 
    return 0;
}

參考網(wǎng)友的:位圖標(biāo)記法

#include <stdio.h>
#include <string.h>

int main(){
    char s[1025];
    while (scanf("%s", s)!= EOF){
        int a[1025] ={0};
        int len=strlen(s);
        for(int i=0; i< len; i++)
        {
            a[s[i]]++;
        }
        for(int i=0; i<1025; i++)
        {
            while(a[i]!=0){
                printf("%c",i);
                a[i]--;
            }
        }
        printf("\n");
    }
}

5、蛇形矩陣

#include <stdio.h>
int getNum(int num_start, int step_start, int idx)
{
    int i;
    int tmp, step;
    tmp = num_start;
    step = step_start;
    for(i = 0; i < idx; i++)
    {
        tmp += step;
        step++;        
    }
    return tmp;
}
int main(void)
{
    int i, j;
    int num;
    while(scanf("%d", &num) != EOF)
    {
        for(i = 0; i < num; i++)
        {
            for(j = 0; j < num - i; j++)
            {
                 printf("%d ", getNum(getNum(1, 1, i), 2 + i, j));
            }       
            printf("\n");
        }       
    }
    return 0;
}

或者


#include <stdio.h>
int main(void)
{
    int i, j;
    int num;
    int step  = 1;
    int start = 1; 
    int start2 = 2; 
    int step3  = 2;
    int start3 = 1; 
    
    while(scanf("%d", &num) != EOF)
    {
        for(i = 0; i < num; i++)
        {                     
            for(j = 0; j < num - i; j++)
            {
                if(j == 0)
                {
                    printf("%d ", start3);    
                }
                else
                {
                    printf("%d ", start3 + step3);
                    start3 += step3;
                    step3++;
                }  
            }       
            printf("\n");
            start += step;
            step++;
            start3 = start;
            start2++;
            step3 = start2;                      
        }       
    }
    return 0;
}

6、砝碼稱重

6.1 未通過(guò)(原因超時(shí)孩锡;空間規(guī)模小的話酷宵,還可以計(jì)算)

分析: 用的是窮舉法,二進(jìn)制為1代表數(shù)存在躬窜,0代表不存在浇垦,2的n次方遍歷代表n個(gè)數(shù)的組合個(gè)數(shù)。我發(fā)現(xiàn)荣挨,最近刷的題用這種方法來(lái)解決總是耗時(shí)久男韧,效率不好。

//注: 遍歷次數(shù)超過(guò)2 的 30 次方默垄,耗時(shí)愈加明顯此虑,達(dá)到秒的級(jí)別
#include <stdio.h>
#include <stdlib.h>
#define MAX_GROUP_NUM 10
#define MAX_FAMA_NUM 6
#define MAXFAMA_WIGHT 2000
int strToNumArray(char *str, int *array, int *array_n, int max_num)
{
    int i, j, tmp, cnt;
    char buf[16] = {0};
    i = j = cnt = 0;
    while(str[i] != '\0')
    {
        if(str[i] == ' ')
        {
            i++;
            continue;
        }
        tmp = i;
        while(str[i] != ' ' && str[i] != '\0')
        {
            buf[i - tmp] = str[i];
            i++;
        }
        buf[i - tmp] = '\0';
        array[cnt] = atoi(buf);       
        if(array[cnt] == 0 && buf[0] != '0')
        {
             *array_n = cnt;   
             return -1;   
        }
        cnt++; 
        if(cnt >= max_num)
        {
            *array_n = cnt;   
             return -1;   
        }
    }
    *array_n = cnt;   
     return 0; 
}


int main(void)
{  
    long long long_i, long_j;
    int i, j;
    int val[MAX_GROUP_NUM] = {0};
    int num[MAX_GROUP_NUM] = {0};
    int val_all[MAX_FAMA_NUM * MAX_GROUP_NUM] = {0};
    int group, totle;   
    char str_val[64] = {0};
    char str_num[64] = {0};
    int val_n, num_n;
    int bit_map[MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1] = {0};
    int tmp, cnt;
    
    while(scanf("%d\n", &group) != EOF)
    {     
        gets(str_val);
        gets(str_num);

        strToNumArray(str_val, val, &val_n, MAX_GROUP_NUM);
        strToNumArray(str_num, num, &num_n, MAX_GROUP_NUM);
        totle = 0;
        for(i = 0; i < group; i ++)
        {
             for(j = 0; j < num[i]; j++)
             {
                 val_all[totle] = val[i];
                 totle++;           
             }
        }

       /* for(i = 0; i < group; i ++)
        {
            printf("%d ", val[i]);
        }
        printf("\n");
        for(i = 0; i < group; i ++)
        {
            printf("%d ", num[i]);
        }
        printf("\n");
        for(i = 0; i < totle; i ++)
        {
            printf("%d ", val_all[i]);
        }
        printf("\n");*/


        for(i = 0; i < MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1; i++)
            bit_map[i] = 0;
        for(long_i = 0; long_i < (((long long)1) << totle); long_i++)
        {
            tmp = 0;
            //printf("===>");
            for(j = 0; j < totle; j++)
            {
                if((long_i & ((long long)1 << j)) != 0)
                {
                    //printf("[j=%d]vla:%d ", j, val_all[j]);
                    tmp += val_all[j];     
                }
            }    
            //printf("<==i:0x%x  tmp:%d\n", long_i, tmp);
            bit_map[tmp] = 1;
        }   
        cnt = 0;
        for(i = 0; i < MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1; i++)
        {
            if(bit_map[i] == 1)
            {
                cnt++;
            }
        }
        printf("%d\n", cnt);
    }  
    return 0;
}

6.2 通過(guò):遞歸法,耗時(shí)110ms

注:遇到錯(cuò)誤的點(diǎn):數(shù)組溢出

#include <stdio.h>
#include <stdlib.h>
#define MAX_GROUP_NUM 10
#define MAX_FAMA_NUM 6
#define MAXFAMA_WIGHT 2000
int strToNumArray(char *str, int *array, int *array_n, int max_num)
{
    int i, j, tmp, cnt;
    char buf[16] = {0};
    i = j = cnt = 0;
    while(str[i] != '\0')
    {
        if(str[i] == ' ')
        {
            i++;
            continue;
        }
        tmp = i;
        while(str[i] != ' ' && str[i] != '\0')
        {
            buf[i - tmp] = str[i];
            i++;
        }
        buf[i - tmp] = '\0';
        array[cnt] = atoi(buf);       
        if(array[cnt] == 0 && buf[0] != '0')
        {
             *array_n = cnt;   
             return -1;   
        }
        cnt++; 
        if(cnt >= max_num)
        {
            *array_n = cnt;   
             return -1;   
        }
    }
    *array_n = cnt;   
     return 0; 
}

//多重循環(huán)
int record[100];
int bit_map[MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1] ;
int forLoop(int *end, int n, int top, int *data)
{
    int i;
    int tmp;
    if(end == NULL || data == NULL || top < n)return -1;
    if(n == 0)
    {        
        tmp = 0;
        for(i = 0; i < top - n; i++)
        {
            tmp += data[i] * record[i];    
        }
        bit_map[tmp] = 1;
    }
    else
    {
        for(i = 0; i <= end[top - n]; i++)
        {
            record[top - n] = i; 
            forLoop(end, n - 1, top, data);            
        }     
    }
         
    return 0;
}

int main(void)
{  
    int val[MAX_GROUP_NUM] = {0};
    int num[MAX_GROUP_NUM] = {0};
    char str_val[64] = {0};
    char str_num[64] = {0};
    int val_n, num_n, cnt, i, group;
    
    while(scanf("%d\n", &group) != EOF)
    {     
        gets(str_val);
        gets(str_num);
            
        strToNumArray(str_val, val, &val_n, MAX_GROUP_NUM);
        strToNumArray(str_num, num, &num_n, MAX_GROUP_NUM);
             
        for(i = 0; i < MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1; i++)
            bit_map[i] = 0;
        forLoop(num, group, group, val);
        cnt = 0;
        for(i = 0; i < MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1; i++)
        {
            if(bit_map[i] == 1)
            {
                cnt++;
            }
        }
        printf("%d\n", cnt);
 
    }     
    return 0;
}

6.3 通過(guò)口锭,類似于動(dòng)態(tài)規(guī)劃:利用前者的結(jié)果推導(dǎo)后面的結(jié)果朦前,耗時(shí)10ms

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_GROUP_NUM 10
#define MAX_FAMA_NUM 6
#define MAXFAMA_WIGHT 2000
int strToNumArray(char *str, int *array, int *array_n, int max_num)
{
    int i, j, tmp, cnt;
    char buf[16] = {0};
    i = j = cnt = 0;
    while(str[i] != '\0')
    {
        if(str[i] == ' ')
        {
            i++;
            continue;
        }
        tmp = i;
        while(str[i] != ' ' && str[i] != '\0')
        {
            buf[i - tmp] = str[i];
            i++;
        }
        buf[i - tmp] = '\0';
        array[cnt] = atoi(buf);       
        if(array[cnt] == 0 && buf[0] != '0')
        {
             *array_n = cnt;   
             return -1;   
        }
        cnt++; 
        if(cnt >= max_num)
        {
            *array_n = cnt;   
             return -1;   
        }
    }
    *array_n = cnt;   
     return 0; 
}

int main(void)
{  
    int val[MAX_GROUP_NUM + 2] = {0};
    int num[MAX_GROUP_NUM + 2] = {0};
    char str_val[64] = {0};
    char str_num[64] = {0};
    int val_n, num_n, cnt, i, j, k, group;
    int bit_map[MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1] ;
    int bit_map_tmp[MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1] ;
    
    while(scanf("%d\n", &group) != EOF)
    {     
        gets(str_val);
        gets(str_num);
            
        strToNumArray(str_val, val, &val_n, MAX_GROUP_NUM);
        strToNumArray(str_num, num, &num_n, MAX_GROUP_NUM);
             
        for(i = 0; i < MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1; i++)
            bit_map[i] = 0;
        cnt = 0;
        for(i = 0; i < group; i++)
        {
            memcpy(bit_map_tmp, bit_map, sizeof(bit_map));
            for(j = 0; j < MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1; j++)
            {                
                if(bit_map_tmp[j] == 1)
                {
                    for(k = 0; k <= num[i]; k++)
                    {
                        bit_map[val[i] * k + j] = 1;
                    }
                }
            }
            for(j = 0; j <= num[i]; j++)
            {
                bit_map[val[i] * j] = 1;
            }           
        }
      
        for(i = 0; i < MAXFAMA_WIGHT * MAX_FAMA_NUM * MAX_GROUP_NUM + 1; i++)
        {
            if(bit_map[i] == 1)
            {
                cnt++;
            }
        }
        printf("%d\n", cnt);
 
    }     
    return 0;
}

6.4 參考網(wǎng)友:只要3ms

我的分析:
(1)位圖 1 代表存在, 0代表不存在
(2)以上一個(gè)數(shù)的結(jié)果為基礎(chǔ)鹃操,推導(dǎo)下一個(gè)數(shù)的結(jié)果韭寸,例如
求{2, 2荆隘, 3恩伺, 3, 3} 和的不同情況:
1)0放到位圖椰拒,2放到位圖===>[0,2]
2)2放到位圖晶渠,0+2放到位圖;2+2放到位圖===>[0,2,4]
3) 3燃观、0+3 褒脯、2+3、4+3 放到位圖 ===>[0,2,4,3,5,7]
以此類推缆毁,類似于數(shù)學(xué)的 “演繹推理”番川。

#include <stdio.h>
 
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j,k,m[n],x[n],max=0,count=0;
        for(i=0;i<n;i++)
            scanf("%d",&m[i]);
        for(i=0;i<n;i++)
            scanf("%d",&x[i]);
        for(i=0;i<n;i++)
            max+=m[i]*x[i];
        int *array=(int *)malloc(sizeof(int)*(max+1));
        memset(array,0,sizeof(int)*(max+1));
        array[0]=1;
        for(i=0;i<n;i++)
            for(j=0;j<x[i];j++)
                for(k=max;k>=0;k--)
                    if(array[k]==1)
                        array[k+m[i]]=1;
        for(i=0;i<=max;i++)
            if(array[i]==1)
                count++;
        printf("%d\n",count);
    }
    return 0;
}

7、學(xué)英文(英文數(shù)字)


#include <stdio.h>
#include <string.h>
const char name_of_num[][32]={
    "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
    "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen","eighteen", "nineteen" 
};
const char name_of_numplus[][32]={
     "aa", "aa", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
};
const char name_key[][32]={
    "and", "hundred", "thousand", "million", "billon" 
};

void getNamePre(int num, char (*out_str)[32], int *pos)
{
    int tmp;
    if(num >= 1000) return ;
    tmp = num / 100;
    if(tmp >= 1)
    {
        strcpy(out_str[(*pos)++], name_of_num[tmp]);
        strcpy(out_str[(*pos)++], name_key[1]);
        strcpy(out_str[(*pos)++], name_key[0]);
    }
    tmp = num % 100;
    if(tmp == 0)
    {
         (*pos)--;
         return ;
    }
    tmp = num % 100;
    if((tmp / 10) == 1)
    {
        strcpy(out_str[(*pos)++], name_of_num[tmp]);
    }
    else if((tmp / 10) > 1)
    {
        strcpy(out_str[(*pos)++], name_of_numplus[tmp / 10]);
    } 
    tmp = num % 10;
    if(tmp != 0 && (((num % 100) / 10) != 1))
    {
        strcpy(out_str[(*pos)++], name_of_num[tmp % 10]);
    }           
}

int getName(int num, char (*out_str)[32], int *n)
{
    if(num >= 1000000000)return -1;
    int tmp;
    *n = 0;
    
    tmp = (num / 1000000) % 1000;
    if(tmp > 0)
    {
        getNamePre(tmp, out_str, n);   
        strcpy(out_str[(*n)++], name_key[3]);
    }
    tmp = (num / 1000) % 1000;
    if(tmp > 0)
    {
        getNamePre(tmp, out_str, n);   
        strcpy(out_str[(*n)++], name_key[2]);
    }
    getNamePre(num % 1000, out_str, n);   
    return 0;  
}

void test(int num)
{
    char buf[128][32] = {{0},};
    int n;
    getName(num, buf, &n);
    int i;
    for(i = 0; i < n; i++)
    {
        printf("%s ", buf[i]);
    }
    printf("\n");
}
int main(void)
{
    int num;
    while(scanf("%d", &num) != EOF)
          test(num);
    return 0;
}

8积锅、迷宮問(wèn)題

#include <stdio.h>
#include <string.h>

struct XYPOS{
    int x;
    int y;  
};
void printPath(struct XYPOS *p, int n)
{
    int i;
    for(i = 0; i < n; i++)
    {
        printf("(%d,%d)\n", p[i].y, p[i].x);
    }
    //printf("\n");
}

struct XYPOS record[128] = {0};
int record_pos = 0;
struct XYPOS tmp_record[128] = {0};
int tmp_pos;
int getPath(int *data, int x_top, int y_top, struct XYPOS now)
{
    int i, j;
    struct XYPOS xypos;
    int mark;
    //記錄
    record[record_pos++] = now;   
    
    if((now.x >= 0 && now.x <= x_top) && (now.y >= 0 && now.y <= y_top))
    { 
        for(i = 0; i < 4; i++)
        {
            xypos = now;
            if(i == 0)
            {
                 xypos.x++;
            }
            else if(i == 1)
            {
                xypos.x--;
            }
            else if(i == 2)
            {
                xypos.y++;
            }
            else if(i == 3)
            {
                xypos.y--;
            }
            //禁止環(huán)路
            mark = 0;
            for(j = 0; j < record_pos; j++)
            {
                if(record[j].x == xypos.x && record[j].y == xypos.y)
                {
                    mark = 1;
                    break;
                }
            }
            if(mark == 1)continue;
            //越界
            if(!(xypos.x >= 0 && xypos.x <= x_top) && (xypos.y >= 0 && xypos.y <= y_top))
            {
                continue;
            }
            //遇 1 
            if(data[xypos.y * (1 + x_top) + xypos.x] == 1)
            {
                continue;
            }        
            //最終結(jié)果
            if(xypos.x == x_top && xypos.y == y_top)
            {               
                record[record_pos++] = xypos;                 
                if(tmp_pos > record_pos)//取最短路徑
                {
                    tmp_pos = record_pos;
                    for(j = 0 ; j < record_pos; j++)
                    {
                        tmp_record[j] = record[j];
                    }                    
                }           
                //printPath(record, record_pos);
                record_pos--;              
                continue;
            }   
            //遞歸
            getPath(data, x_top, y_top, xypos);               
        }       
    }
    record_pos--;
    return 0;
}

int main(void)
{  
    int x, y, i, j;
    int data[100] = {0};
    while(scanf("%d %d", &y, &x) != EOF)
    {
        for(i = 0; i < y; i++)
        {
            for(j = 0; j < x; j++)
            {
                scanf("%d ",(data + i * x + j));
            }
            scanf("\n");
        }
        
        struct XYPOS now = {0, 0};
        record_pos = 0;
        tmp_pos = x*y;
        getPath(data, x - 1, y - 1, now);
        printPath(tmp_record, tmp_pos);       
    }
    return 0;
}

9爽彤、數(shù)獨(dú)問(wèn)題
(部分用例通過(guò) 2/6,,通過(guò)率30%)



#include <stdio.h>

#define TEST -1
int data[81] = {0};
int bit[81] = {0};
int stop = 0;
void printData(int *data)
{
    int i;
    for(i = 0; i < 81; i ++)
    {
        printf("%d ", *(data + i));
        if((i + 1) % 9 == 0)
        {
            printf("\n");
        }  
    }
}
int setnum(int n)
{
    int i, j, x, y, mark, tmp_data, tmp_bit;
    y = n / 9;
    x = n % 9;
    
    if(stop == 1)
    {
        return 0;
    }
    
    if(n >= 80)
    {
        //printf("OK\n");
        printData(data);
        //stop = 1;
        return 0;
    }
    if(data[n] == 0)
    {
        if(n == TEST)printf("TEST = %d is coming.............[0]\n", TEST);
        for(i = 0; i < 9; i++)
        {
            if(bit[y * 9 + i] == 0)
            {
                if(n == TEST)printf("==>[select] num=%d\n", i + 1);
                for(j = 0 ; j < 9 ; j ++)
                {
                     mark = 0;
                     
                     if(data[x + j * 9] == (i + 1) && y != j)
                     {
                         if(n == TEST)printf("==>[not ok](%d,%d) data=%d\n", j, x, i + 1);
                         mark = 1;
                         break;
                     }                    
                }
                if(mark == 1)
                    continue;
                if(n == TEST)printf("==>[ok] data=%d\n", i + 1);
                tmp_data = data[n];
                data[n] = i + 1;
                tmp_bit = bit[y * 9 + i];
                bit[y * 9 + i] = 1;
                setnum(n + 1);
                //回退
                data[n] = tmp_data;
                bit[y * 9 + i] = tmp_bit;
            }                        
        }  
    }
    else
    {
        if(n == TEST)printf("TEST = %d is coming.............[1]\n", TEST);
        setnum(n + 1);
    }
    return  0;
}

int main(void)
{
    int i;
    for(i = 0; i < 81; i ++)
        bit[i] = 0;
    for(i = 0; i < 81; i ++)
    {
        scanf("%d", data + i);
        if((i + 1) % 9 == 0)
            scanf("\n");    
        if(*(data + i) != 0)        
            bit[(i / 9) * 9 + (*(data + i) - 1)] = 1;
    }
    //printData(bit);
    
    stop = 0;
    setnum(0);
   
    return 0;
}

10缚陷、漂亮數(shù)字
python 例子(寫(xiě)代碼速度確實(shí)更快适篙,用到了現(xiàn)成的“輪子”)

num = input()
while True:
    try:
        sum = 0
        mystr = input().strip().lower()
        str_len = len(mystr)
        l = [];
        value_a = ord('a')
        for i in range(26):
            tmp = mystr.count(chr(value_a + i))
            if tmp != 0:
                l.append(tmp)
        l = sorted(l, reverse=True)
        for i in range(len(l)):
            sum += l[i] * (26 - i)
        if(sum != 0):
            print(sum)      
    except:
        break

c語(yǔ)言



#include <stdio.h>
#include <string.h>
/*思路
*統(tǒng)計(jì)字符出現(xiàn)的次數(shù),然后排序箫爷,次數(shù)大的給數(shù)值26嚷节,次之給25聂儒,依次類推,最后求和
*/

void sort(int *p, int n)//冒泡排序
{
    int i, j;
    for(i = 0; i < n -1; i++)
        for(j = i + 1; j < n; j ++)
            if(p[i] < p[j])
                {p[i] ^= p[j];p[j] ^= p[i]; p[i] ^= p[j];}   
}

int main(void)
{
    int num, i, j, len, cnt, sum;
    int bit[26] = {0};
    int array[26] = {0};
    char buf[4096] = {0};
    while(scanf("%d", &num) != EOF)
    {
        for(i = 0; i < num; i++)
        {
            scanf("%s\n", buf);

            len = strlen(buf);
            for(j = 0; j < 26; j++)
                bit[j] = 0;
            for(j = 0; j < len; j++)
            {
                if(buf[j] >= 'a' && buf[j] <= 'z')
                    bit[buf[j] - 'a']++;
                else if(buf[j] >= 'A' && buf[j] <= 'Z')
                    bit[buf[j] - 'A']++;
            }
            cnt = 0;
            for(j = 0; j < 26; j++)
                if(bit[j] != 0)
                    array[cnt++] = bit[j];
            if(cnt == 0)
                continue;
            sort(array, cnt);
            sum = 0;
            for(j = 0; j < cnt; j++)
                sum += array[j] * (26 - j);
            printf("%d\n", sum);
        }  
    }
    return 0;
}

11硫痰、鏈表


#include <stdio.h>
#include <stdlib.h>
struct myque{
    int val;
    struct myque *next;   
};
#define NEW_NODE (struct myque *)malloc(sizeof(struct myque));
#define NULL_THEN_RETURN do{if(head == NULL) return;}while(0);
void que_print(struct myque *head)
{
    NULL_THEN_RETURN;
    struct myque *prob = head;
    while(prob != NULL)
    {
        printf("%d ", prob->val);
        prob = prob->next;         
    }  
    printf("\n");
    
}
struct myque * que_init(int val)
{
    struct myque * q = NEW_NODE;
    q->val = val; 
    q->next = NULL;                                       
    return q;
}
void que_insert(struct myque *head, int pos, int val)
{
    NULL_THEN_RETURN;
    struct myque *prob = head;
    struct myque *tmp;
    if(pos == val)
        return;
    while(prob != NULL)
    {
        if(prob->val == pos)
        {
            tmp = prob->next; 
            prob->next = NEW_NODE;
            prob->next->val = val;
            prob->next->next = tmp;
            break;
        }
        prob = prob->next;         
    }
}
void que_del(struct myque *head, int val)
{
    NULL_THEN_RETURN;
    struct myque *prob = head;
    struct myque *last = head;
    struct myque *tmp;
    while(prob != NULL)
    {
        if(prob->val == val)
        {         
            if(prob == head)
            {
                tmp = head->next;
                *head = *(head->next);
                free(tmp);
                return;
            }
            last->next = prob->next;
            free(prob);
            return;
        }
        last = prob;
        prob = prob->next;         
    }  
}

int main(void)
{
    int num, head_v, i, val, pos, del_v;
    
    while(scanf("%d", &num) != EOF)
    {
        scanf("%d", &head_v);  
        struct myque *head = que_init(head_v);
        for(i = 0; i < num - 1; i++) 
        {
            scanf("%d %d", &val, &pos); 
            que_insert(head, pos, val); 
        }
        scanf("%d\n", &del_v); 
        que_del(head, del_v);
        que_print(head);       
    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末衩婚,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子效斑,更是在濱河造成了極大的恐慌非春,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缓屠,死亡現(xiàn)場(chǎng)離奇詭異奇昙,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)敌完,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)储耐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人滨溉,你說(shuō)我怎么就攤上這事什湘。” “怎么了晦攒?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵闽撤,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我勤家,道長(zhǎng)腹尖,這世上最難降的妖魔是什么柳恐? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任伐脖,我火速辦了婚禮,結(jié)果婚禮上乐设,老公的妹妹穿的比我還像新娘讼庇。我一直安慰自己,他們只是感情好近尚,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布蠕啄。 她就那樣靜靜地躺著,像睡著了一般戈锻。 火紅的嫁衣襯著肌膚如雪歼跟。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,718評(píng)論 1 305
  • 那天格遭,我揣著相機(jī)與錄音哈街,去河邊找鬼。 笑死拒迅,一個(gè)胖子當(dāng)著我的面吹牛骚秦,可吹牛的內(nèi)容都是我干的她倘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼作箍,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼硬梁!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起胞得,我...
    開(kāi)封第一講書(shū)人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤荧止,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后阶剑,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體罩息,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年个扰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瓷炮。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡递宅,死狀恐怖娘香,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情办龄,我是刑警寧澤烘绽,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站俐填,受9級(jí)特大地震影響安接,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜英融,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一盏檐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧驶悟,春花似錦胡野、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至笼呆,卻和暖如春熊响,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背诗赌。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工汗茄, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人境肾。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓剔难,卻偏偏與公主長(zhǎng)得像胆屿,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子偶宫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容