試題2:利用數組下標存元素值以去重

試題一描述:
明明想在學校中請一些同學一起做一項問卷調查,為了實驗的客觀性,他先用計算機生成了N個1到1000之間的隨機整數(N≤1000),對于其中重復的數字,只保留一個,把其余相同的數去掉,不同的數對應著不同的學生的學號。然后再把這些數從小到大排序,按照排好的順序去找同學做調查。請你協助明明完成“去重”與“排序”的工作(同一個測試用例里可能會有多組數據,希望大家能正確處理)。
輸入多行,先輸入隨機整數的個數,再輸入相應個數的整數
返回多行,處理后的結果

c語言解法(一)

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

void restruct_array(int *a, int n)
{
    int tmp[1024] = {0};
    int i = 0;

    for(i = 0; i < n; i++)
    {
        tmp[a[i]] = 1;
    }

    for(i = 0; i < 1024; i++)
    {
        if(tmp[i] == 1)
            printf("%d\n", i);
    }
}
int main()
{
    int N;
    while(~scanf("%d", &N))
    {
        int i = 0;
        int *a = (int *)malloc(sizeof(int) * N);
        for(i = 0; i < N; i++)
        {
            scanf("%d", &a[i]);
        }

        restruct_array(a, N);
        free(a);
    }
    system("Pause");
    return 0;
}

c語言解法(二)

int del_same(int *a, int n)
{
    int i, j, k = 0;
    for(i = 0; i < n; i++)
    {
        for(j = i + 1; j < n && a[j] != a[i]; j++);

        if(j == n)
            a[k++] = a[i];

    }
    return k;
}

void sorted_array(int *a, int n)
{
    int i, j = 0;
    for(i = 0; i < n - 1; i++)
    {
        for(j = 0; j < n - i -1; j++)
        {
            int tmp = 0;
            if(a[j] > a[j+1])
            {
                tmp = a[j+1];
                a[j+1] = a[j];
                a[j] = tmp;
            }
        }
    }
}

int main()
{
    int N;
    while(~scanf("%d", &N))
    {
        int i = 0;
        int *a = (int *)malloc(sizeof(int) * N);
        for(i = 0; i < N; i++)
        {
            scanf("%d", &a[i]);
        }

        //restruct_array(a, N);
        int N1 = del_same(a, N);
        sorted_array(a, N1);
        for(i = 0; i < N1; i++)
        {
            printf("%d\n", a[i]);
        }
        free(a);
    }
    system("Pause");
    return 0;
}

python解法

import sys
while True:
    try:
        num=input()
        num_list=[]
        for i in range(num):
            input_num=sys.stdin.readline()
            num_list.append(int(input_num[:-1]))
        num_list=sorted(list(set(num_list)))
        for i in num_list:
            print i
    except:
        break

試題2:數據表記錄包含表索引和數值,請對表索引相同的記錄進行合并,即將相同索引的數值進行求和運算,輸出按照key值升序進行輸出。
輸入描述:
先輸入鍵值對的個數
然后輸入成對的index和value值,以空格隔開
輸出描述:
輸出合并后的鍵值對(多行)

C語言代碼

#include <stdio.h>

int main()
{
    int num;
    scanf("%d", &num);
    int* arr1 = (int*)malloc(sizeof(int) * num);
    int* arr2 = (int*)malloc(sizeof(int) * num);
    int tmp, tmp1;
    int count = num;
    int i = 0;
    for(i = 0; i < num; i++)
    {
        scanf("%d%d", &tmp, &tmp1);
        if(arr1[tmp])
            arr2[tmp] = arr2[tmp] + tmp1;
        else
        {
            arr2[tmp] = tmp1;
            arr1[tmp] = 1; //將索引值對應的數組元素賦值1,從而可以根據數組元素為1的下標值找到索引值
        }
    }
    
    for(i = 0; i < num; i++)
    {
        if(arr1[i])
        {
            printf("%d %d\n", i, arr2[i]);
        }
    }
}

試題3:輸入一個int型整數,按照從右向左的閱讀順序,返回一個不含重復數字的新的整數。
輸入:9876673
輸出:37689

C語言代碼

#include <stdio.h>

void find_new(int num)
{
    int i = 0;
    int j = 10;
    int arr[1024] = {0};
    while(num % 10 < 10 && num > 0)
    {
        int a;
        a = num % 10;
        num = num / 10;
        
        if(!arr[a])
        {
            arr[a] = 1;  //同樣的將數組元素置1,然后通過數組下標存放元素值以去重
            printf("%d", a);
        }
            
        //j = j * 10;
    }
}
int main()
{
    int num;
    scanf("%d", &num);
    find_new(num);
}

試題4:編寫一個函數,計算字符串中含有的不同字符的個數。字符在ACSII碼范圍內(0~127)。不在范圍內的不作統計。
輸入:abc
輸出:3

C語言代碼

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

void counter(char* p, int N)
{
    int a[1024] = {0};
    int count = N;
    int i = 0;
    while(p[i] != '\0')
    {
        int tmp = (int)p[i];
        if(tmp >= 0 && tmp <= 127)
        {
            if(!a[tmp])
            {
                a[tmp] = 1;
            }
            else
            {
                count--;
            }
        }
        else
        {
            count--;
        }
        i++;
    }
    printf("%d\n", count);
}
int main()
{

    char* str = (char*)malloc(sizeof(char) * 1024);
    gets(str);
    int N = strlen(str);
    
    counter(str, N);
    
    return 0;
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在C語言中,五種基本數據類型存儲空間長度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來閱讀 3,392評論 0 2
  • 一、Python簡介和環境搭建以及pip的安裝 4課時實驗課主要內容 【Python簡介】: Python 是一個...
    _小老虎_閱讀 5,804評論 0 10
  • 1.把二元查找樹轉變成排序的雙向鏈表 題目: 輸入一棵二元查找樹,將該二元查找樹轉換成一個排序的雙向鏈表。 要求不...
    曲終人散Li閱讀 3,357評論 0 19
  • 官網 中文版本 好的網站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,438評論 0 5
  • 1:thrift 服務間調用 1.1 pom.xml添加如下的依賴## 1.2 添加spring-client.x...
    EddieZhang閱讀 826評論 1 0