Life of xhu

About

C语言标准库学习 & HOJ_1251

Jan 27, 2014

  |   #STL   |   #HOJ

今天在网上看到了一个题目,题目是这样的

对于一个给定的字符串,在自然数序列组成的字符串中找出最早出现的和其相匹配的子串出现的位置。自然数序列为12345678910111213...,如果输入101,那么就输出10

对于字符串的处理我真是看了一顿头大,算了,直接看大神的代码吧,这一下让我蛋疼了,我发现我不仅题目不会做,连大神写的代码都看不太懂。。。差距还是太大了啊,今天这道题,就当是一次C语言学习吧。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
using namespace std;

int main(){
    char s[100010], key[200];
    for(int i = 1; i < 17000; i++)
        sprintf(s + strlen(s), "%d", i);
    cin >> key;
    cout << strstr(s, key) - s + 1 << endl;
}

这段代码我觉得有三个知识点很重要:

  1. 在C语言中,声明一个数组,其实就是声明一个指针,比如char s[10010],就是声明了一个名称为s的指针,并且其之后10010的范围都被初始化了,单独输出s时,表面上看是输出了字符串,本质就是输出了s这个指针指向的位置到终结符\0指向的位置之间的所有的数据。

    测试代码:

     #include <iostream>
     #include <iomanip>
     #include <stdio.h>
     using namespace std;
    
     int main(){
         int a[6] = {1, 2, 3, 4, 5, 6};
         cout << a[1] << endl;     //=> 2
         cout << *a << endl;       //=> 1
         cout << *(a + 1) << endl;     //=> 2
     }
    
  2. sprintf函数,需要头文件stdio.h,百度百科的解释是:字串格式化命令,主要功能是把格式化的数据写入某个字符串中。简单点说,就是这个函数可以把任何数据转化为字符串,最常用的估计就是把整数转为字符串了,标准格式为:

     int sprintf( char *buffer, const char *format, [ argument] … );
    

    其中,第一个参数buffer,是转换后存储的指针,第二个参数format,是一个格式化的数据,后面是可选参数,就是需要使用的值什么的。

    测试代码:

     #include <iostream>
     #include <stdio.h>
     #include <cstring>
     #include <string>
     using namespace std;
    
     int main(){
         char test[100];
         int a = 1;
         sprintf(test, "the value of a is %d, ", a);
         cout << test << endl;
         cout << strlen(test) << endl;
         sprintf(test + strlen(test), "and this is another sentence.");
         cout << test << endl;
         cout << strlen(test) << endl;
     }
    

    输出为:

     the value of a is 1,
     21
     the value of a is 1, and this is another sentence.
     50
    
  3. strstr函数,作用就是子串匹配,格式为strstr(*a, *b),从第一个知识点我们可以知道,其实一般用时两个参数都会放字符串,返回的是a中匹配b的子串的起始位置的指针。

    测试代码:

     #include <iostream>
     #include <cstring>
     #include <string>
     using namespace std;
    
     int main(){
         char a[10] = "one test\0";
         char *sub = strstr(a, "e t");
         cout << sub << endl;  //想想会输出什么
         cout << *sub << endl;
         cout << sub - a << endl;   //输出的是两个指针之间的距离,相当于子串起始位置在父串中的角标,做题时很好用
     }
    

    输出:

     e test
     e
     2
    

以上就是今天对C语言标准库的学习,可以说,通过一道不会的题,以及大神的代码,我真是受益匪浅,而且我也认识到,我和大神的差距,不仅在于编程能力,有些也是对一门语言的了解不足,很多时候,一个语言的标准库已经提供了相当多的方法和函数,在实际编程中运用,更能提高编程效率。

然后就是一道HOJ水题了,很弱的一道题,就是多项式输出,格式注意一下就好了。

代码如下:

//HOJ_1251
#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
    int num[9];
    bool all_0;
    while(scanf("%d %d %d %d %d %d %d %d %d",&num[0],&num[1],&num[2],&num[3],&num[4],&num[5],&num[6],&num[7],&num[8])!=EOF){
        all_0 = true;
        for(int i = 0; i < 7; i++){
            if(num[i] != 0){
                if(all_0){
                    if(num[i] == 1)
                        cout << "x^" << 8 - i;
                    else if(num[i] == -1)
                        cout << "-x^" << 8 - i;
                    else
                        cout << num[i] << "x^" << 8 - i;
                }else{
                    if(num[i] == 1)
                        cout << " + x^" << 8 - i;
                    else if(num[i] == -1)
                        cout << " - x^" << 8 - i;
                    else if(num[i] > 0)
                        cout << " + " << num[i] << "x^" << 8 - i;
                    else
                        cout << " - " << num[i] * (-1) << "x^" << 8 - i;
                }
                all_0 = false;
            }
        }

        if(num[7] != 0){
            if(all_0){
                if(num[7] == 1)
                    cout << 'x';
                else if(num[7] == -1)
                    cout << "-x";
                else
                    cout << num[7] << 'x';
                all_0 = false;
            }else{
                if(num[7] == 1)
                    cout << " + x";
                else if(num[7] == -1)
                    cout << " - x";
                else if(num[7] > 0)
                    cout << " + " << num[7] << 'x';
                else if(num[7] < 0)
                    cout << " - " << num[7] * (-1) << 'x';
            }
        }

        if(num[8] != 0){
            if(all_0)
                cout << num[8];
            else{
                if(num[8] > 0)
                    cout << " + " << num[8];
                else if(num[8] < 0)
                    cout << " - " << num[8] * (-1);
            }
            all_0 = false;
        }

        if(all_0)
            cout << 0;

        cout << endl;
    }
}