日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第七周:字符串 + 数组 + 指针

發(fā)布時間:2024/3/12 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第七周:字符串 + 数组 + 指针 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.字符串中的字符刪除

題目:

分別用字符數(shù)組或字符指針做函數(shù)參數(shù),在字符串中刪除與某字符相同的字符。

void Delete1(char s[ ], char ch);

void Delete2(char *p, char ch);
【樣例輸入】

abcdabcd

c
【樣例輸出】

abdabd

代碼:

#include <iostream> #include <cstring> using namespace std;void Delete(char s[ ], char ch) {int num = strlen(s);for(int i = 0; i < num; i ++){if(s[i] == ch)s[i] = ' '; //ch已經(jīng)是一個字符,不用加單引號;} }void Delete2(char *p, char ch) {for(; *p != '\0'; p ++) //注意\0 \0 \0 \0;可以p[i]表示數(shù)組;可以寫成for(int i = 0;p[i]!='\0';i++) if(p[i] == ch) p[i] = ' ';{if(*p == ch) *p = ' ';} }int main() {char s[100];char c;gets(s);cin >> c;Delete2(s, c);int num = strlen(s);for(int i = 0; i < num; i ++){if(s[i] == ' ') continue;else cout << s[i];}return 0; }

2.密碼

題目:

假設(shè)一個比較安全的密碼至少應(yīng)該滿足下面兩個條件:

(1)密碼長度大于等于8,且不要超過16。

(2)密碼中的字符應(yīng)該來自下面“字符類別”中四組中的至少三組。

這四個字符類別分別為:

1.大寫字母:A,B,C…Z;

2.小寫字母:a,b,c…z;

3.數(shù)字:0,1,2…9;

4.特殊符號:~,!,@,#,$,%,^;

給你一個密碼,你的任****務(wù)就是判斷它是不是一個安全的密碼。

請設(shè)計函數(shù)bool Password(char *str)實現(xiàn)之。

【輸入形式】測試實例包含一個密碼(長度最大為50),密碼僅包括上面的四類字符。

【輸出形式】對于測試實例,判斷這個密碼是不是一個安全的密碼,是的話輸出YES,否則輸出NO。

【樣例輸入】a1b2c3d4

【樣例輸出】NO

【測試數(shù)據(jù)】a1b2c3d4 Aa! AAAAZZab001~~~# ~~9Bccc^ 000000000000000000000abA %""""""a12

代碼:

#include <iostream> #include <cstring> #define N 50using namespace std;int flag =0;bool Password(char *str) {int len = strlen(str); //直接strlen;不用取內(nèi)容;bool flag1 = true, flag2 = true, flag3 = true, flag4 = true; //要注意排除出現(xiàn)過的類型;if(len < 8 || len > 16) return false;for(int i = 0; i < len; i ++){if(str[i] >= 'A' && str[i] <= 'Z'){if(flag1){flag += 1;flag1 = false;}}if(str[i] >= 'a' && str[i] <= 'z'){if(flag2){flag += 1;flag2 = false;}}if(str[i] >= '0' && str[i] <= '9'){if(flag3){flag += 1;flag3 = false;}}switch(str[i]){case '~':case '!':case '#':case '$':case '%':if(flag4) //都是一種情況;{flag += 1;flag4 = false;}}}if(flag >= 3)return true;else return false; }int main() {char s[N];gets(s);if(Password(s)) cout << "YES";else cout << "NO"; }

3.合并字符串

題目:

編寫一個函數(shù)char * str_bin(char* str1, char* str2), str1、str2是兩個有序字符串(其中字符按ASCII碼從小到大排序),將str2合并到字符串str1中,要求合并后的字符串仍是有序的,允許字符重復(fù)。在main函數(shù)中測試該函數(shù):從鍵盤輸入兩個有序字符串,然后調(diào)用該函數(shù),最后輸出合并后的結(jié)果。str_bin函數(shù)的返回值就是該結(jié)果串的起始地址。
【輸入形式】分行從鍵盤輸入兩個有序字符串(不超過100個字符)
【輸出形式】輸出合并后的有序字符串
【輸入樣例】
aceg
bdfh
【輸出樣例】
abcdefgh
【樣例說明】
輸入兩個有序字符串a(chǎn)ceg和bdfh,輸出合并后的有序字符串a(chǎn)bcdefgh

