第三章 字符串与数组 学习笔记

第三章 字符串与数组

  1. 比较大的数组因尽量声明再main函数外,否则程序可能无法运行

  2. 如果要从数组A复制K个元素到数组B,可以调用memcpy(b,a,sizeof (int) *K) 它包含在头文件 string.h

  3. memset(a, 0 ,sizeof(a) ) string.h 中定义,作用是清零数组a

  4. “在多数情况下,最好在做一件事之前检查是不是可以做,而不是做完后悔。因为‘悔棋’往往比较麻烦。” 即编写程序时,先要进行的是分析,而不是直接做,最好能写下伪代码。磨刀不误砍柴功,这样做不仅仅能提高编写代码的速度,也能提高AC的正确率。

字符数组

  1. scanf("%s", s) 不能读入空格,TAB和回车。注意,注意:字符串s前面没有&。可以用scanf("%s",s[i]) 读取第s[8][9]中的第i个字符串。
  2. sprintf(buf,"%d%d%d",a,b,c) 表达的意思:将整数 a b c 转换成字符并保存在字符数组buf中。详情请移步keenleung 的博客《C++:sprintf()的用法
  3. strlen(s) 可以用于返回字符数组的“0”之前的长度
  4. 字符串的“赋值''、“比较”、“连接"不能用"="、"=="和"+",只能用strcpy(a,b)strcmp(a,b)strcat(a,b) 注意,它们都在string.h中声明。
  5. 编写程序时,++,--,+= 这些运算符虽然可以简化程序,但是用以造成不可察觉到错误,使用时要考虑清楚。

竞赛题笔记

  1. getchar() 等价于 fgetc(stdin) ,它们都能从缓冲区中读入空格、TAB和回车键。

  2. fgets(buf,10,fin) 读取完整的一行,并放入字符数组buf中,当一个字符都没有是,fegets返回值为NULL。

  3. 常量数组的定义 char s [] = "1 2 3 4 5 s g e j o m"; 又是常量数组可以达到意想不到的效果。

  4. isalpha 函数可以用来判断是否为字母,它在ctype.h中定义,在头文件ctype.h中定义的还有isalphaisdigitisprint 函数,同样可以用来判断字符的属性,toupppertolower工具可以用来转换大小写。

  5. “字典序”:就是字符串在字典中的顺序。例如:“abc的字典序比bcd小”。

补充

  1. 进位制:十进制是“逢十进一”,顾名思义,二进制就是“逢二进一”。

  2. 进制转换:

    • 十转二:
    • 二转十:例如:二进制101转换为5的过程为——5 = 2x1^2+2x0+1;

练习题代码

得分(score,UVa1585

/**
 * @author Miubai
 * Score
 */

#include <iostream>
#include <cstring>
using namespace std;

#define  MAX 500
int main (){
    int n;
    char s[MAX];
    cin >> n;
    while ( n -- ){
        int count = 0 ;
        int sum = 0;
        scanf("%s",s);
        for (int i = 0;i < strlen(s);i++){
            if(s[i] == 'O') {
                count++;
                sum += count;
            } else
                count = 0;
        }
        cout << sum << endl;
    }
    return 0;
}

分子量(Molar Mass,UVa1586)


数数字(Digit Counting UVA - 1225)

/**
 * @author Miubai
 * Digit Counting UVA - 1225 
 */
#include <iostream>
#include <cstring>
using namespace std;

#define MAX 10001
char s[MAX];

int count[10];
int main() {
    int n,N;
    cin >> N;
    while (N--) {
        memset(count, 0, sizeof(count));
        cin >> n;
        for (int i = 1; i <= n; i++) {
            sprintf(s, "%d", i);
            for (int j = 0; j < i; j++) {
                switch (s[j]) {
                    case '0':
                        count[0]++;
                        break;
                    case '1':
                        count[1]++;
                        break;
                    case '2':
                        count[2]++;
                        break;
                    case '3':
                        count[3]++;
                        break;
                    case '4':
                        count[4]++;
                        break;
                    case '5':
                        count[5]++;
                        break;
                    case '6':
                        count[6]++;
                        break;
                    case '7':
                        count[7]++;
                        break;
                    case '8':
                        count[8]++;
                        break;
                    case '9':
                        count[9]++;

                }
            }
        }
        for (int i = 0; i <= 9; i++) {
            cout << count[i] << " ";
        }
        cout << endl;
    }

    return 0;
}


笔者注:数数字(Digit Counting UVA - 1225)这题样例已经过了但是未能AC,笔者还未找出错误原因,望大佬指正。

原文地址:https://www.cnblogs.com/Miubai-blog/p/12803078.html