天天看點

算法競賽入門【碼蹄集新手村600題】(MT1501-1550)算法競賽入門【碼蹄集新手村600題】(MT1501-1550)前言目錄

算法競賽入門【碼蹄集新手村600題】(MT1501-1550)

(文章目錄)

前言

為什麼突然想學算法了?

> 用較為“官方”的語言講,是因為算法對計算機科學的所有分支都非常重要。 在絕大多數的計算機科學分支領域中,要想完成任何實質性的工作,了解算法的基礎知識并掌握與算法密切相關的資料結構知識是必不可少的。

> 但從實際而言,是因為當下快到了考研和找工作的年紀(ಥ_ಥ),無論走哪一條路,都不免需要一些相對豐富的算法知識,是故,便産生了一個暑假速成算法的計劃,可能對于像我這種算法競賽小白而言,幾乎很難,但我仍然還是想嘗試一下,畢竟,夢想還是要有的,萬一實作了呢?~( ̄▽ ̄~)~

算法競賽入門【碼蹄集新手村600題】(MT1501-1550)算法競賽入門【碼蹄集新手村600題】(MT1501-1550)前言目錄

為什麼選擇碼蹄集作為刷題軟體?

碼蹄集,是在全國高等學校計算機教學與産業實踐資源建設專家委員會(TIPCC) 指導下建設的,其依托全國各大名校計算機系和清華大學出版社等機關的強大資源,旨在為計算機學習愛好者提供全面和權威的計算機習題。
算法競賽入門【碼蹄集新手村600題】(MT1501-1550)算法競賽入門【碼蹄集新手村600題】(MT1501-1550)前言目錄

目錄

1. MT1501 字元串轉換整數

(1)題目描述

将一個字元串(含數字、符号,長度小于8)轉換成整型數,需要考慮符号,如果無法轉換(沒有數字)則輸出結果為FAIL。本題不考慮小數。

格式

輸入格式: 輸入字元串,第一個字元為+或者-

.

輸出格式: 輸出為整型(整數不用輸出+号)

樣例1

輸入格式: -123

.

輸出格式: -123

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;
typedef long long ll;
#define MAX_NUM 10005
#define PI 3.1415926

double res;
int ans,n,m,t,k,len,cnt=0,minn=MAX_NUM,maxx=0;
char ch,s1[MAX_NUM] = "",s2[MAX_NUM]="";
bool flag = true;
int a[MAX_NUM] = {0} , b[MAX_NUM] = {0};

int main(){
    cin.getline(s1,MAX_NUM);
    len = strlen(s1);
    if(len==1){
        cout << "FAIL" << endl;
    } else {
        n = atoi(s1);
        cout << n << endl;
    }

    return 0;
}
           

2. MT1502 字元串轉換浮點數

(1)題目描述

将一個數字字元串(不超過正常double範圍)轉換成浮點型數,需要考慮是否有正負符号開頭、是否有且隻有一個小數點,如果是非法的數結果為0。本題不考慮其他進制等情況。

格式

輸入格式: 輸入字元串(長度小于80)

.

輸出格式: 輸出為浮點型。非法資料輸出0。

樣例1

輸入格式: -123.5

.

輸出格式: -123.500000

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main(){
    char a[80];
    cin.getline(a,80);

    double ret = 0;
    int flag;
    if(a[0]=='+') flag = 1;
    if(a[0]=='-') flag = -1;
    bool flag2 = false;
    int cnt = 1;
    int j = (a[0] == '+'||a[0]=='-' ? 1:0);

    for(int i = j; i < strlen(a); i++) {
        if((a[i]>'9' || a[i]<'0')&&a[i]!='.'){
            ret = 0;
            break;
        }

        if(a[i]=='.' && flag2 == true){
            ret = 0;
            break;
        }

        if(a[i] == '.' && flag2 == false){
            flag2 = true;
            continue;
        }

        if(!flag2){
            ret = ret*10 + a[i] - '0';
        }else {
            ret = ret * 10 + a[i] - '0';
            cnt *= 10;
        }

    }
    if(ret == 0)  printf("0");
    else printf("%lf",ret/cnt*flag);
    return 0;

}
           

3. MT1503 中心對稱

(1)題目描述

編寫程式,輸入一個字元串,判斷一個字元串是否中心對稱,如“AAXAA”。

格式

輸入格式: 輸入字元串

.

輸出格式: 輸出為YES或者NO

樣例1

輸入格式: AAXAA

.

輸出格式: YES

備注:

字元串中可能含有空格。

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main(){
    char a[80];
    cin.getline(a,80);

    double ret = 0;
    int flag;
    if(a[0]=='+') flag = 1;
    if(a[0]=='-') flag = -1;
    bool flag2 = false;
    int cnt = 1;
    int j = (a[0] == '+'||a[0]=='-' ? 1:0);

    for(int i = j; i < strlen(a); i++) {
        if((a[i]>'9' || a[i]<'0')&&a[i]!='.'){
            ret = 0;
            break;
        }

        if(a[i]=='.' && flag2 == true){
            ret = 0;
            break;
        }

        if(a[i] == '.' && flag2 == false){
            flag2 = true;
            continue;
        }

        if(!flag2){
            ret = ret*10 + a[i] - '0';
        }else {
            ret = ret * 10 + a[i] - '0';
            cnt *= 10;
        }

    }
    if(ret == 0)  printf("0");
    else printf("%lf",ret/cnt*flag);
    return 0;

}
           