代碼:

#include <iostream> #define N 101using namespace std;int len1 =0;char * str_bin(char* str1, char* str2){for(int i = 0; str1[i] != '\0'; i ++){len1 ++;}for(int i = len1, j = 0; str2[j] != '\0'; i ++, j ++){str1[i] = str2[j];len1 ++;}str1[len1] = '\0';for(int i = 0; str1[i] != '\0'; i ++){for(int j = i + 1; str1[j] != '\0'; j ++){if(str1[i] > str1[j]){char temp;temp = str1[i];str1[i] = str1[j];str1[j] = temp;}}}return str1; } int main() {char str1[N];char str2[N];gets(str1);gets(str2);puts(str_bin(str1,str2)); return 0; }

4.選擇排序

題目:

定義函數(shù)void Sort(int a[],int n),用選擇法對數(shù)組a中的元素升序排列。自定義main函數(shù),并在其中調(diào)用Sort函數(shù)。

【輸入形式】

首先打印提示"Input n:";然后直接在冒號后面輸入正整數(shù)n,表示數(shù)據(jù)的個數(shù);回車;

打印提示"Input array of n integers:";其中n應(yīng)該用如上輸入的具體的數(shù)值代替;然后直接在冒號后面連續(xù)輸入n個整數(shù)數(shù)值,每個數(shù)值之間用空格隔開;回車;

【輸出形式】

打印"After sorted the array is:";然后直接在冒號后面輸出經(jīng)升序排序后的數(shù)組序列,每個數(shù)值之間用空格隔開,第一個數(shù)值前面無空格,最后一個數(shù)值后面無空格;換行;

【運行時的輸入輸出樣例】(下劃線部分表示輸入)

Input n:6

Input array of 6 integers:1 5 -9 2 4 -6

After sorted the array is:-9 -6 1 2 4 5

代碼:

#include <iostream> #define N 100 using namespace std;void Sort(int a[], int n) {int i, j, k, temp;for(i = 0; i < n; i ++){k = i;for( j = i + 1; j < n; j ++){if(a[j] < a[k]){k = j;}}if(k != i){temp = a[i];a[i] = a[k];a[k] = temp;}} }int main() {int n = 0;int s[N];int i = 0;cout << "Input n:";cin >> n;cout << "Input array of n integers:";while(1){cin >> s[i];i ++;if(i == n)break;}Sort(s, n);cout << "After sorted the array is";for(int i = 0; i < n; i ++){cout << s[i] << " ";}return 0; }

5.滿足條件的整數(shù)

題目:

假設(shè)a、b、c均為整數(shù),且滿足a,b,c 大于1,并且小于等于100,找出所有符合條件:“a的平方 + b的平方 = c的平方”的整數(shù)組。

【要求】

1、編寫函數(shù)void Fun(int a, int b)尋找符合條件的三個數(shù),其中ab分別表示數(shù)據(jù)起止范圍1~100。

2、編寫函數(shù)void Print(int a, int b ,int c)按照規(guī)定的輸出格式打印等式,由fun函數(shù)調(diào)用print函數(shù)執(zhí)行輸出功能。

3、主函數(shù)不允許出現(xiàn)循環(huán)語句,所有功能均在子函數(shù)中實現(xiàn)。

【輸入形式】無輸入
【輸出形式】

按a從小到大的順序輸出所有滿足條件的整數(shù)組(若a相同,則按b從小到大的順序輸出),每行一組,每一組數(shù)據(jù)的輸出樣式為:

3*3 + 4*4 = 5*5

注意:

1、3*3 + 4*4 = 5*5 和 4*4 + 3*3 = 5*5雖然是同一組數(shù)據(jù),后者不需要輸出;

