栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

学习过程整理的一些函数以及题解

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学习过程整理的一些函数以及题解

学习了一年的c++,平日学习生活中也浅浅地整理了一些函数,题解什么的。

想了想分享一下也挺好,万一就能帮助到在某个点处于迷茫的初学者捏,欸嘿~

#include//万能头文件
sqrt()//求根
pow( , )//求n次方
abs()//求绝对值
sort(a,a+n)//数组排序,从低到高
sort(a,a+n,greater())//数组排序,从高到低 
strcpy(str1,str2)//str2赋给str1 
strcpy(str1,"world")//"world"赋给str1
strcmp(s1,s2)//比较s1和s2的大小(按ASCII码排序)str1=str2时,返回零;str1str2时,返回正数
printf("%2d",a) //运用于格式化输出类似于00:00:00;
struct student students[n]//结构体可以去定义数组 
ceil(x);//取向上整函数,ceil(2.5)=3;
floor(x);//向下取整函数,floor(2.5)=2;
system("cls");//清除屏幕,清除不必要的内存占用;
cout< cout< #define N 20050//不占用内存,宏定义N=20050 ,不需要分号
const int N = 2000000;//将N转化为数字
char input[20050], str[105][105];//可以用二维数组来表示每一个字符串长度,前面是第几个字符串,后面是每个字符串所包含的字符
setfillcolor(GREEN);//填充颜色 
fillcircle(100, 100, 50);//画实心圆(坐标(100,100)半径50)
circle(50, 50, 50); //画空心圆(坐标(50,50)半径50) 
loadimage(&目标图片的地址,"./images/图片的名称")//导入图片 
putimage(x坐标,y坐标,&取图片的地址);//导入图片
(int*)malloc(sizeof());//malloc函数动态分配内存,格式(指针类型)malloc(数据数量)
stackst;//建一个int类型栈
st.push(x);//将x输入栈中
cout< st.pop()//将最顶端的数弹出栈
st.empty()//判断是否为空
typedef int a;//此时a的作用就是int int x可以换成a x;
//加大cin、cout输入输出效率
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//加大cin、cout输入输出效率https://www.cnblogs.com/PrayG/p/5749832.html(博客原文)
vector A//定义一个整数型动态数组
push_back() //在Vector最后添加一个元素

#include
using namespace std;
const int N = 1e5 + 9;
int n;    
int a[N], temp[N];
long long ans;
while (cin.getline(a,256));//acm上的输入字符串(名字,长度)
while (gets_s(a));//vs上的输入字符串
while (gets(a));//devc++上的输入字符串
//算法:归并排序
void mergesort(int l,int r) {
    if (l >= r) return;//如果前面的点位超过了后面的点位,退出
    int mid = (l + r) / 2;//取中间指
    mergesort(l, mid);//前面一半
    mergesort(mid + 1, r);//后面一半
    int i = l, j = mid + 1, cnt = 0;
    while (i <= mid && j <= r) {
        if (a[i] > a[j]) {
            temp[cnt++] = a[j++];
        }//前面的大于后面的,新数组的cnt位就是后面的那个数
        else {
            temp[cnt++] = a[i++];
        }//否则就是前面的那个数
    }
    while (i <= mid) {
        temp[cnt++] = a[i++];
    }//填入前半边剩下的数字
    while (j <= r) {
        temp[cnt++] = a[j++];
    }//填入后半边剩下的数字
    for (int i = l, j = 0; i <= r;) a[i++] = temp[j++];//将排好序的数组传给a
}
//归并排序