4. MT1504 抛銀币

(1)題目描述

小碼哥無聊做抛銀币實驗,輸入U表示正面朝上,D表示反面朝上,S表示立在桌上,輸入由這3個字元組成的字元串表示抛銀币的結果。如果有至少一次銀币立在桌子上就輸出WA,否則就輸出正面朝上的比率。

格式

輸入格式: 輸入字元串(長度小于1000)

.

輸出格式: 輸出實型

樣例1

輸入格式: UUUDDD

.

輸出格式: 0.500000

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char coins[1000];
    int u = 0, d=0,s=0;
    cin.getline(coins,1000);
    int len = strlen(coins);
    for(int i=0;i<len;i++){
        if(coins[i]=='S'){
            s++;
            break;
        }
        else if(coins[i]=='U') u++;
        else if(coins[i]=='D') d++;
    }

    if(s==1) printf("WA");
    else printf("%f", 1.0*u/len);

    return 0;
}
           

5. MT1505 字元串子集

(1)題目描述

輸入字元串s(s中的字元均為大小寫字元,且均不相同),找出字元串的所有可能子串數目。

格式

輸入格式: 輸入字元串s1(長度小于50)

.

輸出格式: 輸出子串總數

樣例1

輸入格式: ABC

.

輸出格式: 6

備注:

如果字元串為ABC,則其是以子串為

A

B

C

AB

BC

ABC

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;
typedef long long ll;
#define MAX_NUM 10005
#define PI 3.1415926

double res;
int ans,n,m,t,k,len,cnt=0,minn=MAX_NUM,maxx=0;
char ch,s1[MAX_NUM] = "",s2[MAX_NUM]="";
bool flag = true;
int a[MAX_NUM] = {0} , b[MAX_NUM] = {0};


int main( )
{
    cin.getline(s1,MAX_NUM);
    len = strlen(s1);
    cout << len*(len+1) / 2 << endl;
    return 0;
}

           

6. MT1506 查找子串

(1)題目描述

輸入1個字元串包含1,2,3三種字元的字元串,每一次你可以将其中一個數字改成任意另一個,求需要多少次才能将整個字元串改成同樣的數字。

格式

輸入格式: 輸入1個字元串,長度為n (n<100000)

.

輸出格式: 最少需要修改的次數

樣例1

輸入格式: 123123123

.

輸出格式: 6

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char s[100000];
    int cnt[3];
    cin.getline(s,100000);

    int len = strlen(s);
    for(int i = 0;i < len; i++)
        cnt[s[i]-'1']++;
    int ans = 0;
    for(int i = 0;i < 3;i++){
        ans = max(cnt[i],ans);
    }

    printf("%d",len-ans);

    return 0;
}

           

7. MT1507 最長的1

(1)題目描述

輸入1個字元串包含1,2,3三種字元的字元串,在其中找到長度最長的,全部是1的子串,輸出其長度。

格式

輸入格式: 輸入1個字元串,長度為n (n<100000)

.

輸出格式: 最長的全部是1的字串的長度

樣例1

輸入格式: 12112311123

.

輸出格式: 3

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char s[100000];
    cin.getline(s,100000);

    int len = strlen(s);
    int ans = 0;
    int cnt = 0;
    for(int i = 0;i < len;i++)
    {
        if(s[i] == '1')
            cnt++;
        else
        {
            ans = max(ans,cnt);
            cnt = 0;
        }
    }

    ans = max(ans,cnt);
    printf("%d",ans);
    return 0;
}

           

8. MT1508 查找子串1

(1)題目描述

輸入1個字元串其中包含一個字元串“zichuan”,輸出它在字元串的起止位置(從0開始)。

如果子串多次出現,查找第一次出現的位置。如果沒有包含該子串,輸出NULL。

格式

輸入格式: 輸入1個字元串,長度為n (n<100000)

.

輸出格式: 字元串的開始和結尾的位置

樣例1

輸入格式: asdzichuanqewr

.

輸出格式: 3 9

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char a[100000];
    char zi[] = "zichuan";
    cin.getline(a, 100000);

    char * begin = strstr(a,zi);

    if(begin == NULL) cout << "NULL";
    else {
        int i = begin - a;
        cout << i << " " <<i + strlen(zi) - 1;
    }
    return 0;
}

           

9. MT1509 查找子串2

(1)題目描述

輸入2個字元串其中,第一個字元串可能包含第二個字元串。輸出其第一次出現位置,若不存在,則輸出-1

格式

輸入格式: 輸入2個字元串,長度均小于100

.

輸出格式: 字元串的開始的位置

樣例1

輸入格式:

asdzichuanqewr

zichuan

.

輸出格式: 3

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char a[100];
    char b[100];
    cin.getline(a, 100);
    cin.getline(b, 100);

    char* begin = strstr(a,b);

    if(begin == NULL)   cout << "-1";
    else    cout << begin-a;

    return 0;
}
           

10. MT1510 子串比較

(1)題目描述

輸入字元串(長度小于80)S1,S2,如果在字元串S1中找到跟S2相同的子串則輸出YES,沒有輸出NO。要求子串長度最少含2個字元。