2、加號和等號左右各有一個空格

3、9*9 + 12*12 = 15*15 (在前)

9*9 + 40*40 = 41*41 (在后)

【輸出】

3*3 + 4*4 = 5*5

5*5 + 12*12 = 13*13

6*6 + 8*8 = 10*10

7*7 + 24*24 = 25*25

8*8 + 15*15 = 17*17

9*9 + 12*12 = 15*15

9*9 + 40*40 = 41*41

10*10 + 24*24 = 26*26

11*11 + 60*60 = 61*61

12*12 + 16*16 = 20*20

12*12 + 35*35 = 37*37

13*13 + 84*84 = 85*85

14*14 + 48*48 = 50*50

15*15 + 20*20 = 25*25

15*15 + 36*36 = 39*39

16*16 + 30*30 = 34*34

16*16 + 63*63 = 65*65

18*18 + 24*24 = 30*30

18*18 + 80*80 = 82*82

20*20 + 21*21 = 29*29

20*20 + 48*48 = 52*52

21*21 + 28*28 = 35*35

21*21 + 72*72 = 75*75

24*24 + 32*32 = 40*40

24*24 + 45*45 = 51*51

24*24 + 70*70 = 74*74

25*25 + 60*60 = 65*65

27*27 + 36*36 = 45*45

28*28 + 45*45 = 53*53

28*28 + 96*96 = 100*100

30*30 + 40*40 = 50*50

30*30 + 72*72 = 78*78

32*32 + 60*60 = 68*68

33*33 + 44*44 = 55*55

33*33 + 56*56 = 65*65

35*35 + 84*84 = 91*91

36*36 + 48*48 = 60*60

36*36 + 77*77 = 85*85

39*39 + 52*52 = 65*65

39*39 + 80*80 = 89*89

40*40 + 42*42 = 58*58

40*40 + 75*75 = 85*85

42*42 + 56*56 = 70*70

45*45 + 60*60 = 75*75

48*48 + 55*55 = 73*73

48*48 + 64*64 = 80*80

51*51 + 68*68 = 85*85

54*54 + 72*72 = 90*90

57*57 + 76*76 = 95*95

60*60 + 63*63 = 87*87

60*60 + 80*80 = 100*100

65*65 + 72*72 = 97*97

【提示】主函數(shù)就僅僅一條調(diào)用Fun函數(shù)的語句;Fun函數(shù)中設(shè)計三重循環(huán),判斷a*a+b*b==c*c時,且a<b時,調(diào)用Print函數(shù)執(zhí)行輸出,a<b可以在for里判斷,也可以在輸出前判斷。

代碼:

#include <iostream> #include <cmath> using namespace std;void Print(int a, int b ,int c) {if(a < b)cout << a << '*' << a << " + " << b << '*' << b << " = " << c << '*' << c << endl; //單引號表示一個字符,輸出符號加空格用雙引; }void Fun(int a, int b) {for(int i = 1; i <= 100; i ++){for(int j = 1; j <= 100; j ++){int m = sqrt(i * i + j * j);if(m * m == i * i + j * j && m <= 100) Print(i, j, m);}} }int main() {int a, b;Fun(a, b);return 0; }

6.查找元素

題目:

在數(shù)組中查找指定元素。輸入一個正整數(shù)n(1<n<=10),然后輸入n個整數(shù)存入數(shù)組a中,再輸入一個整數(shù)x,在數(shù)組a中查找x,如果找到則輸出相應(yīng)的最小下標(biāo),否則輸出"Not found"。要求定義并調(diào)用函數(shù)int search(int list[], int n, int x),它的功能是在數(shù)組list中查找元素x,若找到則返回相應(yīng)的最小下標(biāo),否則返回-1。

【輸入形式】

首先打印提示"Input n:";然后直接在冒號后面輸入正整數(shù)n,表示數(shù)據(jù)的個數(shù);回車;

打印提示"Input n integers:";其中n應(yīng)該用如上輸入的具體的數(shù)值代替;然后直接在冒號后面連續(xù)輸入n個整數(shù)數(shù)值,每個數(shù)值之間用空格隔開;回車;