//二分法找数字
#include
using namespace std;
const int N = 1000000;
int a[N], n, q, mid, x, i;
int sort1(int x1) {
    int l = 0, r = n - 1;
    while (l < r) {
        mid = l + r >> 1;//二进制右移一位(等于(l+r)/2)
        if (a[mid] >= x1) r = mid;//如果l和r的中间位置的a[mid]大于等于要找的数字,把r往前提
        else l = mid + 1;//如果小的话就把l往后移动一位
    }
    if (a[l] != x1) return -1;
    else return l;
}//查找数字的开始位置
int sort2(int x2) {
    int l = 0, r = n - 1;
    while (l < r) {
        mid = l + r + 1 >> 1;
        if (a[mid] <= x2) l = mid;//如果当前位置小于等于要找的数字,就把l往后移动
        else r = mid - 1;//如果大于就把r往前移动
    }
    if (a[l] != x2) return -1;
    else return l;
}//查找数字的结束位置
int main() {
    cin >> n >> q;
    for (i = 0; i < n; i++) cin >> a[i];//输入原数组
    for (i = 0; i < q; i++) {
        cin >> x;//输入要查找的数字(默认从小到大排)
        cout << sort1(x) << " " << sort2(x) << endl;
    }
}
//二分法找数字的开始和结束位置。

//二分法求三次方根
#include
using namespace std;
int main() {
    double n;
    cin >> n;
    double l = -10000, r = 10000, mid;
    while (r - l > 1e-8) {
        mid = (l + r) * 1.0 / 2;
        if (mid * mid * mid >= n) {
            r = mid;
        }
        else l = mid;
    }
    cout.precision(6);
    cout << fixed << l;
    return 0;
}
//二分法求三次方根
 
//用链表删除元素
#include
using namespace std;
typedef struct Node {
    int num;
    struct Node* next;
}LNode, * linklist;
int main() {
    int m, x;
    linklist head, r, p;
    cin >> m;
    head = new Node();
    head->next = NULL;//创建头节点
    r = head;//让head里面有r
    for (int i = 1; i <= 10; i++) {
        cin >> x;//输入x
        Node* tmp = new Node();//给新的tmp节点申请分配空间
        tmp->num = x;//tmp的内容是x
        tmp->next = NULL;//它的下一个地址为空(尾插法)
        r->next = tmp;//r的下一个指向是tmp
        r = tmp;//把tmp赋给r
    }
    p = head->next;
    int k = 1;
    while (k < m - 1) {
        p = p->next;//遍历到m-1的前面一个
        k++;
    }
    Node* q;
    q = p->next;
    p->next = q->next;
    free(q);
    p = head->next;
    while (p != NULL) {
        cout << p->num << " ";
        p = p->next;
    }
    return 0;
}
//用链表的删除

//用链表插入数字
#include
using namespace std;
typedef struct Node {
    int num;
    struct Node* next;
}Lnode, * linklist;
const int N = 10000;
int main() {
    int n, m, a[N];
    cin >> n;
    linklist head, r, p, q;
    head = new Node();
    head->next = NULL;
    r = head;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        Node* tmp = new Node();
        tmp->num = a[i];
        tmp->next = NULL;
        r->next = tmp;
        r = tmp;
    }
    cin >> m;
    Node* s = new Node();
    s->next = NULL;
    s->num = m;
    p = head->next;
    q = head;
    while (p != NULL) {
        if (p->num > m) {
            break;
        }
        q = p;
        p = p->next;
    }
    s->next = p;
    q->next = s;
    p = head->next;//初始化p的位置
    while (p != NULL) {
        cout << p->num << " ";
        p = p->next;
    }
    return 0;
}
//用链表插入

//约瑟夫问题(跳过数组某个数据)
#include
using namespace std;
int main() {
    int n, m, a[25];
    cin >> n >> m;
    for (int i = 1; i <= n; i++) a[i] = i;
    for (int i = 1, j = 1, r = 1; r <= n; i = i % n + 1, j = j % n + 1) {//i是第几个人,p是计数器,r是退出条件
        while (a[i] == -1) {
            i = i % n + 1;
        }//如果已经被去掉,就往后走一个
        if (j == m) {
            cout << a[i] << " ";
            a[i] = -1;//去掉a[i]
            j = 0;
            r++;
        }
    }
    return 0;
}
//约瑟夫问题(跳过数组某个数据)