格式

輸入格式: 分兩行輸入字元串S1,S2

.

輸出格式: 輸出YES或者NO

樣例1

輸入格式:

Wendy hate C++!

C++

.

輸出格式: YES

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char a[80];
    char b[80];
    cin.getline(a,80);
    cin.getline(b,80);

    char*begin = strstr(a,b);
    if(begin == NULL)       cout << "NO";
    else cout<<"YES";
    return 0;
}

           

11. MT1511 字元串組合

(1)題目描述

輸入字元串s1(由三個不同的小寫英語字母組成),把字元串中的元素重新排列組合。

格式

輸入格式: 輸入字元串s1

.

輸出格式: 輸出字典序最小的一種新的組合

樣例1

輸入格式: xyz

.

輸出格式: xzy

備注:

xyz的所有排列按字典序遞增如下:

xyz

xzy

yxz

yzx

zxy

zyx

因為最小的序列xyz與原字元串一樣,不是新的,是以我們輸出xzy

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    string s1,s2;
    cin >> s1;
    s2 = s1;
    sort(s1.begin(),s1.end());
    if(s1==s2)
        cout << s1[0] << s1[2] << s1[1];
    else
        cout << s1;
    return 0;
}
           

12. MT1512 左右排序

(1)題目描述

将一個字元串以中心位置,左半部分按升序排列,右半部分逆序。若字元串長度是奇數,則中間字元不動。

格式

輸入格式: 輸入字元串

.

輸出格式: 輸出字元串

樣例1

輸入格式: fsad123

.

輸出格式: afsd321

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

bool cmp(int a, int b)
{
    return a>b;
}

int main( )
{
    char a[80];
    cin.getline(a,80);
    int len = strlen(a);
    if(len%2==1){
        sort(a,a+len/2);
        sort(a+len/2+1,a+len,cmp);
    }else{
        sort(a,a+len/2);
        sort(a+len/2,a+len,cmp);
    }

    cout << a;
    return 0;
}
           

13. MT1513 編号排序

(1)題目描述

輸入n個字元串,是所有産品的編号,求按照字典序排序的編号。

編号的總長度量不超過1e6。

格式

輸入格式: 一個整數n (n<100000),接下來有n行,包含n個編号

.

輸出格式: 排序後的編号

樣例1

輸入格式:

3

qwerty

asdfgh

qwer

.

輸出格式:

asdfgh

qwer

qwerty

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    string s[100000];
    int n;
    scanf("%d\n",&n);
    for(int i = 0; i < n;i++)
        getline(cin,s[i]);
    sort(s,s+n);
    for(int i = 0; i < n; i++)
        cout << s[i] << endl;
    return 0;
}
           

14. MT1514 最長的編号

(1)題目描述

輸入n個字元串,是所有産品的編号,求按照編号長度排序的編号。長度相同的編号按照字典序排列。

編号的總長度量不超過1e6。

格式

輸入格式: 一個整數n (n<100000),接下來有n行,包含n個編号

.

輸出格式: 排序後的編号

樣例1

輸入格式:

3

qwerty

asdfgh

qwer

.

輸出格式:

qwer

asdfgh

qwerty

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

bool cmp(string a,string b){
    if(a.length()==b.length())
        return a<b;
    else if (a.length()<b.length())
        return true;
    else return false;
}

int main( )
{
    string s[100000];
    int n;
    scanf("%d\n",&n);
    for(int i = 0; i<n ;i++)
        getline(cin,s[i]);
    sort(s,s+n,cmp);
    for(int i=0;i<n;i++)
        cout<<s[i]<<endl;
    return 0;
}

           

15. MT1515 整型變量和它的指針

(1)題目描述

定義一個整型變量和指針,讓指針指向這個變量,通過指針輸出變量的值。

格式

輸入格式: 輸入整型

.

輸出格式: 輸出整型

樣例1

輸入格式: 3

.

輸出格式: 3

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int a,*p;
    scanf("%d",&a);
    p = &a;
    printf("%d",*p);
    return 0;
}

           

16. MT1516 用指針交換兩個數

(1)題目描述

編寫一個程式,輸入2個整數,用指針指向他們,交換他們的值後輸出。

格式

輸入格式: 輸入整型,空格分隔

.

輸出格式: 輸出整型,空格分隔

樣例1

輸入格式: 3 5

.

輸出格式: 5 3

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void swap(int *pa,int *pb){
    int temp;
    temp = *pa;
    *pa = *pb;
    *pb = temp;
}

int main( )
{
    int a, b, temp;
    scanf("%d%d",&a,&b);
    swap(&a,&b);
    printf("%d %d",a,b);
    return 0;
}
           

17. MT1517 順序輸出

(1)題目描述

編寫一個程式,輸入3個整數,用指針方法指向他們,将它們按由小到大的順序輸出。

格式

輸入格式: 輸入整型,空格分隔

.

輸出格式: 輸出整型,空格分隔

樣例1

輸入格式: 3 5 1

.

輸出格式: 1 3 5

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void swap(int *pa,int *pb){
    int temp;
    temp = *pa;
    *pa = *pb;
    *pb = temp;
}