打印提示"Input x:";然后直接在冒號后面輸入數(shù)值x,代表查找的數(shù)值;回車;

【輸出形式】

有兩種情況:

如果指定的x數(shù)值在數(shù)組中查到,則輸出"index = ";被找到的數(shù)值在數(shù)組中的下標(biāo)值;回車;

如果指定的x數(shù)值在數(shù)組中沒有查到,則輸出"Not found";回車;

【運行時的輸入輸出樣例1】(下劃線部分表示輸入)

Input n:3

Input 3 integers:1 2 -6

Input x:2

index = 1

【運行時的輸入輸出樣例2】(下劃線部分表示輸入)

Input n:5

Input 5 integers:1 2 2 5 4

Input x:0

Not found

代碼:

#include <iostream> #define N 10 using namespace std;int flag = 0;int search(int list[], int n, int x) {for(int i = 0; i < n; i ++){if(list[i] == x){flag = 1;return i;}}if(flag == 0)return 0; }int main() {int n;int s[N];int x;cout << "Input n:";do{cin >> n;}while(n <= 1 || n > 10);cout << "Input n integers:";for(int i = 0; i < n; i ++){cin >> s[i];}cout << "Input x:";cin >> x;int r = search(s, n, x);if(r == 0 && flag == 0) cout << "Not found";else cout << "index = " << r;return 0; }

7.交換數(shù)組中最大最小元素位置

題目:

利用案例1中的swap函數(shù),從鍵盤輸入10個整數(shù),用函數(shù)編程實現(xiàn)計算其最大值和最小值,并互換它們在數(shù)組中的位置。

要求:1)定義swap(int *x, int *y)函數(shù),實現(xiàn)兩個數(shù)據(jù)的交換;

? 2)定義函數(shù)FindMaxMinid(int arr[], int nCount, int *max_id, int *min_id),找到數(shù)組arr中最大元素和最小元素的下標(biāo);

? 3)在main函數(shù)中實現(xiàn)數(shù)組的輸入;調(diào)用FindMaxMinid函數(shù);并交換兩個元素的位置。
【輸入形式】
【輸出形式】
【樣例輸入】

1 2 3 4 5 6 7 8 9 10
【樣例輸出】

10 2 3 4 5 6 7 8 9 1

代碼:

#include <iostream> #define N 10 using namespace std;void swap(int *x, int *y) {int temp = *x;*x = *y;*y = temp; }void FindMaxMinid(int arr[], int nCount, int *max_id, int *min_id) {for(int i = 0; i < nCount; i ++){if(arr[i] > max_id[0]) max_id = &arr[i]; //讓指針指向最值;if(arr[i] < min_id[0]) min_id = &arr[i];}swap(max_id, min_id); }int main() {int s[N];for(int i = 0; i < N; i ++){cin >> s[i];}FindMaxMinid(s, N, s, s);for(int i = 0; i < N; i ++){cout << s[i] << " ";}return 0; }

8.打印極值點下標(biāo)

題目:

在一個整數(shù)數(shù)組上,對于下標(biāo)為i的整數(shù),如果它大于所有它相鄰(左邊和右邊)的整數(shù),或者小于所有它相鄰的整數(shù),則稱為該整數(shù)為一個極值點,極值點的下標(biāo)就是i。

【輸入形式】有2×n+1行輸入:第一行是要處理的數(shù)組的個數(shù)n;對其余2×n行,第一行是此數(shù)組的元素個數(shù)k(4<k<80),第二行是k個整數(shù),每兩個整數(shù)之間用空格分隔。

【輸出形式】輸出為n行:每行對應(yīng)于相應(yīng)數(shù)組的所有極值點下標(biāo)值,下標(biāo)值之間用空格分隔。

【樣例輸入】

3

10

10 12 12 11 11 12 23 24 12 12

15

12 12 122 112 222 211 222 221 76 36 31 234 256 76 76

15

