某知名網校筆試題精選 - 9.2

9.2 晚做了某個著名網校的筆試,有幾題感覺不錯就分享出來了,看完應該會有點收穫的


小題目:
(順序有點打亂了,我只挑了我認爲值得一看的題目分享)

(1)假設32位計算機程序:

short a = 32767*2;

unsigned short b = a;  //cout<<b<<endl;

判斷b的輸出?

有點搞不明白這個題目的意圖,是想問有沒有溢出,還說想說有符號到無符號數的轉變?我覺得就是b=a,一樣一樣的

(2)a的值最後是多少?

int a;

a=5?0:1;

cout<<a;

最後是0,這裏很容易把a誤解成5了,這是對三目運算符的考察

(3)x=9999的輸出值

 int func(int x)
{
int count=0;
while(x)
{
	count++;
	x=x&(x-1);
}
return count;
 }

輸出值是8,其實x=x&(x-1)相當於是檢查x換算成二進制的位數

(4)先序序列爲a,b,c,d的不同二叉樹的個數是多少?

(5)這段代碼一共循環了多少次?

    int k=2000;
    int c=0;
    while(k>1)
    {
    	k=k>>1;
    	c++;
	}
	cout<<c;

輸出是10,>>右移相當於是除2

(6)判斷這段程序的輸出:

#include <iostream>

using namespace std;

int main()
{
	int a=1,b=2,c=3,d=0;
	
	if(a==1 && b++ ==2)
	   if(b!=2 || c--!=3)
	   	    cout<<a<<b<<c;
       else
       		cout<<a<<b<<c;
	else
	    cout<<a<<b<<c;
	
	return 0;
}

這需要注意一個點: 對於||語句來說,如果前面的條件正確,那麼後面的判斷就不執行了


兩道編程題:

第一題

給定一個整數鏈表,輸出不含重複數字的鏈表,鏈表包含負數,比較絕對值,並且輸出的元素都是絕對值

分析:
這個題目,看似和鏈表有關,其實給的輸入就是一串數字,而已,所有和鏈表的處理並沒有關係啦,我這裏才用了數組下標來做是否有重複的判斷,然後在按序輸出即可!
我把數組大小設置爲一個給定的值,因爲感覺數據量也不會太大(題目沒有說明邊界),然後真的直接AC了

AC代碼

/*********************
typedef struct node {
      int data;
      struct node  *link;
} NODE;
 

typedef NODE* PNODE;

**********************/

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn=10000+10;

int main()
{
    int n;
    int a[maxn]={0};

    while(scanf("%d",&n)!=EOF)
    {
        if(n<0)
            n=abs(n);
        if(a[n]==0)
        {
            cout<<n<<" ";
            a[n]++;
        }
    }
   
    return 0;
}

第二題

假設今年網校有N個校招生以及筆試對應的分數,尋找這前TopN 0.1%(向上取整),假設網校的校招的總人數不超過100 0000
輸入:
校招總人數
分數
輸出:
top0.1%的分數

分析:
該題一看,便知道了是對N個分數進行排序的問題;題目要求了必須是C語言編寫,且提示了用堆,題目的數據量較大,因此我這裏就才用堆排序的方法,找出前TopN的分數

(因爲時間有限,當時沒有把這道題目完整得A出來,只過了測試用例的百分之67,我猜測應該是沒有向上取整的問題)

##AC代碼

#include <stdio.h>
#include <stdlib.h>

int a[1000000+10];

void swap(int *arr,int i, int k) 
{
    int temp = arr[i];
    arr[i] = arr[k];
    arr[k] = temp;
}


void max_heapify(int *arr, int start, int end) 
{
    //建立父節點下標和子節點下標
    int dad = start;

    int son = dad * 2 + 1;

    while (son <= end) 
    {   //若子節點下標在範圍內才做比較
        if (son + 1 <= end && arr[son] < arr[son + 1]) //先比較兩個子節點大小,選擇最大的
        {
            son++;
        
        }

        if (arr[dad] > arr[son]) //如果父節點大於子節點代表調整完畢,直接跳出
        {
            return;
        }   
        else 
        {   //否則交換父子節點的值再繼續左右子節點值得比較
            swap(arr,dad, son);
           // printf("dad=%d--son=%d\n",dad,son);
            dad = son;
            son = dad * 2 + 1;
        }
            
    }
}

void heap_sort(int *arr, int len) 
{
    int i;
    //初始化,i從最後一個父節點開始調整
    for (i = len / 2 - 1; i >= 0; i--)
    {
        max_heapify(arr, i, len - 1);
        
    }

    for (i = len - 1; i > 0; i--) 
    {
        swap(arr,0, i);

        max_heapify(arr, 0, i - 1);
    }
}



int main(int argc, char const *argv[])
{
    // int arr[] = {5,3,8,1,6};

    //int len = sizeof(arr) / sizeof(int);

    int n; scanf("%d",&n);
    for (int i = 0; i < n; ++i)
    {
        scanf("%d",&a[i]);
    }

    heap_sort(a, n);

    int top = n*0.001;

    for (int i = 0; i < top; i++)
    {
        printf("%d\n", a[i]);
    }
        
    printf("\n");
    
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章