int main( )
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    if(a>b)
        swap(&a,&b);
    if(a>c)
        swap(&a,&c);
    if(b>c)
        swap(&b,&c);
    printf("%d %d %d",a,b,c);
    
    return 0;
}

           

18. MT1518 用指針交換

(1)題目描述

輸入3個整數a,b,c,編寫一個交換函數,用指針做參數,将它們按由小到大的順序放到a,b,c中再輸出。

格式

輸入格式: 輸入整型,空格分隔

.

輸出格式: 輸出整型,空格分隔

樣例1

輸入格式: 7 3 2

.

輸出格式: 2 3 7

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void swap(int *pa,int *pb){
    int temp;
    temp = *pa;
    *pa = *pb;
    *pb = temp;
}

int main( )
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    if(a>b)
        swap(&a,&b);
    if(a>c)
        swap(&a,&c);
    if(b>c)
        swap(&b,&c);
    printf("%d %d %d",a,b,c);
    
    return 0;
}
           

19. MT1519 實型指針

(1)題目描述

定義一個實型變量和指針,讓指針指向這個變量,通過指針輸出變量的值。

格式

輸入格式: 輸入實型

.

輸出格式: 輸出實型

樣例1

輸入格式: 3.4

.

輸出格式: 3.400000

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    double a,*p;
    scanf("%lf",&a);
    p = &a;
    printf("%lf",*p);
    return 0;
}
           

20. MT1520 字元型指針

(1)題目描述

定義一個字元型變量和指針,讓指針指向這個變量,通過指針輸出變量的值。

格式

輸入格式: 輸入字元型

.

輸出格式: 輸出字元型

樣例1

輸入格式: M

.

輸出格式: M

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char a, *p;
    scanf("%c",&a);
    p = &a;
    printf("%c",*p);
    return 0;
}
           

21. MT1521 指向字元串的指針

(1)題目描述

定義一個指向一個字元串的指針,輸入字元串,然後輸出第n個元素。

格式

輸入格式: 第1行輸入字元串,第2行輸入整數n

.

輸出格式: 輸出字元型

樣例1

輸入格式:

I love coding

3

.

輸出格式: 1

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char a[80],*p;
    int n;
    p = a;
    cin.getline(a, 80);
    scanf("%d",&n);
    printf("%c",p[n-1]);
    return 0;
}
           

22. MT1522 指向字元串的指針

(1)題目描述

編寫函數f(char *s),将字元串s中的大寫字母加3,小寫字母減3。

格式

輸入格式: 輸入字元串

.

輸出格式: 輸出字元串

樣例1

輸入格式: DEFdef

.

輸出格式: GHIabc

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void f(char * s){
    while(*s){
        if(*s >= 'a' && *s <= 'z') *s -=3;
        if(*s >= 'A' && *s <= 'Z') *s +=3;
        s++;
    }
}

int main( )
{
    char s[100];
    cin.getline(s,100);
    f(s);
    printf("%s",s);
    return 0;
}
           

23. MT1523 字元串複制

(1)題目描述

利用指針完成字元串複制函數char *mycpy(char *s1,char *s2),把S2的内容複制到s1串中,再輸出s1。

格式

輸入格式: 輸入兩行,分别為字元串S1和S2

.

輸出格式: 輸出字元串S1

樣例1

輸入格式:

C++

C is great!

.

輸出格式: C is great!

備注:

字元串長度小于10000

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

char *mycpy(char *s1,char *s2){
    char *p = s1;
    while(*s2 != '\0')
        *(p++) = *(s2++);
    *p = '\0';
    return s1;
}

int main( )
{
    char str1[10000],str2[10000];
    cin.getline(str1, 10000);
    cin.getline(str2, 10000);   
    mycpy(str1,str2);
    cout<<str1;
    return 0;
}
           

24. MT1524 字元串連接配接

(1)題目描述

利用指針完成字元串連接配接函數char *mystrcat(char *s1,char *s2),把s2的内容連接配接到s1串後面,再輸出s1。兩個字元串長度均小于5000。

格式

輸入格式: 分兩行輸入字元串s1,s2

.

輸出格式: 輸出字元串s1

樣例1

輸入格式:

I love C!

C is great!

.

輸出格式: I love C!C is great!

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

char *mystrcat(char *s1,char *s2) {
    char *p = s1;
    while(*p !='\0')
        p++;
    while(*s2!='\0')
        *(p++) = *(s2++);
    *p = '\0';
    return s1;
}

int main( )
{
    char s1[10000] ="",s2[5000] = "";
    cin.getline(s1, 10000);
    cin.getline(s2, 5000);
    mystrcat(s1, s2);
    cout<<s1;

    return 0;
}
           

25. MT1525 計數

(1)題目描述

輸入字元串s1,利用指針周遊并統計字元串中字母的個數并輸出。

格式

輸入格式: 輸入字元串S1

輸出格式: 輸出整型

樣例1

輸入格式: I love C!C is great!

.

輸出格式: 14

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char str[10000] = "";
    char *p = str;
    int cnt=0;

    cin.getline(str,10000);
    while(*p != '\0')
        if(isalpha(*(p++)))
            cnt++;
    cout << cnt;
    return 0;
}
           

26. MT1526 字元頻次

(1)題目描述

輸入字元串s1,用指針指向這個字元串,查找字元串中重複字元(區分大小寫),依次輸出他們和他們出現的頻次。

