算法竞赛入门经典(第 2 版)——语言篇

本文介绍了算法竞赛中输入输出的常见框架,包括重定向版和fopen版,以及C++中利用流进行字符串和数字转换的方法。同时讲解了C++中的字符判断函数,如isalpha、isdigit等,以及头文件cctype的相关用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序中的计时

#include<time.h>

printf("Time used=.%2lf\n" ,(double)clock() / CLOCKS_PER_SEC);

Π(圆中的 pai 的表示)

#include<math.h>
const double pi = acos(-1.0);

算法竞赛中的输入输出框架

重定向版

#define LOCAL   //提交前删除本行就可以

 #ifdef LOCAL
     freopen("datain.txt","r",stdin);
     freopen("dataout.txt","w",stdout);
 #endif 

fopen版

int main(){
FILE *fin,*fout;
fin=fopen("datain.txt","rb");
fout=fopen("dataout.txt","wb");
int a,b;
while(fscanf(fin,"%d%d",&a,&b)==1)
    fprintf(fout,"%d\n",a+b);
fclose(fin);
fclose(fout);
  return 0;
}

strchr 的作用是在一个字符串中查找单个字符,会返回查询的自符以及查询的字符之后的所有字符

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
    char buf[99]="abcdefg";
    if(strchr(buf,'d')!=NULL){
        cout<<strchr(buf,'d')<<endl;
    }
    return 0;
}

//输出结果:defg

C++中的流(通过流来实现字符串和数字的转换)

       %% 印出百分比符号,不转换。
  %c 整数转成对应的 ASCII 字元。
  %d 整数转成十进位。
  %f 倍精确度数字转成浮点数。
  %o 整数转成八进位。
  %s 整数转成字符串。
  %x 整数转成小写十六进位。
  %X 整数转成大写十六进位。
  %n sscanf(str, "%d%n", &dig, &n),%n表示一共转换了多少位的字符

printf 输出到屏幕

sprintf 输出到字符串中

fprintf 输出到文件

sprintf函数原型为 int sprintf(char *str, const char *format, ...)。作用是格式化字符串,具体功能如下所示:

 (1)将数字变量转换为字符串。

 (2)得到整型变量的16进制和8进制字符串。

 (3)连接多个字符串。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
    char str[256] = { 0 };
    int data = 1024;
    //将data转换为字符串
    sprintf(str,"%d",data);
    //获取data的十六进制
    sprintf(str,"0x%X",data);
    //获取data的八进制
    sprintf(str,"0%o",data);
    const char *s1 = "Hello";
    const char *s2 = "World";
    //连接字符串s1和s2
    sprintf(str,"%s %s",s1,s2);
    cout<<str<<endl; 
    return 0;
} 

sscanf函数原型为int sscanf(const char *str, const char *format, ...)。将参数str的字符串根据参数format字符串来转换并格式化数据,转换后的结果存于对应的参数内。具体功能如下:

  (1)根据格式从字符串中提取数据。如从字符串中取出整数、浮点数和字符串等。

  (2)取指定长度的字符串

  (3)取到指定字符为止的字符串

  (4)取仅包含指定字符集的字符串

  (5)取到指定字符集为止的字符串

  当然,sscanf可以支持格式串"%[]"形式的,有兴趣的可以研究一下。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
    char s[15] = "123.432,789";
    int n;
    double f1;
    int f2;
    sscanf(s, "%lf,%d%n", &f1, &f2, &n);
    cout<<f1<<" "<<f2<<" "<<n;
    return 0;
} 
//输出结果:123.432 789 11, 即一共转换了11位的字符。

<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。

  1.stringstream::str(); returns a string object with a copy of the current contents of the stream.

  2.stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.

  3.stringstream清空,stringstream s; s.str("");

  4.实现任意类型的转换

一个通用的转换模板,用于任意类型之间的转换。函数模板convert()含有两个模板参数out_type和in_value,功能是将in_value值转换成out_type类型:

template<class out_type,class in_value>
out_type convert(const in_value & t)
{
stringstream stream;
stream<<t;//向流中传值
out_type result;//这里存储转换结果
stream>>result;//向result中写入值
return result;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream> 
using namespace std;
template<class out_type,class in_value>
out_type convert(const in_value & t)
{
stringstream stream;
stream<<t;//向流中传值
out_type result;//这里存储转换结果
stream>>result;//向result中写入值
return result;
}
int main(){
    string s = "1 23 # 4";
    stringstream ss;
    ss<<s;     //将string输入流
    while(ss>>s){  //从stream中抽取前面插入的值
        cout<<s<<endl;
        int val = convert<int>(s);
        cout<<val<<endl;
    }
    ss.clear(); //在进行多次转换前,必须清除stream
    return 0;
}

// 输出:1 1 23 23 # 0 4 4

参考博客:https://blog.youkuaiyun.com/fanyun_01/article/details/66967710

https://blog.youkuaiyun.com/Sophia1224/article/details/53054698

 

 

判断字符的函数(头文件 cctype)

isalpha 判断字符是否为字母

isdigit 判断是否为数字

isprint 判断是否为字符    (字母,数字也是字符)

tolower()函数是把字符串都转化为小写字母

toupper()函数是把字符串都转化为大写字母

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;
int main(){
    char buf[99]="156Aefg !";
    for(int i=0;i<strlen(buf);i++){
        if(isalpha(buf[i]))
            cout<<"字母:"<<buf[i]<<endl;
        if(isdigit(buf[i]))
            cout<<"数字:"<<buf[i]<<endl;
        if(isprint(buf[i]))
             cout<<"字符:"<<buf[i]<<endl;
    }
    return 0;
}

algorithm头文件定义了一个count的函数,其功能类似于find。这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果。

string类型的大小写转换:https://blog.youkuaiyun.com/qq_31186409/article/details/50545682

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值