zoukankan      html  css  js  c++  java
  • 算法学习--Day3

    今天搞了一波算法的哈希,代码难道不大,记录在这里吧。

    题目描述

        “臭味相投”——这是我们描述朋友时喜欢用的词汇。两个人是朋友通常意味着他们存在着许多共同的兴趣。然而作为一个宅男,你发现自己与他人相互了解的机会并不太多。幸运的是,你意外得到了一份北大图书馆的图书借阅记录,于是你挑灯熬夜地编程,想从中发现潜在的朋友。     首先你对借阅记录进行了一番整理,把N个读者依次编号为1,2,…,N,把M本书依次编号为1,2,…,M。同时,按照“臭味相投”的原则,和你喜欢读同一本书的人,就是你的潜在朋友。你现在的任务是从这份借阅记录中计算出每个人有几个潜在朋友。

    输入描述:

        每个案例第一行两个整数N,M,2 <= N ,M<= 200。接下来有N行,第i(i = 1,2,…,N)行每一行有一个数,表示读者i-1最喜欢的图书的编号P(1<=P<=M)

    输出描述:

        每个案例包括N行,每行一个数,第i行的数表示读者i有几个潜在朋友。如果i和任何人都没有共同喜欢的书,则输出“BeiJu”(即悲剧,^ ^)
    示例1

    输入

    4  5
    2
    3
    2
    1
    

    输出

    1
    BeiJu
    1
    BeiJu

    
    #include <stdio.h>
    int hash[201];
    int main(){
        int n;
        while (scanf("%d",&n)!=EOF){
            int m;
            int save_array[10000];
    
    
            scanf("%d",&m);
            for(int i=0;i<n;i++){
                scanf("%d",&save_array[i]);
                hash[save_array[i]]++;
            }
    
            for(int j=0;j<n;j++){
                if(hash[save_array[j]]>1) printf("%d
    ",hash[save_array[j]]-1);
                else printf("BeiJu
    ");
            }
        }
    
        return 0;
    }

    题目描述

        有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L+1棵树。     现在要移走一些树,移走的树的区间用一对数字表示,如 100 200表示移走从100到200之间(包括端点)所有的树。     可能有M(1<=M<=100)个区间,区间之间可能有重叠。现在要求移走所有区间的树之后剩下的树的个数。

    输入描述:

        两个整数L(1<=L<=10000)和M(1<=M<=100)。
        接下来有M组整数,每组有一对数字。

    输出描述:

        可能有多组输入数据,对于每组输入数据,输出一个数,表示移走所有区间的树之后剩下的树的个数。
    示例1

    输入

    500 3
    100 200
    150 300
    470 471
    

    输出

    298

    #include <stdio.h>
    #include <iostream>
    int main(){
        int n;
        while (scanf("%d",&n)!=EOF){
            int times;
            int hash[10001]={1};
            for(int z=0;z<=n;z++){
    
                hash[z]=1;
            }
            std::cin>>times;
            for (int i = 0; i < times; i++) {
                int x,y;
                std::cin>>x>>y;
                for (int j = x; j <= y; j++) {
                    hash[j]=0;
                }
            }
            int fin=0;
            for(int z=0;z<=n;z++){
                if(hash[z]==1) fin++;
    
            }
    std::cout<<fin<<std::endl;
        }
        return 0;
    }

    题目描述

    You are given an unsorted array of integer numbers. Your task is to sort this array and kill possible duplicated elements occurring in it.

    输入描述:

    For each case, the first line of the input contains an integer number N representing the quantity of numbers in this array(1≤N≤1000). Next N lines contain N integer numbers(one number per each line) of the original array.

    输出描述:

    For each case ,outtput file should contain at most N numbers sorted in ascending order. Every number in the output file should occur only once.
    示例1

    输入

    6
    8 8 7 3 7 7
    

    输出

    3 7 8

    #include <algorithm>
    #include <stdio.h>
    
    bool cmp(int a, int b){
        return a<b;
    }
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            int array[1001];
            for(int i=0;i<n;i++){
                scanf("%d",&array[i]);
            }
        std::sort(array,array+n,cmp);
            printf("%d",array[0]);
            for(int i=1;i<n;i++){
                if(array[i]!=array[i-1]) printf(" %d",array[i]);
                if(i==n-1) printf("
    ");
    
            }
    
        }
        return 0;
    }

    题目描述

    读入N名学生的成绩,将获得某一给定分数的学生人数输出。

    输入描述:

    测试输入包含若干测试用例,每个测试用例的格式为
    
    
    第1行:N
    第2行:N名学生的成绩,相邻两数字用一个空格间隔。
    第3行:给定分数
    
    当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。

    输出描述:

    对每个测试用例,将获得给定分数的学生人数输出。
    示例1

    输入

    3
    80 60 90
    60
    2
    85 66
    0
    5
    60 75 90 55 75
    75
    0
    

    输出

    1
    0
    2

    #include <stdio.h>
    
    int main(){
        int n;
        while (scanf("%d",&n)!=EOF&&n!=0){
            int hash[101]={0};
            for(int i=0 ;i<n;i++){
                int x;
                scanf("%d",&x);
                hash[x]++;
            }
    
            int a;
            scanf("%d",&a);
            printf("%d
    ",hash[a]);
        }
        return 0;
    }
  • 相关阅读:
    【hdu 2222】Keywords Search
    【codeforces 750E】New Year and Old Subsequence
    Bit error testing and training in double data rate (ddr) memory system
    北风网09.接收普通消息2
    北风网08.接收普通消息1
    慕课网消息的接收与响应2
    MyEclipse/eclipse 添加作者、注释、版本、时间等
    北风网开发者接入
    java.lang.Error: Unresolved compilation problem: 解决方案
    北风网环境搭建二
  • 原文地址:https://www.cnblogs.com/Pinging/p/8859091.html
Copyright © 2011-2022 走看看