格式

輸入格式: 輸入字元串

.

輸出格式: 輸出字元和頻次,空格分隔。

樣例1

輸入格式: Wendy is a great girl

.

輸出格式:

e 2

i 2

a 2

g 2

r 2

備注:

輸出字元,按照原字元串中的排序順序

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char s1[10000];
    cin.getline(s1,10000);
    char *p =s1;

    for(int i=0;i<strlen(s1);i++){
        if(*(p+i)==' ')
            continue;
        int num = 1;
        for(int j= i+1; j < strlen(s1);j++){
            if(*(p+i) == *(p+j)){
                num++;
                *(p+j) = ' ';
            }
        }

        if(num >= 2){
            printf("%c %d\n",*(p+i),num);
        }
    }
    return 0;
}
           

27. MT1527 回文串

(1)題目描述

利用指針判斷字元串是否為回文。(正讀和反讀都─樣的字元串)

格式

輸入格式: 輸入字元串

.

輸出格式: 輸出YES或者NO

樣例1

輸入格式: wenew

.

輸出格式: YES

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    char str[10000] = "";
    char *p1,*p2;
    bool state = true;

    cin.getline(str, 10000);

    p1=str;
    p2=str+strlen(str)-1;

    while (p1 < p2){
        if(*p1 != *p2){
            state = false;
            break;
        }
        p1++;
        p2--;
    }

    if(state)
        cout << "YES";
    else
        cout << "NO";

    return 0;
}
           

28. MT1528 插入字元

(1)題目描述

輸入字元串s1,整數N和字元c,用指針指向這個字元串,在位置N插入字元c,輸出新串。

格式

輸入格式: 第一行輸入字元串,第二行輸入整數N和字元c,空格分隔。

.

輸出格式: 輸出字元串

樣例1

輸入格式:

Wendy i great

7 s

.

輸出格式: Wendy is great

備注:

位置N插入字元c,指的是下标N處放字元c,其他字元相應後移一位。字元串長度小于700。

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void insert(char *s,char ch,int pos){
    char *p = s+strlen(s);
    *(p+1) = '\0';
    while(p > s+pos){
        p--;
        *(p+1) = *p;
    }

    *p = ch;
    return;
}


int main( )
{
    char str[700] = "";
    char ch;
    int pos;

    cin.getline(str,700);
    cin>>pos>>ch;
    insert(str,ch,pos);
    cout << str;
    return 0;
}
           

29. MT1529 統計子串

(1)題目描述

統計一個字元串出現某子串的次數。

格式

輸入格式: 第一行輸入字元串,第二行輸入子串。

.

輸出格式: 輸出字元串

樣例1

輸入格式:

aabsdfjslaadkk

aa

.

輸出格式: 2

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int match(char *s1,char *s2){
    int ans = 0;
    int len = strlen(s2);
    while(*s1){
        for(int i = 0;i<len;i++){
            if(*(s1+i) != *(s2+i))
                break;
            if(i==len-1)
                ans++;
        }
        s1++;
    }
    return ans;
}

int main( )
{
    char s1[100],s2[100];
    cin.getline(s1,100);
    cin.getline(s2,100);
    printf("%d\n",match(s1,s2));
    return 0;
}
           

30. MT1530 最大等值子串

(1)題目描述

若一個字元串的一個子串的每個字元均相同,則稱為等值子串。求一個字元串的最大等值子串的長度。

格式

輸入格式: 一行:一串字元串。

.

輸出格式: 一行:最長等值字串的個數。

樣例1

輸入格式: 1223334444

.

輸出格式: 4

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int maxLen(char *s){
    int len = strlen(s);
    int ret = 1;
    int cnt = 1;
    for (int i = 1; i < len; i++){
        if(*(s+i) == *(s+i-1))      cnt++;
        else cnt = 1;
        ret = max(ret,cnt);
    }
    return ret;
}

int main( )
{
    char s[100];
    cin.getline(s,100);
    printf("%d",maxLen(s));
    return 0;
}

           

31. MT1531 指針做函數參數

(1)題目描述

編寫函數将參數s所指字元串中除了下标為奇數,同時ASCII值也為奇數的字元之外,其餘所有字元都删除。

格式

輸入格式: 輸入字元串

.

輸出格式: 輸出字元串

樣例1

輸入格式: 0123456789

.

輸出格式: 13579

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void fun(char *str) {
    char s[100];
    int j = 0, n = strlen(str);
    for(int i = 1;i < n; i =i+2)
        if(str[i] % 2 != 0)
            s[j++] = str[i];
    s[j] = '\0';
    int len = strlen(s);
    for(int i = 0; i < len; i++)
        str[i] = s[i];
    str[len] = '\0';
}

int main( )
{
    char str[100];
    cin.getline(str, 100);
    fun(str);
    printf("%s",str);
    return 0;
}
           

32. MT1532 通配符

(1)題目描述

編寫函數實作通配符的比對,其中通配符為“?”,表示比對任意一個字元,若比對成功傳回字元串的比對位置(起始為0)。如“there”和“?re”是比對的,傳回2。若比對失敗,傳回-1。

格式

輸入格式: 第一行為原字元串,第二行為目标字元串

.

輸出格式: 傳回比對的下标位置,失敗傳回-1。

樣例1

