机试笔记-1

1.输入与输出

在实际机考题目中,大多数情况下对于浮点数都使用double而不是float类型。对于四种输入,除了字符串参数是不需要加&符号,其他都要加,作为传递参数。输出格式中"\n"表示换行,在输出中,参数不需要加&号。这里可以理解为一个%号代表一个参数,比如说:
scanf("%c%c%d",&x,&y,&z);这里三个%对应三个参数,分别是xyz,%后字母代表其类型。

#输入int型变量
scanf("%d",&x);

#输入long long型变量
scanf("%lld",&x);

#输入double型变量
scanf("%lf",&x);

#输入char类型的变量
scanf("%c",&x);

#输入字符串类型
scanf("%s",s);

#输出通用格式
printf("%d\n",s);

2.输入格式问题

对于下面代码程序,如果输入1998-2-4,会得到1998 2 4;

但是如果输入1998 2 4,就只能得到1998 0 0;

int main()
{
    int year, month, day;
    scanf("%d-%d-%d",&year,&month,&day);
    printf("%d %d %d\n",year,month,day);
}

例如输入:hello world, my name is Han Mei Mei !

代码运行后结果都是hello world, my name is Han Mei Mei !

    string s = "hello world, my name is Han Mei Mei !";
    cout << s << endl;
    printf("%s\n",s.c_str());

但是指的注意的是,对于string类型,输出需要使用字符串.c_str()方法或者直接cout将字符串输出。

如果遇到需要读取一行字符串,单独使用scanf以及cin都会遇到空格而停止。所以需要使用getline()方法进行读取。

    string s ;
    getline(cin,s);
    
    cout << s << endl;
    printf("%s\n",s.c_str());


    //如果是字符串数组,则有cin.getline()
      char s[105];
      cin.getline(s,105);

反正,对于读入字符串,不建议使用scanf,直接用cin或者getline(cin,s)读取。

3.输出进制转换

假设给定一个int类型变量a为10,那么可以"%x"与"%X"其转化为小写的16进制与大写的16进制。

int main()
{
    int a = 10;
    printf("%d\n",a); //10    
    printf("%x\n",a); //a     大写十六进制输出
    printf("%X\n",a); //A     小写十六进制输出
    printf("%o\n",a); //12     八进制输出
}

4.输出增加前置0

int main()
{
    int a = 5;
    printf("%04d\n",a); //4代表宽度,用0补充  0005
    printf("% 4d\n",a);//   5
}

5.输出保留小数

    double a = 3.678;
    printf("%.1lf",a); //3.7
    printf("%.2lf",a); //3.68
    printf("%.3lf",a); //3.678
    printf("%.4lf",a); //3.6780
    
    double b = 3.000;
    printf("%g",b); //3

如果有小数就输出小数,没有小数输出整数,则 %g即可。

6.ASCII码,不用硬背,忘了直接输出来看

int main()
{
    printf("%d\n",'A'); //65
    printf("%d\n",'a'); //97
}

7.万能头文件

#include <bits/stdc++.h>

不排除有些地方考场是不支持该头文件,那么就手动输入了,基本上如下:

#include <cstdio.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>

8.C++的STL应用

C++的头文件#include <algorithm>提供了很多的实用函数,直接拿来用。

1.排序sort():以此传入三个参数,排序区间起点,排序区间的终点+1,比较函数。

此处比较函数如果不填则默认为从小到大。

int n;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
        // scanf("%d",&a[i]);
    }
    
    sort(a,a+n); //默认从小到大
    
    for(int i=0;i<n;i++)
    {
        printf("%d",a[i]);
    }

如果要实现从大到小,可以自写一个比较函数即可

int a[10];

bool compare(int a,int b)
{
    return a>b;
}

int main()
{
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
        // scanf("%d",&a[i]);
    }
    
    sort(a,a+n,compare); 
    
    for(int i=0;i<n;i++)
    {
        printf("%d",a[i]);
    }
}

2.二分查找中的lower_bound()函数与upper_bound()函数

lower_bound(begin,end,num) 从数组begin位置到end-1位置二分查找第一个大于或等于num的数字,返回该数字的地址,不存在则返回end. 

反之,如果lower_bound(begin,end,num,compare())从数组begin位置到end-1位置二分查找第一个于或等于num的数字,返回该数字的地址,不存在则返回end. 


upper_bound(begin,end,num)从数组begin位置到end-1位置二分查找第一个大于num的数字,返回该数字的地址,不存在则返回end. 