12 14 122 112 222 222 222 221 76 36 31 234 256 76 73

【樣例輸出】

0 7

2 3 4 5 6 10 12

0 2 3 10 12 14

代碼:

#include <iostream> #define N 100 using namespace std;void Find(int s[], int len) {for(int i = 0; i < len; i ++){if(i == 0 && s[i] != s[i + 1]) cout << i << " ";if(i != 0 && i != len - 2){if(s[i] < s[i + 1] && s[i] < s[i - 1]) cout << i << " ";if(s[i] > s[i + 1] && s[i] > s[i - 1]) cout << i << " ";}if(i == len - 1 && s[i] != s[i - 1]) cout << i << " ";}}int main() {int s[N];int num = 0;cin >> num;while(num --){int len;cin >> len;for(int i = 0; i < len; i ++){cin >> s[i];}Find(s, len);}return 0; }

9.利用指針交換兩個數(shù)組中的數(shù)據(jù)并排序

題目:

編寫函數(shù)int Read(int *x); 錄入數(shù)組元素,返回值代表元素個數(shù)。錄入數(shù)據(jù)時輸入0代表數(shù)據(jù)錄入的結(jié)束。

編寫函數(shù)void Print(int *x, int n); 負(fù)責(zé)輸出數(shù)組中的元素。

編寫函數(shù)void Swap(int *x, int m, int *y, int n); 交換主函數(shù)里的兩個整形數(shù)組a和b里的數(shù)據(jù)值,其中ab中數(shù)據(jù)元素的實際個數(shù)傳遞給形參m和n。

編寫函數(shù)void Sort(int *x, int n); 負(fù)責(zé)實現(xiàn)數(shù)組元素從小到大的排序。

【樣例輸入】

9 3 2 8 5 7 0

6 1 4 0

【樣例輸出】

6 1 4

9 3 2 8 5 7

1 4 6

2 3 5 7 8 9

【提示】首先依次輸出交換后的數(shù)組元素值,然后依次輸出排序后的數(shù)組元素值。

代碼:

#include <iostream> using namespace std;int a[100]; int b[100]; int c[100];void QuickSort(int a[], int left, int right) {if (left < right){int mid = a[(left + right) / 2];int low = left - 1;int high = right + 1;while (low < high){while (a[ ++ low] < mid);while (a[ -- high] > mid);if (low < high)swap(a[low], a[high]);}QuickSort(a, left, low - 1);QuickSort(a, high + 1, right);} }int main() {int ch;int pos = -1;while (cin >> ch){c[ ++ pos] = ch;}if (pos == 0) return 0;int newpos1 = 0;pos = 0;while (c[pos] != 0){a[newpos1 ++ ] = c[pos ++ ];}pos ++;int newpos2 = 0;while (c[pos] != 0){b[newpos2 ++ ] = c[pos ++ ];}for (int i = 0; i < newpos2; ++ i)cout << b[i] << " ";cout << endl;for (int i = 0; i < newpos1; ++ i)cout << a[i] << " ";QuickSort(b, 0, newpos2 - 1);QuickSort(a, 0, newpos1 - 1);cout << endl;for (int i = 0; i < newpos2; ++ i)cout << b[i] << " ";cout << endl;for (int i = 0; i < newpos1; ++i)cout << a[i] << " ";return 0; }

10.利用指針交換兩個數(shù)據(jù)

題目:

編寫函數(shù)void swap(int *x, int *y); 交換主函數(shù)里的兩個整形變量a和b的值。輸入和輸出語句都在主函數(shù)內(nèi)部完成。

【樣例輸入】

3 4

【樣例輸出】

4 3

代碼:

#include<iostream> using namespace std;void swap(int *x, int *y) {int temp = *x;*x = *y;*y = temp; } int main() {int a, b;cin >> a >> b;swap(a, b);cout << a << ' ' << b; }

歡迎提問,學(xué)弟學(xué)妹們加油~

總結(jié)

以上是生活随笔為你收集整理的第七周:字符串 + 数组 + 指针的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。