輸入格式:

there

?re

.

輸出格式: 2

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int match(char *s1, char *s2){
    int len1=strlen(s1), len2=strlen(s2);
    for(int i = 0;i < len1; i++){
        for(int j = 0;j < len2;j++){
            if(s2[j]!='?' && s1[i+j] != s2[j])  break;
            if(j==len2-1) return i;
        }
    }
    return -1;
}

int main( )
{
    char s1[100],s2[100];
    cin.getline(s1, 100);
    cin.getline(s2,100);
    printf("%d",match(s1,s2));
    return 0;
}
           

33. MT1533 指向一維數組的指針

(1)題目描述

定義一個指向一維數組的指針,用指針周遊數組進行輸入輸出。數組含10個數組元素。

格式

輸入格式: 輸入整型

.

輸出格式: 輸出整型

樣例1

輸入格式: 1 2 3 4 5 6 7 8 9 10

.

輸出格式: 1 2 3 4 5 6 7 8 9 10

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int a[10],*p;
    p=a;
    for(int i = 0;i <10; i++)
        scanf("%d",&p[i]);
    for(int i = 0;i < 10; i++)
        printf("%d ",p[i]);
    return 0;
}
           

34. MT1534 指針遞增

(1)題目描述

編寫一個使用指針遞增方式通路數組a的元素的程式。數組長度為3,數組元素為1,2,3。

格式

輸入格式: 無

.

輸出格式: 分行輸出數組a的元素,數組為整型

樣例1

輸入格式: 無

.

輸出格式:

a[0]=1

a[1]=2

a[2]=3

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int a[3] = {1,2,3};
    int *ptr;
    ptr = a;
    for(int i = 0;i < 3; i ++){
        printf("a[%d]=%d\n",i,*ptr);
        ptr++;
    }
    return 0;
}
           

35. MT1535 間隔插入

(1)題目描述

定義一個指向一維數組的指針,從鍵盤上輸入10個數組元素,按後原來數組中,每兩個元素之間插入一個1,輸出新數組。

格式

輸入格式: 輸入數組元素,整型,空格分隔。

.

輸出格式: 輸出整型,空格分隔。

樣例1

輸入格式: 1 2 3 4 5 6 7 8 9 10

.

輸出格式: 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int a[10];
    for(int i = 0;i<10;i++){
        cin >> a[i];
    }
    for(int *i = a;i != &a[10];i++){
        cout << *i << " ";
        if(i!=&a[9])  cout << 1 <<" ";
    }
    return 0;
}
           

36. MT1536 指針逆序

(1)題目描述

指針法将數組A中n個整數按相反順序存放。不考慮非法輸入。

格式

輸入格式: 輸入為整型,第一行輸入正整數n,第二行輸入數組元素,空格分隔。

.

輸出格式: 輸出為整型,空格分隔。

樣例1

輸入格式:

5

1 4 5 2 0

.

輸出格式: 0 2 5 4 1

備注:

n不大于10

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void reverse(int *a,int n){
    for(int i=0;i<n/2;i++){
        int temp = a[i];
        a[i] = a[n-i-1];
        a[n-i-1] = temp;
    }
}

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

37. MT1537 用指針逆序輸出數組

(1)題目描述

定義一個指向一維數組的指針,用指針周遊數組并逆序輸出。數組含10個數組元素。

格式

輸入格式: 輸入數組元素,整型,空格分隔。

.

輸出格式: 輸出整型,空格分隔。

樣例1

輸入格式: 1 2 3 4 5 6 7 8 9 10

.

輸出格式: 10 9 8 7 6 5 4 3 2 1

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int a[10];
    for(int i = 0; i < 10; i++)
        cin >> a[i];
    for(int *i = &a[9]; i != a-1;i--)
        cout << *i << ' ';
    return 0;
}
           

38. MT1538 用指針間隔輸出數組

(1)題目描述

定義一個指向一維數組的指針,用指針周遊數組并從第一個元素開始間隔地輸出。數組含10個數組元素。

格式

輸入格式: 輸入數組元素,整型,空格分隔。

.

輸出格式: 輸出整型,空格分隔。

樣例1

輸入格式: 1 2 3 4 5 6 7 8 9 10

.

輸出格式: 1 3 5 7 9

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int a[10];
    for(int i = 0; i < 10; i++)
        cin >> a[i];
    for(int *i = a; i != &a[10]; i = i+2)
        cout << *i << " ";
    return 0;
}
           

39. MT1539 中心對稱

(1)題目描述

用指針法判斷數組是否中心對稱,如 (1,2,3,5,3,2,1)。

格式

輸入格式:第一行輸入數組長度N(>=1),第二行輸入數組元素,整型,空格分隔。

.

輸出格式: 輸出YES或者NO

樣例1

輸入格式:

7

1 2 3 5 3 2 1

.

輸出格式: YES

備注:

數組不超過10個元素

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int a[10];
    int n;
    scanf("%d",&n);
    for(int i = 0;i < n;i++)  scanf("%d", a+i);
    for(int i = 0; i < n/2; i++){
        if(*(a+i) != *(a+n-i-1)){
            printf("NO");
            return 0;
        }
    }
    printf("YES");
    return 0;
}

           

40. MT1540 奇偶和

(1)題目描述