反之,如果upper_bound(begin,end,num,compare())从数组begin位置到end-1位置二分查找第一个于num的数字,返回该数字的地址,不存在则返回end. 

通过返回的地址减去起始地址begin即可找到数字在数组中的下标。

int main()
{
   int num[6] = {1,7,2,4,34,17};
   sort(num,num+6); //从小到大排序: 1 2 4 7 17 34
   
   //想要查找第一个大于或等于7的数
   int pos1 = lower_bound(num,num+6,7) - num;
   
   //想要查找第一个大于7的数
   int pos2 = upper_bound(num,num+6,7) - num;
   
   cout << pos1 << " " << num[pos1] << endl ; //3 7
   cout << pos2 << " " << num[pos2] << endl ; //4 17
   
   cout << " ===================================== " << endl;
   sort(num,num+6,compare); //从大到小排序: 34 17 7 4 2 1
   
   for(int i = 0;i<6;i++) cout << num[i] << " " ;
   
   //想要查找第一个小于或等于7的数
   int pos3 = lower_bound(num,num+6,7,greater<int>()) - num;
   
   //想要查找第一个小于7的数
   int pos4 = upper_bound(num,num+6,7,greater<int>()) - num;
   
   cout << pos3 << " " << num[pos3] << endl ; //2 7
   cout << pos4 << " " << num[pos4] << endl ; //3 4
}

3.大根堆 priority_queue<int,vector<int>,less<int>> 从大到小

int main()
{
    priority_queue<int,vector<int>,less<int>> q ;
    q.push(2);
    q.push(5);
    q.push(1);
    
    while(!q.empty())
    {
        cout << q.top() << endl; //只是查看队首
        q.pop(); //弹出队首
    }
    
    // 5 2 1 
}

4.小根堆 priority_queue<int,vector<int>,greater<int>> 从小到大

int main()
{
    priority_queue<int,vector<int>,greater<int>> q ;
    q.push(2);
    q.push(5);
    q.push(1);
    
    while(!q.empty())
    {
        cout << q.top() << endl; //只是查看队首
        q.pop(); //弹出队首
    }
    
    // 1 2 5
}

5.vector:倍增思想下的可变容器。

int main()
{
   vector<int> v;
   for(int i =0;i<6;i++){
       v.push_back(i*i); //将值存入vector
   }
   
   for(int i =0;i<v.size();i++){
       cout << v[i] << " "; //将值存入vector
   }
   
   //0 1 4 9 16 25
}

其实把vector看作是数组就行,甚至说它比数组还要强大,空间是可变的。

6.queue队列

int main()
{
   queue<int> q;
   q.push(1);
   q.push(4);
   q.push(2);
   
   while(!q.empty()){
       cout << q.front() << endl;
       q.pop();
   }
   
   // 1 4 2
}

7.stack 栈

int main()
{
  stack<int> s;
  s.push(1);
  s.push(5);
  s.push(4);
  
  while(!s.empty()){
      cout << s.top() << endl;
      s.pop();
  }
  
  //4 5 1
}

8.map<string,int> dict 定义了一个key:value映射关系的map,其中map是无序访问的

int main()
{
    map<string,int> dict;
    dict["Tom"] = 98 ;
    dict["Wei"] = 99 ;
    dict["Jane"] = 96 ;
    
    if(dict.count("Jane")){
        cout << "Jane score is " << dict["Jane"] << endl;
    }
    
    //迭代循环
    for(map<string,int>::iterator it = dict.begin();it != dict.end();++it){
        cout << it->first <<  " score is " << it ->second << endl;
    }
    
    dict.clear(); //清空map
    
    /*
    Jane score is 96
    Jane score is 96
    Tom score is 98
    Wei score is 99
    */
}

9.set<string> country 定义一个存储的字符串set,set是无序且不重复的

int main()
{
   set<string> cnt ;
   cnt.insert("Ch");
   cnt.insert("As");
   cnt.insert("Fr");

    set<string>::iterator it ;
    
    for(it = cnt.begin();it!=cnt.end();++it){
        cout << *it << " ";
    }
    
    cout << endl;
    
    cnt.erase("As");
    if(cnt.count("Fr")){
        cout << " France is country. " << endl;
    }
    
    cnt.clear();
    
    
    /*
    As Ch Fr 
    France is country. 
    */
}

10.多组输入问题

int main()
{
    int a, b;
    while(cin >> a >> b ){
        cout << a + b << endl;
    }
    
    while(scanf("%d%d",&a,&b) !=EOF){
        printf("%d\n",a+b);
    }
}
  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值