【PTA】代码部分基础整理

目录

一、结尾无空格

二、数字和字符串转换

三、数值格式化输出

四、字符串长度

五、判断字符串是否相等

六、字母大小写转换

1.char 类

2.string 类

七、define、typedef、const


报名了PTA考证,最近打算开始刷题,之前没怎么打C代码,基础不是很好。积累一些自己容易忘的知识或者没发现的技巧在里面

一、结尾无空格

1.把空格的输出放在内容的输出之前

for(int i=1;i<=N;i++){
    if(i!=1)printf(" ");
    printf("%d\n",context)
}

 2.计数变量count,当count不等于0时输出空格

int n = 10;
int temp = n;
for(int i=0;i<n;i++){
	cout << temp;
	temp--;
	if (temp != 0)cout << " ";
}

二、数字和字符串转换

//数字转字符串sprintf
int a=1234321;
double b=123.4321;
char s1[10];
char s2[10];
sprintf(s1, "%d", a);
sprintf(s2, "%.3lf", b);
//数字转字符串to_string
string s = to_string(a + b);

//字符串转数字
char s1[]="1234321";
char s2[]="123.321";
int a;
sscanf(s1,"%d",&a);
double b;
sscanf(s2,"%lf",&b);

sprintf函数具体使用方式:https://www.runoob.com/cprogramming/c-function-sprintf.html

sscanf函数具体使用方式:https://www.runoob.com/cprogramming/c-function-sscanf.html

其他的还有stoi【用于string】https://blog.csdn.net/sinat_40872274/article/details/81367815

三、数值格式化输出

#include<iostream>
#include <iomanip>
using namespace std;
double a=3.1415926;
//保留两位数字
cout << setiosflags(ios::fixed) << setprecision(2) << a;
#include<cstdio>
double ans=3.1415926
printf("%.2f",ans);

四、字符串长度

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

int main() {
	char a[20] = { "1316167" };
	string s1 = "dsaf65";
	cout << strlen(a)<<endl;
	cout << s1.length();
	return 0;
}

输出结果:

7

6

五、判断字符串是否相等

//第一种
//判断是否相同
//这里只能用单引号
char a = 'F';
if (a == 'F')cout << "True";

//第二种
char a[10] = "abcd";
if (strcmp(a,"abcd")==0)cout << "True";

//第三种
//判断是否相同
//#include<cstring>
string s1 = "abc", s2 = "abc";
if (s1==s2)cout << "True";

在strcmp函数中。strcmp(const char* s1,const char* s2)的返回值是int型

s1<s2时,返回-1;s1==s2时,返回0;s1>s2时,返回1。可用于判断s1是否包含s2

六、字母大小写转换

1.char 类

  • tolower(char c)和toupper(char c)
#include<iostream>
using namespace std;
int main() {
	char a = 'a',b='B';
	a = toupper(a), b = tolower(b);
	cout << a << " " << b;
	return 0;
}
  • s[i]±=32
#include<iostream>
using namespace std;
int main() {
	char a = 'a',b='B';
	a = a - 32; b = b + 32;  //a大写,b小写
	cout << a << " " << b<<endl;
	return 0;
}

2.string 类

  • transform(str.begin(),str.end(),str.begin(),::tolower);
#include<iostream>
#include <algorithm>
using namespace std;
int main() {
	string str;
	cin >> str;	
	transform(str.begin(), str.end(), str.begin(), ::tolower);
	cout << str << endl;
	transform(str.begin(), str.end(), str.begin(), ::toupper);
	cout << str << endl;
	return 0;
}
  • 循环设置每个字符
#include<iostream>
#include<string>
using namespace std;
int main() {
	string str;
	getline(cin,str);
	//up
	for (int i = 0; i < str.length(); i++) {
		if (str[i] >= 'a' && str[i] <= 'z') {
			str[i] = str[i] -32;
		}
	}
	cout << str<<endl;
	//low
	for (int i = 0; i < str.length(); i++) {
		if (str[i] >= 'A' && str[i] <= 'Z') {
			str[i] = str[i] + 32;
		}
	}
	cout << str;
	return 0;
}

七、define、typedef、const

这几个都有不一些不同的用法,这里只列举了最常用的,详细的可以参考其他博主的博客.

define:https://blog.csdn.net/u012611878/article/details/52534622

const:https://www.cnblogs.com/lanjianhappy/p/7298427.html

typedef:https://www.cnblogs.com/eeluo/articles/7470826.html

1.#define 标识符 字符串

它将一个标识符定义为一个字符串,定义之后标识符不可在后续程序中修改

2.const 类型说明符 变量名

同样的其他函数不能对该成员赋值。它与define的区别是:

const 拥有内存空间有地址,所以要用数据类型定义

define 只是一个表示符号

3.typedef 类型名称 类型标识符;

为已有的数据类型重新命名

【任何声明变量的语句前面加上typedef之后,原来是变量的都变成一种类型】

//define
#define END "end"
#define MAXN 10001

//const int
const int MAXN=10001
const float=52.0

//typedef
typedef int I;  //指定标识符I代表int类型
typedef double D;  //指定标识符D代表double类型
//以下两者等价
int i; double j;
I i;D j;
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值