zoukankan      html  css  js  c++  java
  • C++学习(二)

    1.插入排序

    #include <iostream>
    using namespace std;
    
    int main()
    {
        //N数字<=10000,元素个数
        //第二行各个元素值
        //第三个表示插入的值
        int *a,N;
        cin >>N;
        a = new int[N];
        for(int i = 0;i < N;i++)
            cin >>a[i];
    
        //do something
        int insertValue;
        cin >> insertValue;
        if(a[0] > insertValue)
            cout <<insertValue<<" ";
        for(int i = 0;i < N;i++)
        {
            cout <<a[i]<<" ";
            if(a[i] < insertValue && a[i + 1] > insertValue)
                cout <<insertValue<<" ";
    
        }
    
        if(a[N-1] < insertValue)
            cout <<insertValue<<" ";
        cout <<endl;
        system("pause");
        return 0;
    }
    View Code

    2.统计字符

    #include <iostream>
    #include <ctype.h>
    using namespace std;
    
    int main()
    {
        char c;
        int alpha = 0,number = 0,space = 0,other = 0;
        while((c = getchar()) != '
    ')
        {
            if(isalpha(c))
                alpha++;
            else if(isdigit(c))
                number++;
            else if(c == 32)
                space++;
            else
                other++;
        }
    
        cout <<"letter="<<alpha<<endl;
        cout <<"space="<<space<<endl;
        cout <<"digit="<<number<<endl;
        cout <<"other="<<other<<endl;
        system("pause");
        return 0;
    }
    View Code

    3.稀疏数组,数组的第一部分用于记录原数组的列数和行数及元素的使用个数,第二部分为原数组非零元素的行数、列数、元素值

    #include <iostream>
    using namespace std;
    
    int main()
    {
        //set n*n array
        int row,col,s = 0;
        cin >>row>>col;
        int **a = new int*[row];
        for(int i = 0;i < row;i++)
            a[i] = new int[col];
    
        //set value
        for(int i = 0;i < row;i++)
        {
            for(int j = 0;j < col;j++)
            {
                cin >> a[i][j];
                if(a[i][j] != 0)
                    s++;
            }
    
        }
        cout <<row<<" "<<col<<" "<<s<<endl;
        //print
        for(int i = 0;i < row;i++)
        {
            for(int j = 0;j < col;j++)
            {
                if(a[i][j] != 0)
                    cout <<i<<" "<<j<<" "<<a[i][j]<<endl;
            }
    
        }
        system("pause");
        return 0;
    }
    View Code

    4.c++标识符和java标识符的转换

    #include <iostream>
    #include <string.h>
    #include <ctype.h>
    using namespace std;
    
    int main()
    {
        char s1[100],s2[100];
        char c;
        int r,t = 0;
        bool upper = false,line = false,firstA = false,isLineRepeat = false;
        while((c = getchar()) != '
    ')
        {
            if(isupper(c))
                upper = true;
            if(c == 95)
                line = true;
            if(islower(c) && t == 0)
                firstA = true;
            if(t > 0 && c == 95 && s1[t - 1] == 95)
                isLineRepeat = true;
            s1[t++] = c;
        }
        int s1_len = strlen(s1);
        if(firstA== true && line == false && upper == true)
            r = 1;
        else if(firstA == true && isLineRepeat == false && upper == false)
            r = 2;
        else
        {
            cout <<"error!"<<endl;
            system("pause");
            return 0;
        }
    
        if(r == 1)
        {
            cout <<"java to c++:"<<endl;
            int j = 0;
            for(int i = 0;s1[i] != '';i++,j++)
            {
                if(islower(s1[i]))
                    s2[j] = s1[i];
                else
                {
                    s2[j++] = '_';
                    s2[j] = s1[i];
                }
            }
            s2[j] = '';
        }
        else if(r == 2)
        {
            cout <<"c++ to java:"<<endl;
            int j = 0;
            for(int i = 0;s1[i] != '';i++)
            {
                if(islower(s1[i]) )
                    s2[j++] = s1[i];
                else
                    s2[j++] = s1[++i] - 32;
            }
            s2[j] = '';
        }
        //print
        for(int i = 0;s2[i] != '';i++)
            cout <<s2[i];
        cout <<endl;
        system("pause");
        return 0;
    }
    View Code

    5.5位数(找出前两位数、后两位数、中间数能被6整除)

    ①穷举法

    #include <iostream>
    using namespace std;
    int main()
    {
        int coutLine = 0;
        for(int i = 10000;i <= 99999;i++)
        {
    
            int a = i/1000;
            int b = i/100%10;
            int c = i - a*1000 - b*100;
            if(a%6==0 && b%6==0 && c%6==0)
            {
                cout <<i<<" ";
                coutLine++;
                if(coutLine%10==0)
                    cout <<endl;
            }
        }
        cout <<coutLine;
        system("pause");
        return 0;
    }
    View Code

    ②找规律

    #include <iostream>
    using namespace std;
    int main()
    {
        int coutLine = 0;
        for(int a = 12;a <= 96;a+=6)
            for(int b = 0;b <= 6;b+=6)
                for(int c = 0;c <= 96;c+=6)
                {
                    if(coutLine % 10 == 0)
                        cout<<endl;
                    if(c < 10)
                        cout<<a<<b<<"0"<<c<<" ";
                    else
                        cout <<a<<b<<c<<" ";
                    coutLine++;
                }
        cout <<endl<<coutLine;
        system("pause");
        return 0;
    }
    View Code

    6.计算n!的末尾有几个零

    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        cin >>n;
        int i = n,t = 0;
        while(i >= 5)
        {
            i = i/ 5;
            t = t+i;
        }
        cout <<t<<endl;
        system("pause");
        return 0;
    }
    View Code

    7.最大子序列和

    ①方法一

    #include <iostream>
    using namespace std;
    #define N 35000
    int a[N + 1];
    int main()
    {
        int n;
        cin >>n;
        for(int i = 0;i < n;i++)
            cin >>a[i];
    
        int maxValue = 0;
        for(int i = 0;i < n;i++)
        {
            int sum = 0;
            for(int j = 0;j < n;j++)
            {
                sum += a[j];
                if(sum > maxValue)
                    maxValue = sum;
            }
        }
        cout <<maxValue<<endl;
        return 0;
    }
    View Code

    ②方法二

    #include <iostream>
    using namespace std;
    #define N 35000
    int a[N + 1];
    int main()
    {
        //联机算法
        int n;
        cin >>n;
        for(int i = 0;i < n;i++)
             cin >>a[i];
        int nowSum = 0;
        int maxSum = 0;
        for(int i = 0;i < n;i++)
        {
            nowSum += a[i];
            if(nowSum > maxSum)
                maxSum = nowSum;
            else if(nowSum < 0)
                nowSum = 0;
        }
        cout <<maxSum<<endl;
        return 0;
    }
    View Code

    8.抢椅子算楚谁最后第一

    ①方法一 时间复杂难度略高

    #include <iostream>
    #define N 100
    using namespace std;
    int a[N + 1],c = 0;
    int main()
    {
        int m,n;
        cin >>m>>n;
        for(int i =1;i<=m;i++)
            a[i] = 1;
        int countNumber = 0;
        int k = 1;
        while(countNumber < m -1)
        {
            for(int i = 1;i <= m;i++)
            {
                if(a[i] == 1)
                {
                    if(k % n == 0)
                    {
                        k = 0;
                        a[i] = 0;
                        //cout <<i<<" 0"<<endl;
                        countNumber++;
                        //cout <<countNumber<<endl;
                    }
                    k++;
                }
            }
        }
        for(int i = 1;i <=m;i++)
        {
            if(a[i] == 1)
                cout <<i<<endl;
        }
        system("pause");
        return 0;
    }
    View Code

    ②方法二

    #include <iostream>
    #define N 100
    using namespace std;
    int a[N + 1];
    int main()
    {
        int m,n;
        cin >>m>>n;
        for(int i = 1;i <= m -1;i++)
            a[i] = i+1;
        a[m] = 1;
        int countNumber = 0;
        int k = m;
        for(int i = 1;i <= m;i++)
        {
            for(int countNumber = 0;countNumber < n - 1;countNumber++)
                k = a[k];
            a[k] = a[a[k]];     //指向后一个值
    
        }
        cout <<k<<endl;
        system("pause");
        return 0;
    }
    View Code

    ③方法三 按规律得出公式

    #include <iostream>
    using namespace std;
    int main()
    {
        int m,n,ans = 0;
        cin >>m>>n;
        for(int i= 2;i<=m;i++)
            ans = (ans + n)%i;
        cout <<ans + 1;
        system("pause");
        return 0;
    }
    View Code

    9.shlqsh数

    #include <iostream>
    using namespace std;
    int main()
    {
        int t1,t2;
        cin >>t1>>t2;
        int ans1 = 0,ans2 = 0;
        for(int i = 1;i < t1;i++)
            ans1 += (t1 - 1)/i;
        for(int j = 1;j <= t2;j++)
            ans2 += t2/j;
        cout <<ans2 - ans1<<endl;
        system("pause");
        return 0;
    }
    View Code

    10.判断素数

    #include <iostream>
    #include <math.h>
    using namespace std;
    void isPrime(int x)
    {
        int t = sqrt(x);
        for(int i = 2;i <= t;i++)
        {
            if(!(x % i))
            {
                cout <<x<<"不是素数!"<<endl;
                return;
            }
        }
        cout <<x<<"是素数!"<<endl;
    }
    
    int main()
    {
        int a;
        cin >>a;
        if(a > 2)
            isPrime(a);
        else
            cout <<"error!"<<endl;
        system("pause");
        return 0;
    }
    View Code

    11.哥德巴赫猜想(验证两千以内的正偶数能够分解为两个素数的和)

    include <iostream>
    #include <math.h>
    using namespace std;
    int isPrime(int x)
    {
        if(x == 2)
            return 1;
        if(!(x % 2))
            return 0;
        int t = sqrt(x);
        for(int i = 3;i <= t;i++)
        {
            if(!(x % i))
                return 0;
        }
        return 1;
    }
    
    int main()
    {
        for(int i = 4;i <= 2000;i+=2)
        {
            for(int j = 2;j < i;j++)
            {
                if(isPrime(j))  //a是素数
                {
                    if(isPrime(i - j))  //a-j是素数
                    {
                        cout <<j<<"+"<<i-j<<"="<<i<<endl;
                        break;
                    }
                }
            }
        }
        system("pause");
        return 0;
    }
    View Code

    12.逆序数

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int reverseNum(int x)
    {
        double t = 0;
        if(x >= 10)
        {
            cout <<x%10;
            reverseNum(x/10);
        }
        else
            cout <<x<<endl;
    }
    int main()
    {
        int a;
        cin >>a;
        reverseNum(a);
        system("pause");
        return 0;
    }
    View Code

    13.数字转字符串

    #include <iostream>
    using namespace std;
    void intToString(int x)
    {
        int t;
        if((t = x/10)!= 0)
            intToString(x/10);
        putchar(x%10 + 48);
    
    }
    int main()
    {
        int a;
        cin >>a;
        if(a < 0)
        {
            putchar('-');
            a = -a;
        }
        intToString(a);
        system("pause");
        return 0;
    }
    View Code

    14.递归打印等边三角形

    #include <iostream>
    using namespace std;
    void trangle(char c,int n)
    {
        if(n > 0)
        {
            cout <<c;
            trangle(c,n-1);
        }
    }
    
    int main()
    {
        int n;
        cin >>n;
        for(int i = 1;i<=n;i++)
        {
            trangle(' ',n - i);
            trangle('*',2*i-1);
            cout<<endl;
        }
        system("pause");
        return 0;
    }
    View Code
  • 相关阅读:
    连续型随机变量
    离散型随机变量
    vue1.0生命周期
    vue2.0生命周期函数
    vue2.0 vue.set()
    vue2.0 vue.extend()的拓展
    vue2.0 自定义指令详解
    vue2.0 v-model指令
    vue2.0排序应该注意的问题
    vue2.0版本指令v-if与v-show的区别
  • 原文地址:https://www.cnblogs.com/Fantac/p/13237381.html
Copyright © 2011-2022 走看看