ACM模式刷题常用

LeedCode

输入数据处理–ACM

1. 字符串的输入处理 -

// cin>>n;  getline(cin,str)  之间要加cin.ignore(), 消除上一个换行符
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<string>
#include<sstream>
int main()
{
    string s;
    while(getline(cin, s))
    {
        string x;
        stringstream ss(s);    // include<sstream>			
        while (getline(ss, x,' '))			//getline 是单引号
        {
            cout << x;         // "105",  "a"
            cout << x.at(0);   //  '1' ,  'a'    
            cout << stoi(x);   //string to int 仅限数字
        }
    }
}

2. 链表的输入建立

//  !!! 指针的操作 按照指针的箭头顺序
#include<iostream>
#include<vector>
using namespace std;

struct ListNode{
    int val;
    ListNode * next;
    ListNode(int x) : val(x), next(NULL){}
};

int showList(ListNode* head)
{
    ListNode *p = head;
    while(p!=NULL)
    {
        cout << p->val;
        p = p->next;
    }
}

// 删除某个结点
ListNode * deleteNode(ListNode* head, int target)
{
    ListNode *dum = new ListNode(-1);   // 设置一个虚拟头结点
    dum->next = head;   // 将虚拟头结点指向head,方便后面操作
    ListNode *cur = dum;
    while (cur->next!=NULL)
    {
        if(cur->next->val==target)
        {
            ListNode *tmp = cur->next;//  用tmp保存 cur->next,用于之后的释放。不能直接cur->next = cur->next->next; delete cur->next
            cur->next = tmp->next;
            delete tmp;
        }
        else
            cur = cur->next;
    }

     // head = dum->next;
    // return head;
    return dum->next;
}

int main() {
    int x, n;
    cin >> n;
    vector<int> v;
    ListNode *dummpy = new ListNode(0); // 设置一个虚拟头结点
    ListNode *cur = dummpy;     //设置一个移动指针,初始为dummy
    
    while (n--)
    {
        cin >> x;
        //尾插法
        ListNode *node = new ListNode(x);
        cur->next = node;
        cur = cur->next;     
    }

    showList(dummpy->next);
    deleteNode(dummpy->next, 5);
    showList(dummpy->next);
    return 0;
}

3. 常用头文件和库函数

#include<bits/stdc++.h>  //万能头文件 注意.h
#include<cctype>  // 字符相关的

4.字符处理

#include<cctype>
//  x = tolower(x);
isalnum()	字母或数字
isalpha()	字母
iscntrl()	控制字符
isdigit()	数字(0~9)
isgraph()	除了空格之外的打印字符
islower()	小写字母
isprint()	打印字符(包括空格)
ispunct()	标点符号
isspace()	标准空白字符(空格、进纸、换行符、回车、水平制表符、垂直制表符)
isupper()	大写字母
isxdigit()	十六进制数字(0~9, a~f或A~F)
tolower()	如果参数是大写字符,返回其小写,否则返回该参数
toupper()	如果参数是小写字符,返回其大写,否则返回该参数		  

4. 字符串的处理

int i = atoi(str)     	// “数字型“字符串   转换为整数 		c语言
double d = atof(str)     // “数字型“字符串   转换为浮点型f		c语言    

int i = stoi(s1);		 // string to int     string		c++语言
float f = stof(s2);	 	  // string to float    string		 c++语言
string str = to_string(m)  // to_string(str) 将各种类型数字转成字符串。 c++11		!!!!!!!!!
A = toupper(a);		//  字母大小写转换
a = tolower(A);
cin>>s;  getline(cin,s);  // cin不能输入空格, getline可以输入空格,遇到换行号截止。  getline()前加入cin.ignore()消除换行符
cout<< s <<endl;  //可以直接读取字符串  

stringstream str(ss);
string x;
while(getline(str,x,",")){}

// 2. 小数转换和输出 
float a = 5/3.0;	 	// 至少一个是浮点型
float a = 5./0; 
float b = float(a)		//强制类型转换    
float b = static_cast<float>(a)	// c++ 静态转化
int i2 = (int)c;  int i2 = int(c);  //char 转 int 
  
printf("%.2f",number) 	//输出小数后2位	

// 3. 初始化f
int arr[500] = {0};  // 
vector<int> vec1 = {1, 2, 3, 4, 5};
vector<int> vec1(4,1); 	//初始化4个1
string str1 = "Hello";			      
String str = new String("Hello");
for (auto &i : arr )		//要修改内部元素,需要取地址  &
    
// 4. 进制转换
 int shi = stoi(str, nullptr, 16)	// 2、8、16进制的字符串转换为十进制
 int num = 10; // 要转换的10进制数
 char str[100]; // 放结果
 itoa(num, str, 2);  			   // 十进制转2、8、16进制,保存到str中
 cout<<str;
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值