數組中有N個數字(數字之和不超過int範圍),用指針指向這個數組,檢查下标為奇數的數字之和是否等于下标為偶數的數字之和,輸出YES或者NO。

格式

輸入格式: 第一行輸入數組長度N,第二行輸入數組元素(下标從0開始),整型,空格分隔。

.

輸出格式: 輸出為YES或者NO

樣例1

輸入格式:

3

1 2 3

.

輸出格式: NO

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int a[100], n;
    int *p = a;
    cin >> n;
    int even = 0, odd = 0;
    for(int i = 0; i< n;i++) cin>>*(p+i);
    for(int i = 0; i< n; i++){
        if(i%2 == 1)
            odd += *(p+i);
        else
            even += *(p+i);
    }

    if(odd ==even)
        cout << "YES";
    else
        cout << "NO";
    return 0;
}
           

41. MT1541 指針求和

(1)題目描述

指針法将數組中奇偶下标的元素分别求和并輸出他們的內插補點(奇下标的和-偶下标的和)。

格式

輸入格式: 輸入為整型,第一行輸入n,第二行輸入數組元素,空格分隔。

.

輸出格式: 輸出為整型,空格分隔。

樣例1

輸入格式:

5

1 4 5 2 0

.

輸出格式: 0

備注:

數組不超過10個元素

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int n,dp[1005];
    cin >>n;
    for(int i=0;i<n;i++){
        cin>>dp[i];
    }
    int *p,ji=0,ou=0;
    p=dp;
    for(int i=0;i<n;i++){
        if(i%2==0) ou+=*(p+i);
        else ji+=*(p+i);
    }
    cout<<ji-ou<<endl;
    return 0;
}
           

42. MT1542 違章

(1)題目描述

小碼哥在整理交通違章的資訊,他用一個數組記錄違章的車牌号,一個數組記錄對應的罰金。

請輸入日期。如果日期是偶數,罰款從奇數牌号的車輛收取;如果日期是奇數,罰款從偶數牌号的車輛收取。

輸出給定日期的罰款總額,用指針實作。不考慮不合理的輸入等特殊情況。

格式

輸入格式: 第一行輸入數組長度N和日期M,第二行輸入車牌數組的元素,第三行輸入罰金的數組的元素。空格分隔,整型。

.

輸出格式: 輸出整型

樣例1

輸入格式:

4 6

1115 1112 2135 1334

450 500 150 200

.

輸出格式: 600

備注:

N不大于10

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    int n,m,dp[1005]={0},bucket[1005]={0},ans=0;
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>dp[i];
    for(int i=0;i<n;i++) cin>>bucket[i];
    int *p1 = dp,*p2=bucket;
    if(m%2==0){
        for(int i=0;i<n;i++){
            if(*p1%2==1) ans+=*p2;
            p1++,p2++;
        }
    }else{
        for(int i=0;i<n;i++){
            if(*p1%2==0) ans+=*p2;
            p1++,p2++;
        }
    }
    cout<<ans<<endl;
    return 0;
}
           

43. MT1543 二維數組行指針

(1)題目描述

定義一個指向3X3二維數組的行指針,用行指針周遊數組進行輸入輸出。

格式

輸入格式: 輸入整型

.

輸出格式: 輸出整型

樣例1

輸入格式: 1 2 3 4 5 6 7 8 9

.

輸出格式:

1 2 3

4 5 6

7 8 9

(2)參考代碼

/**
使用1級指針通路二維數組
因為數組本身在位址空間中就是連續排列的,根據行數和列數,
    計算出通路單元的 位址偏移量 就可以用一級指針周遊二維數組中的所有資料。
*/
#include<stdio.h>
void printMatirx(int *pArray,int rows,int cols);
int main()
{
    int array[3][3],i,j;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            scanf("%d",&array[i][j]);
        }
    }
    int *pArray = NULL;
    pArray = array;
    printMatirx(array,3,3);//列印2行3列的數組
    return 0;
}
void printMatirx(int *pArray,int rows,int cols)
{
    int i;
    int j;
    for(i=0;i<rows;i++)
    {
        for(j=0;j< cols;j++) {
            printf("%d ", *(pArray + i * cols + j));//通路i行j列的二維數組元素
        }
        printf("\n");
    }
}
           

44. MT1544 二維數組列指針

(1)題目描述

定義一個指向4X4二維數組的列指針,用列指針周遊數組進行輸入輸出。

格式

輸入格式: 輸入整型

.

輸出格式: 輸出整型,格式%2d

樣例1

輸入格式: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

.

輸出格式:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int dp[1005][1005]={0};
int main( )
{
    int *p;
    for(p=dp[0];p<dp[0]+16;p++) cin>>*p;
    for(p=dp[0];p<dp[0]+16;p++){
        printf("%2d ",*p);
        if((p-dp[0])%4==3){
            cout<<endl;
        }
    }
    return 0;
}
           

45. MT1545 行元素乘積

(1)題目描述

用一維數組指針通路二維數組(MXN),計算二維數組每一行元素乘積之和。

格式

輸入格式: 第一行輸入正整數M和N,第二行輸入數組元素,整型,空格分隔。

.

輸出格式: 輸出整型

樣例1

輸入格式:

3 2

1 2

3 5

3 2

.

輸出格式: 23

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

