第二講 線性結構

来源:http://www.cnblogs.com/VincentValentine/archive/2017/05/08/6828576.html
-Advertisement-
Play Games

02 線性結構2:一元多項式的乘法與加法運算 Description: 設計函數分別求兩個一元多項式的乘積與和。 Input: 輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入一個多項式非零項繫數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。 Output: 輸出分2 ...


02-線性結構2:一元多項式的乘法與加法運算
Description:

設計函數分別求兩個一元多項式的乘積與和。

Input:

輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入一個多項式非零項繫數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。

Output:

輸出分2行,分別以指數遞降方式輸出乘積多項式以及和多項式非零項的繫數和指數。數字間以空格分隔,但結尾不能有多餘空格。零多項式應輸出0 0。

SampleInput:

4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1

SampleOutput:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

Codes:
//#define LOCAL

#include <cstdio>

#define M 2010
int A[M], B[M], C[M], D[M];

void add() {
    int i, j, f = 0;
    for(i=0; i<M; ++i)
        C[i] = A[i]+B[i];
    for(i=M; i>=0; --i) {
        if(C[i] != 0) {
            if(!f) f = 1;
            else printf(" ");
            printf("%d %d", C[i], i);
        }
    }
    if(f == 0) printf("0 0");
    printf("\n");
}

void mul() {
    int i, j, f = 0;
    for(i=0; i<M; ++i) 
        for(j=0; j<M; ++j)
            D[i+j] += A[i]*B[j];
     for(i=M; i>=0; --i) {
        if(D[i] != 0) {
            if(!f) f = 1;
            else printf(" ");
            printf("%d %d", D[i], i);
        }
    }
    if(f == 0) printf("0 0");
    printf("\n");
}

int main()
{
    #ifdef LOCAL
        freopen("E:\\Temp\\input.txt", "r", stdin);
        freopen("E:\\Temp\\output.txt", "w", stdout);
    #endif

    int n1, n2, i, p, q;
    scanf("%d", &n1);
    for(i=0; i<n1; ++i) {
        scanf("%d%d", &q, &p);
        A[p] = q;
    }
    scanf("%d", &n2);
    for(i=0; i<n2; ++i) {
        scanf("%d%d", &q, &p);
        B[p] = q;
    }

    mul(); add();

    return 0;
}
PAT-1074:Reversing Linked List.
Description:

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.

Input:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

SampleInput:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

SampleOutput:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

Codes:
//#define LOCAL

#include <cstdio>
#include <algorithm>

#define M 100010
struct Node { int d, p; };
Node A[M]; int B[M];

int main()
{
    #ifdef LOCAL
        freopen("E:\\Temp\\input.txt", "r", stdin);
        freopen("E:\\Temp\\output.txt", "w", stdout);
    #endif

    int a, b, c, f, n, i, k, j = 0;
    scanf("%d%d%d", &f, &n, &k);
    for(i=0; i<n; ++i) {
        scanf("%d%d%d", &a, &b, &c);
        A[a].d = b, A[a].p = c;
    }
    
    while(f != -1) { B[j++] = f; f = A[f].p; } i = 0;
    while(i+k <= j) { std::reverse(&B[i], &B[i+k]); i+=k; }

    for(i=0; i<j-1; ++i) printf("%05d %d %05d\n", B[i], A[B[i]].d, B[i+1]);
    printf("%05d %d -1\n", B[i], A[B[i]].d);

    return 0;
}
PAT-1051:Pop Sequence.
Description:

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

Input:

Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.

Output:

For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.

SampleInput:

5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

SampleOutput:

YES
NO
NO
YES
NO

Codes:
//#define LOCAL

#include <cstdio>

#define M 1010
int A[M], B[M];

int main()
{
    #ifdef LOCAL
        freopen("E:\\Temp\\input.txt", "r", stdin);
        freopen("E:\\Temp\\output.txt", "w", stdout);
    #endif

    int m, n, k, i, j, t;
    scanf("%d%d%d", &m, &n, &k);
   
    while(k--) {
        for(i=0; i<n; ++i) scanf("%d", &A[i]);
        j = 1, i = t = 0;
        while(i < n) {
            if(A[i] == j) ++i, ++j;
            else if(B[t] == A[i]) ++i, --t;
            else if(t < m-1) B[++t] = j++;
            else break;
        }
        if(i == n) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 廣義的網站的監控涵蓋所有的非業務行為的數據採集與管理,包括數據分析師和產品設計師使用的網站用戶行為日誌、業務運行數據,以及供運維工程師和開發工程師使用的性能統計數據等。 本文主要是通過shell腳本來收集伺服器性能指標,如系統load、記憶體占用、磁碟IO、CPU占用,並將其寫入一個文件中,及時判斷應 ...
  • 最近做了一個使用 C# 寫了一個發送郵件的 windows 服務,在這裡記錄一下。 首先使用 Visual Studio 2015 創建一個 windows 服務項目。 然後在設計器上面右擊添加安裝程式。如下圖。 安裝好後,選擇安裝程式設計界面,選擇服務和安裝程式右擊選擇屬性修改一些屬性值。 PS: ...
  • 文檔目錄 本節內容: 簡介 IObjectMapper 介面 集成 AutoMapper 安裝 創建映射 自動映射的特性 自定義映射 擴展方法 MapTo 單元測試 預定義的映射 LocalizableString -> string 註入 IMapper 安裝 創建映射 自動映射的特性 自定義映射 ...
  • 《Effective C#》快速筆記 - C# 高效編程要點補充 目錄 四十五、儘量減少裝箱拆箱 四十六、為應用程式創建專門的異常類 四十七、使用強異常安全保證 四十八、儘量使用安全的代碼 四十九、實現與 CLS 相容的程式集 五十、實現小尺寸、高內聚的程式集 這是這一系列的最後一篇。 四十五、儘量 ...
  • .NET數據訪問 在.NET中對於數據的訪問大致有三個層面,數據訪問層、記憶體數據集、業務邏輯層。數據層,包括了XML配置文件以及一些常用的資料庫(使用SQL語句);記憶體數據集,主要是DataSet數據集,在DataSet中包括Datatable,而Datatable中又分為DataRow和DataC ...
  • 首先要準備一下的工具作為環境 MySQL Community Server 5.7.x My Workbench 6.3 VS2017 新建一個項目,NetMySQLCodeFirst 選擇MVC,再選擇無用戶驗證 然後通過NuGet包管理器安裝三個包,安裝最新穩定版本即可 EntityFramew ...
  • MVC中這樣實現。。。 ...
  • 前言: 該博客產生的背景是客戶那邊有部署網站的方法是iis authentication身份驗證,而系統中使用Webclient來調用別的系統的方法。在此情況下,原本可以使用的功能,都不能調用方法了,返回結果是401。在經過艱難地查找資料之後,也沒有找到很好的方法來解決。找到的幾個方法也不能很好的解 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...