//链表逆转
#include
using namespace std;
typedef struct Node {
    int num;
    struct Node* next;
}lnode, * linklist;
int main() {
    int x;
    linklist head, r;
    head = new lnode();
    head->next = NULL;
    r = head;
    while (cin >> x) {
        if (x == -1) break;
        linklist tmp = new lnode();
        tmp->num = x;
        tmp->next = NULL;
        r->next = tmp;
        r = tmp;
    }
    linklist o, p;
    linklist q;
    q = new lnode();//创建新链表的头节点
    o = head;//o为旧链表的头节点
    q->next = NULL;//q的下一个指向空(避免野指针的出现)
    while (o) {
        p = o->next;//p为o下一个指向,避免链断了导致后面的数据丢失,同时更新p点
        o->next = q->next;//插入数据
        q->next = o;
        o = p;//更新o点
    }
    linklist h;
    h = q->next;//用新链表来输出
    while (h->next) {
        cout << h->num << " ";
        h = h->next;
    }
    cout << endl;
    return 0;
}
//链表逆转

//顺序表插入数据
#include
using  namespace std;
#define MAXSIZE 1005
struct List {
    int data[MAXSIZE];
    int last;
};
List* makeEmpty() {
    List* ptra;
    ptra = (List*)malloc(sizeof(List));
    ptra->last = 0;
    return ptra;
};
void insertList(int x, int i, List* ptr) {
    for (int j = ptr->last - 1; j >= i - 1; j--) {
        ptr->data[j + 1] = ptr->data[j];
    }
    ptr->data[i - 1] = x;
    ptr->last++;
    return;
}
void disPlayList(List* ptr) {
    for (int i = 0; i <= ptr->last - 1; i++)
        cout << ptr->data[i] << " ";
}
int main() {
    List* a = makeEmpty();
    int n, m, i, x;
    cin >> n >> m;
    for (i = 0; i < n; i++) {
        cin >> x;
        a->data[i] = x;
        a->last++;
    }
    insertList(5, m, a);
    disPlayList(a);
    return 0;
}
//顺序表插入数据 

//矩阵
#include
using namespace std;
int main() {
    int ra, ca, rb, cb;
    cin >> ra >> ca;
    int i, j, k = 0, h, d = 0;
    int a[101][101], b[101][101], c[101][101];
    for (i = 0; i < ra; i++)
        for (j = 0; j < ca; j++) cin >> a[i][j];
    cin >> rb >> cb;
    for (i = 0; i < rb; i++)
        for (j = 0; j < cb; j++) cin >> b[i][j];
    if (ca != rb) cout << "Error: " << ca << " != " << rb;
    else {
        for (i = 0; i < ra; i++) {
            for (j = 0; j < cb; j++) {
                d = 0;
                for (h = 0; h < ca; h++) {
                    d += a[i][h] * b[h][j];
                }
                c[i][j] = d;
            }
        }
        cout << ra << " " << cb << endl;
        for (i = 0; i < ra - 1; i++) {
            for (j = 0; j < cb - 1; j++) cout << c[i][j] << " ";
            cout << c[i][cb - 1];
            cout << endl;
        }
        for (j = 0; j < cb - 1; j++) cout << c[ra - 1][j] << " ";
        cout << c[ra - 1][cb - 1];
    }
    return 0;
}
//矩阵

//栈
#include
using namespace std;
const int maxsize = 100;
struct stk {
    int st[maxsize];
    int top;
};
void push(stk* stak, int x) {
    stak->st[++stak->top] = x;
}
void init(stk* stak) {
    stak->top = 0;
}
void pop(stk* st) {
    st->top--;
}
int gettop(stk* st) {
    int x;
    x = st->st[st->top];
    return x;
}
bool isempty(stk* st) {
    return st->top == 0;
}
int main() {
    int m, x;
    string str;
    stk ss;
    init(&ss);
    cin >> m;
    while (m--) {
        cin >> str;
        if (str == "push") {
            cin >> x;
            push(&ss, x);
        }
        else if (str == "pop") {
            pop(&ss);
        }
        else if (str == "empty") {
            if (isempty(&ss)) {
                cout << "YES" << endl;
            }
            else cout << "NO" << endl;
        }
        else if (str == "query") {
            cout << gettop(&ss) << endl;
        }
    }
    return 0;
}
//栈
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/767260.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号