zoukankan      html  css  js  c++  java
  • C,C++语法基础 | 函数 | 06

    函数 | 06

    函数部分的基础知识大部分都已经掌握,主要以习题为主.

    n的阶乘

    #include<iostream>
    using namespace std;
    
    
    int fact(int n){
        int res = 1;
        for(int i=1;i<=n;i++)res *= i;
        return res;
    }
    
    int main(){
        int n;
        cin >> n;
        cout << fact(n) << endl;
        
        return 0;
    }
    

    x和y的最大值

    #include<iostream>
    using namespace std;
    
    int max(int x,int y){
        return x > y ? x : y;
    }
    
    int main(){
        int x,y;
        cin >> x >> y;
        cout << max(x,y) << endl;
        
        return 0;
    }
    
    
    

    最大公约数

    #include<iostream>
    using namespace std;
    
    int gcd(int a,int b){
        if(b==0){
            return a;
        }
        return gcd(b,a%b);
    }
    
    
    int main(){
        int a,b;
        cin >> a >> b;
        cout << gcd(a,b) << endl;
        return 0;
    }
    

    交换数值

    #include<iostream>
    using namespace std;
    
    void swap(int &a,int &b){ // 这里使用引用&是因为在函数的内外都需要变化
        if(a==b)return;
        int t = a;
        a=b,b=t;
    }
    
    
    int main(){
        int a,b;
        cin >> a >> b;
        swap(a,b);
        cout << a << " " << b << endl;
        
        return 0;
    }
    

    打印数字

    #include<iostream>
    using namespace std;
    
    
    void print(int a[],int size){
        for(int i=0;i<size;i++)cout << a[i] << " ";
    }
    
    int a[1010];
    int main(){
        int size,n;
        cin >> n >> size;
        for(int i=0;i<n;i++)scanf("%d",&a[i]);
        print(a,size);
        return 0;
    }
    

    打印矩阵

    #include<iostream>
    using namespace std;
    
    const int N = 110;
    void print2D(int a[][N],int row,int col){
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                cout << a[i][j] << " " ;
            }
            cout << endl;
        }
    }
    
    int a[N][N];
    int main(){
        int r,c;
        cin >> r >> c;
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                cin >> a[i][j];
            }
        }
        print2D(a,r,c);
        
        return 0;
    }
    

    递归求阶乘

    #include<iostream>
    using namespace std;
    
    
    int fact(int n){
        if(n==1)return 1;
        return n*fact(n-1);
    }
    
    int main(){
        int n;
        cin >> n;
        cout << fact(n) << endl;
        
        return 0;
    }
    

    递归求斐波那契数列

    #include<iostream>
    using namespace std;
    
    int fib(int n){
        if(n==1 || n == 2) return 1;
        return fib(n-1) + fib(n-2);
        
    }
    
    
    int main(){
        int n;
        cin >> n;
        cout << fib(n) << endl;
        
        return 0;
    }
    

    绝对值

    #include<iostream>
    using namespace std;
    
    int abs(int x){
        if(x>0)return x;
        return -x;
    }
    
    
    int main(){
        int x;
        cin >> x;
        cout << abs(x) << endl;
    }
    

    两个数的和

    #include<iostream>
    using namespace std;
    
    double add(double a,double b){
        return a + b;
    }
    
    int main(){
        double a,b;
        cin >> a >> b;
        printf("%.2lf",add(a,b));
        
        return 0;
    }
    

    区间求和

    #include<iostream>
    using namespace std;
    
    
    int sum(int l,int r){
        int res = 0;
        for(int i=l;i<=r;i++)res +=i;
        return res;
    }
    
    
    int main(){
        int l,r;
        cin >> l >> r;
        cout << sum(l,r) << endl;
        
        return 0;
    }
    

    最小公倍数

    #include<iostream>
    using namespace std;
    
    int gcd(int a,int b){
        if(b==0)return a;
        return gcd(b,a%b);
    }
    
    int lcm(int a,int b){
        return a*b / gcd(a,b);
    }
    
    int main(){
        int a,b;
        cin >> a >> b;
        cout << lcm(a,b) << endl;
        
        return 0;
    }
    
    

    复制数组

    #include<iostream>
    using namespace std;
    
    const int N = 110;
    int a[N],b[N];
    
    void copy(int a[], int b[], int size){
        for(int i=0;i<size;i++){
            b[i] = a[i];
        }
    }
    
    
    int main(){
        int n,m,size;
        cin >> n >> m >> size;
        for(int i=0;i<n;i++) cin >> a[i];
        for(int i=0;i<m;i++) cin >> b[i];
        copy(a,b,size);
        for(int i=0;i<m;i++){
            cout << b[i] << " ";
        }
        return 0;
    }
    

    打印字符串

    #include<iostream>
    using namespace std;
    
    void print(char str[]){
        printf("%s",str);
    }
    
    int main(){
        char s[100];
        fgets(s,200,stdin);
        print(s);
        return 0;
    }
    

    数组翻转

    #include<iostream>
    using namespace std;
    
    int a[1010];
    
    void reverse(int a[], int size){
        int l=0,r=size-1;
        while(l < r){
            swap(a[l],a[r]);
            l++,r--;
        }
    }
    
    int main(){
        int n,size;
        cin >> n >> size;
        for(int i=0;i<n;i++)cin >> a[i];
        reverse(a,size);
        for(int i=0;i<n;i++)cout << a[i] << " ";
        
        return 0;
    }
    

    数组去重

    #include<iostream>
    using namespace std;
    
    const int N = 1010;
    
    
    int unique(int a[], int size){
        int cnt = 0;
        for(int i=0;i<size;i++){
            bool is_exist = false;
            for(int j=i+1;j<size;j++){
                if(a[i] == a[j]){
                    is_exist = true;
                    break;
                }
            }
            if(!is_exist)cnt++;
        }
    
        return cnt;
    }
    
    
    int main(){
        int a[N],n,size;
        cin >> n >> size;
        for(int i=0;i<size;i++)cin >> a[i];
        cout << unique(a,size) + (n - size) << endl;
        
        return 0;
    }
    
    

    关于去重,如果不借助容器,那么就是两重循环. 如果借助容器,就只需要一重循环.

    // 去重是往数组前面看或者往数组后面看其实都可以
        for(int i=0;i<size;i++){ 
            bool is_exist = false;
            for(int j=i+1;j<size;j++){ // 向后面看
                
        for(int i=0;i<size;i++){
            bool is_exist = false;
            for(int j=0;j<i;j++){ // 向前面看            
    

    数组排序

    #include<iostream>
    using namespace std;
    
    
    const int N = 1010;
    
    void sort(int a[], int l,int r){ // 这里数组直接传递的就是地址
        // 插入排序
        for(int i=l+1;i<=r;i++){
            int j = i;
            while(j>l && a[j] < a[j-1])swap(a[j],a[j-1]),j--;
        }
    }
    
    
    int main(){
        int n,l,r,a[N];    
        cin >> n >> l >> r;
        for(int i=0;i<n;i++)cin >> a[i];
        sort(a,l,r);
        for(int i=0;i<n;i++)cout << a[i] << " ";
        
        return 0;    
    }
    

    C++方法中传递的数组直接就是传递的数组地址,这个注意了,不需要引用&,只有变量才需要引用&

    跳台阶

    #include<iostream>
    using namespace std;
    
    int f(int n){
        if(n<0)return 0;
        if(n==0)return 1;
        return f(n-1) + f(n-2);
    }
    
    int main(){
        int n;
        cin >> n;
        cout << f(n) << endl;
        
        
    }
    

    这是一道非常经典的递归题目,就是用来锻炼递归能力的. 然后这个跳台阶题目的本质就是斐波那契数列.

    首先是有几种相似的选择,然后考虑边界.

    走方格

    #include<iostream>
    using namespace std;
    
    
    int f(int n,int m){
        if(n<0 || m<0)return 0;
        if(n==0 && m == 0)return 1;
        return f(n-1,m) + f(n,m-1);
    }
    
    int main(){
        int n,m;
        cin >> n >> m;
        cout << f(n,m) << endl;
        
        return 0;
    }
    

    走方格,也是一个非常经典的练手递归的题目.

    如果不清楚还是先画出一个递归搜索树出来.可是是正序也可以是反序,结果都是一样的.

    排列

    排列

    #include<bits/stdc++.h>
    using namespace std;
    
    
    // 这种dfs都设置全局变量和全局数组反而更加方便
    int n;
    int a[20]; // 待选择的数字最好用一个数组存起来
    bool vis[20]; // 判重数组
    int res[20]; // 存储结果的数组
    
    
    void dfs(int u){
        if(u == n){
            for(int i=0;i<n;i++){
                cout << res[i] << " ";
            }
            cout << endl;
            return;
        }
        for(int i=0;i<n;i++){
            if(!vis[i]){
                vis[i] = true;
                res[u] = a[i];
                dfs(u+1);
                vis[i] = false;
                res[u] = 0;
                
            }
        }
    }
    
    
    int main(){
        cin >> n;
        for(int i=0,j=1;j<=n;i++,j++){
            a[i] = j;
        }
        dfs(0);
        
        return 0;
    }
    

    排列也是很典型的问题,必会问题.

  • 相关阅读:
    Trie字典树
    NET Web API和Web API Client Gen使Angular 2应用程序
    MyBatis实体属性与表的字段不对应的解决方案
    携程Apollo(阿波罗)配置中心使用Google代码风格文件(在Eclipse使用Google代码风格)(配合阿里巴巴代码规约快速设置)
    Spring Boot实现多个数据源教程收集(待实践)
    携程Apollo(阿波罗)配置中心用户管理和部门管理
    Spring Boot多数据源连接8小时后断开的问题解决(MySQL)
    携程Apollo(阿波罗)配置中心本地开发模式不接入配置中心进行本地开发
    携程Apollo(阿波罗)配置中心把现有项目的配置文件迁移到Apollo
    携程Apollo(阿波罗)配置中心在Spring Boot项目快速集成
  • 原文地址:https://www.cnblogs.com/Rowry/p/13924604.html
Copyright © 2011-2022 走看看