int ans=0,m,n,dp[1005][1005];

int main( )
{
    cin>>m>>n;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++) cin>>dp[i][j];
    }
    for(int i=0;i<m;i++){
        int now=1;
        for(int *p = &dp[i][0]; p!=&dp[i][n];p++){
            now = now*(*p);
        }
        ans += now;
    }
    cout<<ans<<endl;
    return 0;
}
           

46. MT1546 矩陣轉置

(1)題目描述

寫一個函數,用指針做參數,将3×3矩陣轉置。

格式

輸入格式: 按矩陣形式輸入數組元素,整型,空格分隔。

.

輸出格式: 輸出整型,按矩陣形式輸出。

樣例1

輸入格式:

1 2 3

4 5 6

7 8 9

.

輸出格式:

1 4 7

2 5 8

3 6 9

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void change(int *a,int *b){
    int f;
    f = *a;
    *a = *b;
    *b = f;
}

void zhuanzhi(int (*a)[5]){
    change(*(a+1)+2,*(a+2)+1);
    change(*(a+1)+3,*(a+3)+1);
    change(*(a+2)+3,*(a+3)+2);
}

int main( )
{
    int a[5][5];
    for(int i=1;i<=3;i++){
        for(int j=1;j<=3;j++) cin>>a[i][j];
    }
    zhuanzhi(a);
    for(int i=1;i<=3;i++){
        for(int j=1;j<=3;j++) cout<<a[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}
           

47. MT1547 矩陣相加

(1)題目描述

輸入2個3X3的整型矩陣A和B,編寫函數計算A+B,放到矩陣C裡面,用指針做參數,輸出矩陣C。

格式

輸入格式: 分兩行輸入兩個矩陣,空格分隔。

.

輸出格式: 按矩陣形式輸出,整型,每個數字占3列,空格分隔。

樣例1

輸入格式:

1 2 3 8 7 6 6 4 3

0 7 5 4 3 5 2 1 4

.

輸出格式:

1 9 8

12 10 11

8 5 7

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void matrixPlus(int (*a)[3],int (*b)[3]){
    int c[3][3];
    for(int i=0;i<3;i++){
       for(int j=0;j<3;j++){
            *(*(c+i)+j) = *(*(a+i)+j) + *(*(b+i)+j);
            if(j==0) printf("%3d",*(*(c+i)+j));
            else printf(" %3d",*(*(c+i)+j));
       }
    printf("\n");
    }
}
int main( )
{
    int a[3][3],b[3][3];
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            scanf("%d",&b[i][j]);
        }
    }
    matrixPlus(a, b);
    return 0;
}
           

48. MT1548 矩陣相減

(1)題目描述

輸入2個3X3的整型矩陣A和B,編寫函數計算A-B,放到矩陣C裡面,用指針做參數,輸出矩陣C。

格式

輸入格式: 分兩行輸入兩個矩陣,空格分隔。

.

輸出格式: 按矩陣形式輸出,整型,每個數字占3列,空格分隔。

樣例1

輸入格式:

1 2 3 8 7 6 6 4 3

0 7 5 4 3 5 2 1 4

.

輸出格式:

1 -5 -2

4 4 1

4 3 -1

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void matrixPlus(int (*a)[3],int (*b)[3]){
    int c[3][3];
    for(int i=0;i<3;i++){
       for(int j=0;j<3;j++){
            *(*(c+i)+j) = *(*(a+i)+j) - *(*(b+i)+j);
            if(j==0) printf("%3d",*(*(c+i)+j));
            else printf(" %3d",*(*(c+i)+j));
       }
    printf("\n");
    }
}
int main( )
{
    int a[3][3],b[3][3];
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            scanf("%d",&b[i][j]);
        }
    }
    matrixPlus(a, b);
    return 0;
}
           

49. MT1549 左下角

(1)題目描述

輸入3X3的整型數組,編寫函數,用指針做參數,輸出數組左下角。

格式

輸入格式: 輸入數組,元素在0到9之間,空格分隔。

.

輸出格式: 輸出數組,空格分隔。

樣例1

輸入格式: 1 2 3 4 5 6 7 8 9

.

輸出格式:

1

4 5

7 8 9

(2)參考代碼

#include<bits/stdc++.h> 

using namespace std;

void lower_left(int(*a)[1005]){
    for(int i=0;i<3;i++){
       for(int j=0;j<3;j++){
           if(j<=i){
               if(j==0) cout<<*(*(a+i)+j);
                else cout<<" "<<*(*(a+i)+j);
           } 
          
       }
       cout<<endl;
    }
}

int main( )
{
    int a[1005][1005];
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++) cin>>a[i][j];
    }
    lower_left(a);
    return 0;
}
           

50. MT1550 十二月

(1)題目描述

編一個程式,輸入月份數,輸出該月的名稱,小寫,要求用指針處理。不考慮非法輸入等特殊情況。

格式

輸入格式:輸入整型

.

輸出格式: 輸出字元串

樣例1

輸入格式: 4

.

輸出格式: april

#include <bits/stdc++.h>
using namespace std;
int main() {
    string month[12] = {"january","february","march","april","may","june","july","august","september","october","november","december"};
    string *ptr = month;
    int n;
    cin >> n;
    cout << *(ptr+n-1);
    return 0;
}
           

結語