• C++ 计算机程序设计(西安交大mooc)


      第一周 基础练习

      1、显示Hello World!

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "Hello World!" << endl;
        return 0;
    }
    View Code

      2、显示唐诗

    #include <iostream>
    using namespace std;
    int main()
    {
        cout<<  "慈母手中线
    "
                "游子身上衣
    "
                "临行密密缝
    "
                "意恐迟迟归
    "
                "谁言寸草心
    "
                "报得三春晖
    ";
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        cout<< "慈母手中线
    
    游子身上衣
    
    临行密密缝
    
    意恐迟迟归
    
    谁言寸草心
    
    报得三春晖" << endl;    
        return 0;
    }
    View Code

      3、显示一句话

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string name;
        cin >> name;
        cout << "This program is coded by " + name + "." << endl;
        return 0;
    }
    View Code

      4、还是一句话,getline(cin,name); 是string类重载的友元函数;cin.getline()是istream类的成员函数

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string name;
        getline(cin,name);
        cout << "This program is coded by " + name + "." << endl;
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        char name[20];
        /* 从键盘输入字符时,字符一直在键盘缓冲区中(shell缓冲区),
        当在键盘输入'
    '时,键盘缓冲区中的字符才被全部传送到stdin缓冲区中 */
        cin >> name;
        cout << name << endl;
        
        cin >> name;  /* 忽略空白符 缓冲区还剩余一个回车*/
        cout << name << endl;
        
        cin.sync();   /* 清空缓冲区 */
        
        /* 可以读取空白符'
    ' 以回车作为结束标记并且读取回车符 缓冲区没有剩余*/
        cin.getline(name, 15); 
        cout << name << endl;
        
        cout << getchar() << endl;  /* 缓冲区是空的 等待输入 */
        return 0;
    }
    View Code

      5、计算矩形周长

    #include <iostream>
    using namespace std;
    int main()
    {
        int a, b;
        cin >> a >> b;
        cout << 2 * ( a + b ) << endl;
        return 0;
    }
    View Code

      6、已知直角边求斜边

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double a, b, c;
        cin >> a >> b;
        cout << sqrt ( a*a + b*b ) << endl;
        return 0;
    }
    View Code

      第一周 中级练习

      1、计算公式的值(对数),以m为底的n的对数 log(n) / log(m)

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double x, a;
        cin >> x >> a;
        cout << log(x+sqrt(x*x+1)) / log(a) << endl;
        return 0;
    } 
    View Code

      2、e的近似值

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double n;
        cin >> n;
        cout << pow(1+1/n, n) << endl;
        return 0;
    } 
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        int n, t;
        double y = 1.0;
        cin >> n;
        
        t = n;
        while(t--)
        {
            y *= (1+1.0/n);
        }
        
        cout << y << endl;
        return 0;
    } 
    View Code

      3、计算公式的值(三角等)

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double x;
        cin >> x;
        cout << sin(x) - log(x) + sqrt(x) - 5 << endl;
        return 0;
    } 
    View Code

      4、计算公式的值(开方)

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double x;
        cin >> x;
        cout << x / sqrt(x*x-3*x+2) << endl;
        return 0;
    } 
    View Code

      第一周 编程作业

      1、1-1我爱C++

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "Hello C++." << endl;
        cout << "I like programming." << endl;
        return 0;
    }
    View Code

      2、1-2来自系统的问候,注意字符数组的长度

    #include <iostream>
    using namespace std;
    int main()
    {
        char name[256];
        cin.getline( name, 35 );
        cout << "Hello " << name << "." << endl;
        return 0;
    }
    View Code

      3、1-3乘法计算器

    #include <iostream>
    using namespace std;
    int main()
    {
        double a, b;
        cin >> a >> b;
        cout << a * b << endl;
        return 0;
    }
    View Code

      4、1-4单位换算

    #include <iostream>
    using namespace std;
    int main()
    {
        double inch;
        const double inch_to_cm = 2.54;
        cin >> inch;
        cout << inch << "inch=" << inch * inch_to_cm << "cm" << endl;
        return 0;
    }
    View Code

      5、1-5平方根计算器

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double x;
        cin >> x;
        cout << sqrt(x) << endl;
        return 0;
    }
    View Code

      第二周 基础练习

      1、求过平面上两点的直线的斜率,斜率 ( y1 - y2 ) / ( x1 - x2 ) 

    #include <iostream>
    using namespace std;
    int main()
    {
        double x1, y1 , x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        cout << ( y1 - y2 ) / ( x1 - x2 ) << endl;
        return 0;
    } 
    View Code

      2、计算平面上两点之间的距离

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double x1, y1 , x2, y2, distance;
        cin >> x1 >> y1 >> x2 >> y2;
        distance = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
        cout << distance << endl;
        return 0;
    } 
    View Code

      3、判断大小写

    #include <iostream>
    using namespace std;
    int main()
    {
        char ch;
        cin >> ch;
        if(ch >= 'A' && ch <= 'Z')
            cout << 1 << endl;
        else
            cout << 0 << endl;
        return 0;
    } 
    View Code

      4、判断数字

    #include <iostream>
    using namespace std;
    int main()
    {
        char ch;
        cin >> ch;
        if(ch >= '0' && ch <= '9')
            cout << 1 << endl;
        else
            cout << 0 << endl;
        return 0;
    } 
    View Code

      5、判断闰年

    #include <iostream>
    using namespace std;
    int main()
    {
        int year;
        cin >> year;
        if(year%4==0&&year%100!=0||year%400==0)
            cout << "IsLeapYear" << endl;
        else
            cout << "NotLeapYear" << endl;
        return 0;
    } 
    View Code

      6、求商和余数

    #include <iostream>
    using namespace std;
    int main()
    {
        int a, b;
        cin >> a >> b;
        cout << a/b << " " << a%b << endl;
        return 0;
    } 
    View Code

      7、计算平均分取整

    #include <iostream>
    using namespace std;
    int main()
    {
        int x, y, sum = 0, n = 7;
        
        while(n--)
        {
            cin >> x;
            sum += x;
        }
        
        y = int( sum/7.0 + 0.5 );
        cout << y << endl;
        return 0;
    } 
    View Code

      8、计算点到直线的距离保留两位小数,int( d*100 + 0.5 ) / 100.0

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        int A, B, C, x, y;
        cin >> A >> B >> C;
        cin >> x >> y;
        
        double d = fabs(A*x+B*y+C) / sqrt(A*A+B*B);
        
        cout <<  int( d*100 + 0.5 ) / 100.0 << endl;
        return 0;
    } 
    View Code

      9、输入字符显示ASCII值,int(ch)

    #include <iostream>
    using namespace std;
    int main()
    {
        char ch;
        cin >> ch;
        cout << int(ch) << endl;
        return 0;
    } 
    View Code

      10、输入整数显示ASCII字符

    #include <iostream>
    using namespace std;
    int main()
    {
        int k;
        cin >> k;
        cout << char(k) << endl;
        return 0;
    } 
    View Code

      11、输入整数显示十六进制

    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;
        cout << hex << n << endl;
        return 0;
    } 
    View Code

      12、输入整数显示十六进制和八进制,int sign = n < 0 ? (n = -n, -1) : 1;

    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;
        
        int sign = n < 0 ? (n = -n, -1) : 1;
        
        if(sign == -1){
            cout << n*sign << " -" << hex << n << " -"<< oct << n << endl;
        }
        else{
            cout << n << " " << hex << n << " "<< oct << n << endl;
        }
        return 0;
    } 
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;    
        
        int sign = n < 0 ? (n = -n, -1) : 1;
        
        sign == -1 ? cout << n*sign << " -" << hex << n << " -"<< oct << n << endl    
            : cout << n << " " << hex << n << " "<< oct << n << endl;
        return 0;
    } 
    View Code

      第二周 中级练习

      1、加密

    #include <iostream>
    using namespace std;
    int main()
    {
        char a, b, c, d;
        cin >> a >> b >> c >> d;
        (a-'a'>10) ? cout << a-'a' : cout << '0' << a-'a';
        (b-'a'>10) ? cout << b-'a' : cout << '0' << b-'a';
        (c-'a'>10) ? cout << c-'a' : cout << '0' << c-'a';
        (d-'a'>10) ? cout << d-'a' : cout << '0' << d-'a';
        cout << endl;
        return 0;
    } 
    View Code
    #include <iostream>
    using namespace std;
    void fun(char a)
    {
        (a-'a'>10) ? cout << a-'a' : cout << '0' << a-'a';
    }
    int main()
    {
        char a, n = 4;
        while(n--)
        {
            cin >> a;
            fun(a);
        }
        cout << endl;
        return 0;
    } 
    View Code

      2、解密

    #include <iostream>
    using namespace std;
    int main()
    {
        char a[256];
        cin >> a;
        for(int i=0; i<7; i += 2){
            cout << char((a[i]-'0')*10 + a[i+1]-'0' + 'a');
        }
        cout << endl;
        return 0;
    } 
    View Code

      3、压缩存储,位移

    #include <iostream>
    using namespace std;
    int main()
    {
        int x = 0, a, b, c, d;
        cin >> a >> b >> c >> d;
        x = a << 24;
        x += b << 16;
        x += c <<8;
        x += d;
        cout << x << endl;
        return 0;
    } 
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        int a, x = 0,  n = 4;
        
        while(n--)
        {
            cin >> a;
            x += (a << n*8);
        }
       
        cout << x << endl;
        return 0;
    }
    View Code

      4、石头剪刀布

    #include <iostream>
    using namespace std;
    int main()
    {
        const char* answer[] = {"不认识", "石头", "剪刀", ""};
        int i;
        cin >> i;
        (i >= 1 && i <= 3) ? cout << answer[i] : cout << answer[0];
        cout << endl;
        return 0;
    }
    View Code

      5、排排坐分果果,格式要求,末尾无空格

    #include <iostream>
    using namespace std;
    int main()
    {
        int n = 4, a, k;
        cin >> a >> k;
        
        while(n--)
        {    
            ( n == 3 ) ? : cout << " ";
            (a = (a + k - 1)%10) ? cout << a : cout << "10";
            a = (a+1)%10;
        }
        
        cout << endl;
        return 0;
    } 
    View Code

      第二周 编程作业

      1、温度转换,小数常量默认是double类型

    #include <iostream>
    using namespace std;
    int main()
    {
        double C, F;
        cin >> F;
        C = 5.0/9*(F-32);
        cout << C << endl;
        return 0;
    }
    View Code

      2、计算数学函数式的值

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double x, y;
        cin >> x;
        y = sin(x*x)/(1-cos(x));
        cout << y << endl;
        return 0;
    }
    View Code

      3、数据的简单统计,题目要求,平均值的四舍五入整数值

    #include <iostream>
    using namespace std;
    int main()
    {
        int a, b, c;
        cin >> a >> b >> c;
        int sum = a + b + c;
        cout << sum << endl;
        cout << sum/3.0 << endl;
        int ave = sum/3.0 + 0.5;
        cout << ave << endl;
        return 0;
    }
    View Code

      4、找零钱,格式最后没有空格

    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
        cin >> x;
        
        cout << x/50 << " ";
        x %= 50;
        
        cout << x/20 << " ";
        x %= 20;
        
        cout << x/10 << " ";
        x %= 10;
    
        cout << x/5 << " ";
        x %= 5;
        
        cout << x << endl;
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    
    void fun(int x){
        int arr[] = {50,20,10,5,1};
        for(int i=0; i<4; i++){
            cout << x/arr[i] << " ";
            x %= arr[i];
        }
        cout << x << endl;
    }
    
    int main()
    {
        int x;
        cin >> x;
        fun(x);    
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
        cin >> x;
        int arr[] = {50,20,10,5,1};
        for(int i=0; i<5; i++){
            if(i) cout << " ";
            cout << x/arr[i];
            x %= arr[i];
        }
        return 0;
    }
    View Code

      5、小写转大写,判断是否为小写字母

    #include <iostream>
    using namespace std;
    int main()
    {
        char ch;
        cin >> ch;
        if(ch >= 'a' && ch <= 'z')
            ch -= 32;
        cout.put(ch) << endl;    
        return 0;
    }
    View Code
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string s;
        cin >> s;
        if(s[0] >= 'a' && s[0] <= 'z')
            s[0] -= 32;
        cout << s[0] << endl;
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        char s[256];
        cin >> s;
        if(s[0] >= 'a' && s[0] <= 'z')
            s[0] -= 32;
        cout << s[0] << endl;
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        char ch;
        cin >> ch;
        if(ch >= 'a' && ch <= 'z')
            ch -= 32;
        cout<< ch << endl;    
        return 0;
    }
    View Code

      第三周 基础练习

      1、判断奇偶数

    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
        cin >> x;
        if(x%2)
            cout << "odd" << endl;
        else
            cout << "even" << endl;
        return 0;
    }
    View Code

      2、判断数的类型,int(x)==x,判断是否为整数

    #include <iostream>
    using namespace std;
    int main()
    {
        double x;
        cin >> x;
        switch(int(x)==x){ //是否为整数
            case 0:
                if(x>1e-6)
                    cout << "positive real" << endl;
                else if(x<1e-6)
                    cout << "negative real" << endl;
                else
                    cout << "zero" << endl;
                break;
            case 1:
                if(int(x)>0)
                    cout << "positive integer" << endl;
                else if(int(x)<0)
                    cout << "negative integer" << endl;
                else
                    cout << "zero" << endl;
                break;
        }
        return 0;
    }
    View Code

      3、判断点的象限

    #include <iostream>
    using namespace std;
    int main()
    {
        double x, y;
        cin >> x >> y;
        if(x>1e-6 && y>1e-6)
            cout << 1 << endl;
        else if(x<1e-6 && y>1e-6)
            cout << 2 << endl;
        else if(x<1e-6 && y<1e-6)
            cout << 3 << endl;    
        else
            cout << 4 << endl;
        return 0;
    }
    View Code

      4、判断字符类型

    #include <iostream>
    using namespace std;
    int main()
    {
        char ch;
        cin >> ch;
        if(ch>='0' && ch<='9')
            cout << 0 << endl;
        else if(ch>='A' && ch<='Z')
            cout << 1 << endl;
        else if(ch>='a' && ch<='z')
            cout << 2 << endl;    
        else
            cout << -1 << endl;
        return 0;
    }
    View Code

      5、百分制成绩转五分制成绩

    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
        cin >> x;
        switch(x/10){
            case 10:
            case 9:
                cout << 5 << endl;
                break;
            case 8:
                cout << 4 << endl;
                break;
            case 7:
                cout << 3 << endl;
                break;
            case 6:
                cout << 2 <<endl;
                break;
            case 5:case 4:case 3:case 2:case 1:
                cout << 1 << endl;
                break;
            default:
                cout << 0 << endl;
        }
        return 0;
    }
    View Code

      6、显示n个字符

    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        char ch;
        cin >> n >> ch;
        while(n--)
            cout << ch;
        cout << endl;
        return 0;
    }
    View Code

      7、显示字符组成的矩形

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, m;
        char ch;
        cin >> m >> n >> ch;
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j)
                cout << ch;
            cout << endl;
        }
        return 0;
    }
    View Code

      8、用循环计算1+2+3+...+n

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, s = 0;
        cin >> n;
        for(int i=1; i<=n; ++i){
            s += i;
        }
        cout << s << endl;
        return 0;
    }
    View Code

      9、计算1+1/2+1/3+...+1/n

    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        double s = 0;
        cin >> n;
        for(int i=1; i<=n; ++i){
            s += 1.0/i;
        }
        cout << s << endl;
        return 0;
    }
    View Code

      10、计算n!,long fac = 1;

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

      11、交替输出1和-1

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, sign = 1, t;
        cin >> n;
        t = n;
        while(n--)
        {
            if(n<t-1) cout << " ";  
            cout << sign;
            sign = -sign;
        }
        return 0;
    }
    View Code

      12、判断整数的位数,n非负,即可以为0

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, cnt=0;
        cin >> n;
        
        if(!n)
            cnt++;
        
        while(n)
        {
            n /= 10;
            cnt++;
        }
        
        cout << cnt << endl;
        return 0;
    }
    View Code

      13、求非负整数的各位数字的和

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, sum=0;
        cin >> n;
        
        while(n)
        {
            sum += n%10;
            n /= 10;
        }
        
        cout << sum << endl;
        return 0;
    }
    View Code

      14、九九乘法表,行尾无空格

    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;
        
        for(int i=1; i<=n; ++i){
            for(int j=1; j<=i; ++j){        
                if(j>1)
                    cout << " ";
                cout << i << "*" << j << "=" << i*j;
            }        
            cout << endl;
        }
        return 0;
    }
    View Code

      15、不一样的九九乘法表

    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;
        
        for(int i=n; i>=1; --i){
            for(int j=1; j<=i; ++j){        
                if(j>1)
                    cout << " ";
                cout << i << "*" << j << "=" << i*j;
            }        
            cout << endl;
        }
        return 0;
    }
    View Code

      16、Fibonacci序列,输出n+1项

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, f0 = 0, f1 = 1;
        cin >> n;
        for(int i=0; i<n+1; i++){
            if(i)
                cout << " ";
            cout << f0;
            int sum = f0 + f1;    
            f0 = f1;
            f1 = sum;            
        }
        return 0;
    }
    View Code

      第三周 中级练习

      1、计算1!+2!+3!+…+n!

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, t = 1, result = 0;
        cin >> n;
        for(int i=1; i<=n; ++i){
            t *= i;
            result += t;
        }
        cout << result << endl;
        return 0;
    } 
    View Code

      2、a+aa+aaa

    #include <iostream>
    using namespace std;
    int main()
    {
        int a, n, t = 0, result = 0;
        cin >> a >> n;
        for(int i=0; i<n; ++i){
            t = t*10 + a; //保存中间结果
            result += t;
        }
        cout << result << endl;
        return 0;
    } 
    View Code

      3、arcsin(x)

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double x;
        cin >> x;
        
        int n = 0; //第0项
        double u = x, arcsin = 0;
        
        while(fabs(u)>=1e-8){            
            arcsin += u; //累加
            n++;         //从第一项开始计算u
            u =  u*(2*n-1)*(2*n-1)*x*x/2/n/(2*n+1);    
        }
        
        cout << arcsin << endl;
        return 0;
    } 
    View Code
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        double x;
        cin >> x;
        cout << asin(x) << endl;
        return 0;
    } 
    View Code

      4、回文数

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, t, sum = 0;
        cin >> n;
        t = n;
        
        while(t)
        {
            sum = sum*10 + t%10;
            t /= 10;
        }
        
        if(sum==n)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;        
        return 0;
    } 
    View Code
    #include <iostream>
    #include <string.h>
    using namespace std;
    int main()
    {
        char str[256], *p, *q;
        cin >> str;
        p = str, q = str + strlen(str)-1; 
        
        while(*p==*q && p<q){
            p++,q--;
        }
        
        if(p>=q)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;        
        return 0;
    }
    View Code
    #include <iostream>
    #include <string.h>
    using namespace std;
    int main()
    {
        char str[256], i, j;
        cin >> str;
        for(i=0, j=strlen(str)-1; str[i] == str[j] && i<j; ++i, --j);
        if(i>=j)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;        
        return 0;
    }
    View Code

      5、整数的素数因子分解

    #include <iostream>
    using namespace std;
    int main()
    {
        int i=2, n;
        cin >> n;
        cout << n << "=";
        while(n>1)
        {        
            if(n%i==0){
                cout << i;
                n /= i;
                if(n>1)
                cout << "*"; /* */
            }
            else{
                i++;
            }                
        }    
        return 0;
    } 
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;
        cout << n << "=";
        for(int i=2; n>1; ++i)
        {        
            if(n%i==0){
                cout << i;
                n /= i;
                --i;
                if(n>1)
                    cout << "*"; 
            }                
        }    
        return 0;
    } 
    View Code

      第三周 编程作业

      1、3-1打印3个相邻字母

    #include <iostream>
    using namespace std;
    int main()
    {
        char ch,ch1 = 'A';
        cin >> ch;
        if(ch>='a' && ch<='z')
            ch1 = 'a';
        cout << char((ch-ch1-1+26)%26+ch1) << ch << char((ch-ch1+1)%26+ch1) << endl;
        return 0;
    }
    View Code

      2、3-2歌唱大赛选手成绩计算

    #include <iostream>
    using namespace std;
    int main()
    {
        int score, min = 100, max = 0, sum = 0;
        int n = 10;
        while(n--)
        {
            cin >> score;
            if(score<0 || score>100){
                cout << "the score is invalid." << endl;
                break;
            }
            if(score>max)
                max = score;
            if(score<min)
                min = score;
            sum += score;
        }
        if(n==-1)
            cout << (sum - max - min)/8.0 << endl;    
        return 0;
    }
    View Code

      3、3-3猴子吃桃

    #include <iostream>
    using namespace std;
    int main()
    {
        int days,x=1;
        cin >> days;
    
        while(--days){
            x = 2*(x+1);
        }
        
        cout << x << endl;
        return 0;
    }
    View Code

      4、3-4搬砖问题

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, flag = 0;
        cin >> n;
        for(int men=0; men<=n/4; ++men)
        {
            for(int women=0; women<=(n-men)/3; ++women)
            {
                int children = n - men - women;
                //if(men*4*2+women*3*2+children==2*n){
                if(children%2==0 && men*4+women*3+children/2==n){
                    cout << "men" << men << endl;
                    cout << "women" << women << endl;
                    cout << "children" << children << endl;
                    flag = 1;
                }
            }
        }
        if(!flag)
            cout << "no result!" << endl;
        return 0;
    }
    View Code

      5、3-5美分找钱

    #include <iostream>
    using namespace std;
    int arr[] = {25, 10, 5};
    int func(int left, int index)
    {
        if(left==0 || index>=3)
            return 1;
        int count = 0;
        int x = left/arr[index];
        for (int i=x; i>=0; i--)
        {
            count += func(left - i*arr[index], index+1);
        }
        return count;
    }
    int main()
    {
        unsigned int n, count = 0;
        cin >> n;
        if (n<0||n>99)
            cout << "the money is invalid!" << endl;
        else
            cout << func(n, 0) << endl;
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int MakeChangeCore(int n,int denom)
    {
        int next_denom=0;
        switch(denom)
        {
        case 25:
            next_denom=10;
            break;
        case 10:
            next_denom=5;
            break;
        case 5:
            next_denom=1;
            break;
        case 1:
            return 1;
        }
     
        int res=0;
        for(int i=0;i*denom<=n;i++)
            res+=MakeChangeCore(n-i*denom,next_denom);
     
        return res;
    }
     
    int MakeChange(int n)
    {
        if(n<=0)
                return -1;
        return MakeChangeCore(n,25);
    }
    
    int main()
    {
        unsigned int n, count = 0;
        cin >> n;
        if (n<0||n>99)
            cout << "the money is invalid!" << endl;
        else
            cout << MakeChange(n) << endl;
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        unsigned int n, count = 0;
        cin >> n;
        if (n<0||n>99)
            cout << "the money is invalid!" << endl;
        else{
            for (int a=n/25; a>=0; a--)  //25美元
            {
                int ten = n-25*a;      //10美元
                for (int b=ten/10; b>=0; b--)
                {
                    int five = ten - 10*b;   //5美元
                    for (int c=five/5; c>=0; c--) 
                    {   //(n-25*a-10*b-5*c) 1美元
                        /* if (25*a+10*b+5*c+(n-25*a-10*b-5*c) == n)
                        {
                            count++;
                        } */
                        count++;
                    }
                }
            }
            cout << count << endl;
        }
        return 0;
    }
    View Code

      第4周 基础练习

      1、数组元素反序输出

    #include <iostream>
    using namespace std;
    int main( )
    {
        int n;
        cin >> n;
        int *arr = new int[n];
        for(int i=0; i<n; ++i)
            cin >> arr[i];
        cout << arr[n-1];
        for(int j=n-2; j>=0; --j)
            cout << " " << arr[j];
        return 0;
    }
    View Code

      2、求数组元素最大值

    #include <iostream>
    #include <climits>
    using namespace std;
    int main( )
    {
        int arr[100], index = 0; //其实数组无实质意义
        int max = INT_MIN;
        int x;
        for(; cin >> x, x != -9999; )
        {
            arr[index++] = x;
            if(arr[index-1] > max )
                max = arr[index-1];
        }
        cout << max << endl;
        return 0;
    }
    View Code
    #include <iostream>
    #include <climits>
    using namespace std;
    int main( )
    {
        int max = INT_MIN;
        int x;
        for(; cin >> x, x != -9999; )
        {
            if(x > max )
                max = x;
        }
        cout << max << endl;
        return 0;
    }
    View Code

      3、数组指定区间的元素的最大、最小、总和和平均值,总和及平均值均为double,总和若为整形,求平均值要强转

    #include <iostream>
    #include <climits>
    using namespace std;
    int main( )
    {
        int arr[] = {-1,15,-40,-180,99,-122,-124,27,192,128,
                    -165,95,161,-138, -183,51,107,39,-184,113,
                    -63,9,107,188,-11,-13,151,-52,7,6};
        int i, j, max = INT_MIN, min = INT_MAX;     
        double sum = 0, ave = 0;
        cin >> i >> j;
        
        if(i<j && i>=0 && j<=30)
        {
            for(int k = i; k<j; ++k)
            {
                sum += arr[k];
                if(arr[k] > max) max = arr[k] ;
                if(arr[k] < min) min = arr[k];
            }    
            cout << max << " " << min << " " << sum << " " << sum/(j-i) << endl;    
        }    
        else cout << "0 0 0 0" << endl;
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main( )
    {
        int arr[] = {-1,15,-40,-180,99,-122,-124,27,192,128,
                    -165,95,161,-138, -183,51,107,39,-184,113,
                    -63,9,107,188,-11,-13,151,-52,7,6};
        int i, j, max, min;     
        double sum = 0, ave = 0;
        cin >> i >> j;
        
        if(i<j && i>=0 && j<=30) // i,j条件
        {
            max = arr[i], min = arr[i]; //赋初始值
            for(int k = i; k<j; ++k)    //从i开始累加
            {
                sum += arr[k];
                if(arr[k] > max) max = arr[k] ;
                if(arr[k] < min) min = arr[k];
            }    
            cout << max << " " << min << " " << sum << " " << sum/(j-i) << endl;    
        }    
        else cout << "0 0 0 0" << endl;
        return 0;
    }
    View Code

      4、求矩阵每行元素最大值

    #include <iostream>
    using namespace std;
    int main( )
    {
        //int arr[20][20]; //省略
        int row, col, x;
        cin >> row >> col;
        for( int i= 0; i<row; i++){
            cin >> x;
            int max = x;
            for(int j=1; j<col; ++j)
            {
                cin >> x;
                if(x>max) max = x;
            }
            cout << max << endl;
        }
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main( )
    {
        int arr[20][20]; 
        int row, col;
        cin >> row >> col;
        for( int i= 0; i<row; i++){
            cin >> arr[i][0];
            int max = arr[i][0];
            for(int j=1; j<col; ++j)
            {
                cin >> arr[i][j];
                if(arr[i][j]>max) max = arr[i][j];
            }
            cout << max << endl;
        }
        return 0;
    }
    View Code

      5、求矩阵每列元素最大值,int max = arr[0][i];

    #include <iostream>
    using namespace std;
    int main( )
    {
        int arr[20][20]; 
        int row, col, i, j;
        cin >> row >> col;
        for( i= 0; i<row; i++){
            for( j=0; j<col; ++j)
            {
                cin >> arr[i][j];        
            }        
        }
        for( i= 0; i<col; i++){
            int max = arr[0][i];
            for(j=1; j<row; ++j)
            {
                if(max<arr[j][i])
                    max = arr[j][i];
            }
            if(i) cout << " ";
            cout << max;
        }
        return 0;
    }
    View Code

      6、计算向量的和

    #include <iostream>
    using namespace std;
    int main( )
    {
        int n;
        int arr1[100],arr2[100];
        cin >> n;
        for(int i=0; i<n; ++i)
            cin >> arr1[i];
        for(int i=0; i<n; ++i)
            cin >> arr2[i];
        for(int i=0; i<n; ++i){
            if(i) cout << " ";
            cout << arr1[i] + arr2[i];
        }    
        return 0;
    }
    View Code

      7、矩阵向量的内积

    #include <iostream>
    using namespace std;
    int main( )
    {
        int n, sum = 0;
        int arr1[100],arr2[100];
        cin >> n;
        for(int i=0; i<n; ++i)
            cin >> arr1[i];
        for(int i=0; i<n; ++i)
            cin >> arr2[i];
        for(int i=0; i<n; ++i){
            sum += arr1[i] * arr2[i];
        }    
        cout << sum << endl;
        return 0;
    }
    View Code

      8、计算向量的范数

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main( )
    {
        int n;
        double arr[100],sum = 0;
        cin >> n;
        for(int i=0; i<n; ++i){
            cin >> arr[i];
            sum += arr[i]*arr[i];
        }        
        cout << sqrt(sum) << endl;
        return 0;
    }
    View Code
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main( )
    {
        int n;
        double x, sum = 0;
        cin >> n;
        for(int i=0; i<n; ++i){
            cin >> x;
            sum += x * x;
        }        
        cout << sqrt(sum) << endl;
        return 0;
    }
    View Code

      9、计算向量的欧氏距离,double类型

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main( )
    {
        int n;
        double sum = 0;
        double arr1[100],arr2[100];
        cin >> n;
        for(int i=0; i<n; ++i)
            cin >> arr1[i];
        for(int i=0; i<n; ++i)
            cin >> arr2[i];
        for(int i=0; i<n; ++i){
            sum += (arr1[i] - arr2[i]) * (arr1[i] - arr2[i]);
        }    
        cout << sqrt(sum) << endl;
        return 0;
    }
    View Code

      10、矩阵求和

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main( )
    {
        int n, m;
        double arr1[100][100],arr2[100][100],sum[100][100];
        cin >> n >> m;
        for(int i=0; i<n; ++i)
        {
            for(int j=0; j<m; ++j)
                cin >> arr1[i][j];    
        }
        for(int i=0; i<n; ++i)
        {
            for(int j=0; j<m; ++j)
                cin >> arr2[i][j];
        }
        for(int i=0; i<n; ++i)
        {
            for(int j=0; j<m; ++j){
                sum[i][j] = arr1[i][j] + arr2[i][j];
                if(j) cout << " ";
                cout << sum[i][j];
            }
            cout << endl;
        }
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main( )
    {
        int n, m;
        double arr1[100][100], arr2[100][100];
        cin >> n >> m;
        for(int i=0; i<n; ++i)
        {
            for(int j=0; j<m; ++j)
                cin >> arr1[i][j];    
        }
        for(int i=0; i<n; ++i)
        {
            for(int j=0; j<m; ++j)
                cin >> arr2[i][j];
        }
        for(int i=0; i<n; ++i)
        {
            for(int j=0; j<m; ++j){            
                if(j) cout << " ";
                cout << arr1[i][j] + arr2[i][j];
            }
            cout << endl;
        }
        return 0;
    }
    View Code

      11、输入字符串,求长度

    #include <iostream>
    using namespace std;
    int main( )
    {
        char str[100];
        cin.getline(str,100);
        char *p = str;
        int cnt = 0;
        while(*p)
        {
            cnt++;
            p++;
        }
        cout << cnt << endl;
        return 0;
    }
    View Code

      12、统计字符串中大写字母的数量

    #include <iostream>
    using namespace std;
    int main( )
    {
        char str[100];
        cin.getline(str,100);
        char *p = str;
        int cnt = 0;
        while(*p)
        {
            if(*p>='A' && *p<='Z')
                cnt++;
            p++;
        }
        cout << cnt << endl;
        return 0;
    }
    View Code

      13、复制字符串

    #include <iostream>
    using namespace std;
    int main( )
    {
        char s1[100], s2[100];
        cin >> s1;
        char* p1 = s1, *p2 = s2;
        while( *p2++ = *p1++ );
        cout << s2 << endl;
        return 0;
    }
    View Code

      14、字符串逆序

    #include <iostream>
    using namespace std;
    int main( )
    {
        char s[100];
        cin >> s;
        
        int index = 0, i,j;
        while(s[index++]);
        
        for(i=0, j = index - 2; i<j; ++i,--j)
        {
            char t = s[i];
            s[i] = s[j];
            s[j] = t;
        }
        cout << s << endl;
        return 0;
    }
    View Code

      15、定义表示平面坐标点的结构体计算两点距离

    #include <iostream>
    #include <cmath>
    using namespace std;
    struct POINT{
        double x, y;
    };
    int main( )
    {
        POINT p1, p2;
        cin >> p1.x >> p1.y >> p2.x >> p2.y;
        cout << sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)) << endl;
        return 0;
    }
    View Code

      第4周 中级练习

      1、矩阵原地转置,for(j=0; j<i; ++j)

    #include <iostream>
    using namespace std;
    int main( )
    {
        int arr[10][10], i, j;
        int n;
        cin >> n;
        for(i=0; i<n; ++i){
            for(int j=0; j<n; ++j){
                cin >> arr[i][j];
            }
        }
        //转置
        for(i=0; i<n; ++i){
            for(j=0; j<i; ++j){
                if(i!=j){
                    int t = arr[i][j];
                    arr[i][j] = arr[j][i];
                    arr[j][i] = t;
                }        
            }
        }
        for(i=0; i<n; ++i){
            for( j=0; j<n; ++j){
                if(j) cout << " ";
                cout << arr[i][j];            
            }
            cout << endl;
        }
        return 0;
    }
    View Code

      2、判断对称矩阵,flag = 1;

    #include <iostream>
    using namespace std;
    int main( )
    {
        int arr[10][10], i, j;
        int n, flag = 0;
        cin >> n;
        for(i=0; i<n; ++i){
            for(j=0; j<n; ++j){
                cin >> arr[i][j];
            }
        }
        //判断对称
        for(i=0; i<n; ++i){
            for(j=0; j<i; ++j){
                if(arr[i][j] != arr[j][i]){
                    flag = 1;
                    break;
                }
            }
        }
        if(flag)
            cout << "No" << endl;
        else
            cout << "Yes" << endl;
        return 0;
    }
    View Code

      3、去掉字符串末尾的空格

    #include <iostream>
    using namespace std;
    int main( )
    {
        char str[100];
        cin.get(str,100);
        cout << "|" << str << "|" << endl;
        
        char *p = str;
        while( *p ) p++;
        while( *--p == ' ' );
        *(p+1) = '';
        
        cout << "|" << str << "|" << endl;
        return 0;
    }
    View Code

      4、去掉字符串开头的空格符

    #include <iostream>
    using namespace std;
    int main( )
    {
        char str[100];
        cin.get(str,100);
        cout << "|" << str << "|" << endl;
        
        char *p = str, *q = str;
        while( *p == ' ') p++;
        while( *q++ = *p++);
        
        cout << "|" << str << "|" << endl;
        return 0;
    }
    View Code

      5、去掉字符串中间的所有空格,两头的空格保留,从后向前可能会覆盖(计算后头空格个数)

    #include <iostream>
    using namespace std;
    int main( )
    {
        char str[100];
        cin.get(str,100);
        cout << "|" << str << "|" << endl;
        
        //保留字符串前空格
        char *p = str, *q;
        while(*p == ' ') p++;
        q = p;
        
        //去掉字符串中间的空格
        while( *p )
        {
            if( *p != ' ' )
                *q++ = *p;        
            p++;
        }
        
        //计算字符串后的空格
        int cnt = 0;
        while(*--p == ' ')
            cnt++;
        while(cnt--)
            *q++ = ' ';
        *q = '';
    
        cout << "|" << str << "|" << endl;
        return 0;
    }
    View Code

      6、查找子串

    #include <iostream>
    using namespace std;
    int main( )
    {
        char s1[100], s2[100];
        char *p, *q;
        gets(s1);
        gets(s2); 
        /*cin.getline(s1,100);
        cin.getline(s2,100);*/
        
        p = s1, q = s2; /* p,q 源串、目标串比较开始位置 */
    
        int cnt = 1;
        while(*p&&*q)
        {
            if (*p==*q)
            {
                p++;
                q++;
            }
            else
            {                  
                p = s1 + cnt++;
                q = s2;
            } 
        }
        if (*q=='') /* 匹配 */
            cout << cnt << endl;
        else
            cout << "没有该子串" << endl;
        return 0;
    }
    View Code

      7、排序,插入排序

    #include <iostream>
    using namespace std;
    int main( )
    {
        int arr[100];
        int n, i, j;
        cin >> n;
        for(i=0; i<n; ++i)
            cin >> arr[i];
        for (j=1; j<n; j++ ) { 
            int t = arr[j];
            for ( i=j; i>0 && arr[i-1]<t; i-- )
                arr[i] = arr[i-1]; /*依次与已排序序列中元素比较并右移*/
            arr[i] = t; /* 放进合适的位置 */
        }
        for(i=0; i<n; ++i){
            if(i) cout << " ";
            cout << arr[i];
        }
        return 0;
    }
    View Code

      8、二分查找,找到,break;

    #include <iostream>
    using namespace std;
    int main( )
    {
        int arr[300], index = 0;
        int x1, x2;
        
        for(; cin >> x1, x1 != -99999; )
            arr[index++] = x1;
        cin >> x2;
        
        int left = 0, right = index-1;
        while(left<=right)
        {
            int mid = (right-left)/2 + left; //防溢出
            if(arr[mid]>x2)
                right = mid - 1;
            else if(arr[mid]<x2)
                left = mid + 1;
            else{
                cout << mid << endl;
                break;
            }        
        }
        if(left>right)
            cout << -1 << endl;
        return 0;
    }
    View Code

      第4周 编程作业

      1、恺撒加密

    #include <iostream>
    using namespace std;
    int main( )
    {
        char str[256];
        cin >> str;
        char *p = str;
        while(*p)
        {
            char ch = 'a';
            char num = -32;
            if(*p>='A' && *p<='Z')
                ch = 'A', num = 32;
            *p = (*p - ch + 3)%26 + ch + num;
            p++;
        }
        cout << str << endl;
        return 0;
    }
    View Code

      2、矩阵转置

    #include <iostream>
    using namespace std;
    int main( )
    {
        int n, arr[6][6]; 
        cin >> n;
        if(n<1 || n > 5)
            cout << "matrix order error" << endl;
        else{
            for(int i=0; i<n; ++i){
                for(int j=0; j<n; ++j){
                    cin >> arr[j][i];
                }
            }
            for(int i=0; i<n; ++i){
                for(int j=0; j<n; ++j){
                    if(j) cout << " ";
                    cout << arr[i][j];
                }
                cout << endl;    
            }    
        }
        return 0;
    }
    View Code

      3、按点击率显示歌曲

    #include <iostream>
    using namespace std;
    struct SONG{
        char title[50];
        char name[20];
        int point;
    };
    int main( )
    {
        SONG songs[5];
        int i, j;
        for(i=0; i<5; ++i){
            cin >> songs[i].title >> songs[i].name >> songs[i].point;
        }
        /* 插入排序 */
        for (j=1; j<5; j++ ) { 
            SONG t = songs[j]; /* 取出未排序序列中的第一个元素*/
            for ( i=j; i>0 && songs[i-1].point<t.point; i-- )
                songs[i] = songs[i-1]; /*依次与已排序序列中元素比较并右移*/
            songs[i] = t; /* 放进合适的位置 */
        }
        for(i=0; i<5; ++i){
            cout << songs[i].title << " " << songs[i].name << " ";
            cout << songs[i].point << endl;
        }     
        return 0;
    }
    View Code

      4、星期转换

    #include <iostream>
    using namespace std;
    int main( )
    {
        const char *weeks[] = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
        int n;
        cin >> n;
        if(n<1||n>7)
            cout << "invalid" << endl;
        else
            cout << weeks[n%7] << endl;
        return 0;
    }
    View Code

      5、插入加密,明文最后一个字符后也要打印插入字符

    #include <iostream>
    using namespace std;
    int main( )
    {
        char str[30], letter[6] = "abcde";
        int n, index = 0;
        
        cin >> str >> n;    
        char *p = str;
        
        while(*p){        
            int t = n;
            while(*p && t--){
                putchar(*p++);
            }    
            putchar(letter[index++]);
            index %= 5;
        }
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main( )
    {
        char str[30], letter[6] = "abcde";
        int n, index = 0;
        
        cin >> str >> n;    
        char *p = str;
             
        for(int t = n; *p; index %= 5){    //单循环            
            if(t--)
                putchar(*p++);
            //间隔后打印插入字符并恢复间隔, 明文最后一个字符后也要打印插入字符
            if(!*p || !t){  
                putchar(letter[index++]);
                t = n;
            }                    
        }
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    int main()
    {
        string s;
        string word[5] = {"a","b","c","d","e"};
        int step, index = 0;
        getline( cin, s );
        cin >> step;
    
        for(decltype(s.size())i=step; i<s.size(); i+=(step+1),index%=5){
            s.insert(i,word[index++]);
        }
        s += word[index];
    
        cout << s << endl;
        return 0;
    }
    string / 同学解题代码
    #include <iostream>
    using namespace std;
    int main()
    {
        string s;
        string word = "abcde";
        int step, n = 1, index = 0; //n插入的字符个数
        
        cin >> s >> step;
    
        for(int i=step; i<s.size(); i+=(n+step),index%=5){
            s.insert(i,n,word[index++]);
        }
        s.insert(s.size(),n,word[index]);//字符串尾部要求插入字符
        
        cout << s << endl;
        return 0;
    }
    View Code

      6、三色球,枚举,j = i+1;

    #include <iostream>
    using namespace std;
    int main( )
    {
        enum color {red, yellow, blue};
        int tmp, i, j, t;
        for(i=red; i<=yellow; ++i){
            for(j=i+1; j<=blue; ++j){  //j=i+1
                for(t=0; t<2; ++t){    //二种选择或i或j
                    switch(t){
                        case 0: tmp = i; break; //对应枚举
                        case 1: tmp = j; break;
                    }
                    switch((enum color)tmp){    //强转枚举
                        case red: cout << "red "; break;
                        case yellow: cout << "yellow "; break;
                        case blue: cout << "blue "; break;
                    }                
                }
                cout << endl;
            }
        }
        return 0;
    }
    View Code

      第5周 基础练习

      1、求两个数的和

    #include <iostream>
    using namespace std;
    double mysum(double a, double b){
        return a + b;
    }
    int main()
    {
        double a, b;
        cin >> a >> b;   
        cout << mysum(a,b) << endl;
        return 0;
    }
    View Code

      2、求绝对值的函数,double的最高位是符号位,位运算 &0x7fffffff 可以改变最高位的值

    #include <iostream>
    using namespace std;
    double myfabs(double x){
        *((int*)&x + 1) &= 0x7fffffff;
        return x;
    }
    int main()
    {
        double x;
        cin >> x;   
        cout << myfabs(x) << endl;
        return 0;
    }
    View Code

      3、x的k次方

    #include <iostream>
    using namespace std;
    
    double myfabs(double x){
        *((int*)&x + 1) &= 0x7fffffff;
        return x;
    }
    
    double mypow(double x, int n){
        double ret = 1;
        if( myfabs(x) <= 1e-7 )
            ret = 0.0;
        else if(!n) 
            ret = 1.0;
        else if( n<0 ){
            n = -n;
            for(int i=0; i<n; ++i)
                ret /= x;
        }
        else if(n>0){
            for(int i=0; i<n; ++i)
                ret *= x;
        }
        return ret;
    }
    int main()
    {
        double x;
        int n;
        cin >> x >> n;   
        cout << mypow(x, n) << endl;
        return 0;
    }
    View Code

      4、求n!的函数

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

      5、输入数组元素

    #include <iostream>
    using namespace std;
    
    int input(int *arr){
        int index = 0, x;
        for(; cin >> x, x != -9999; )
            arr[index++] = x;
        return index;
    }
    
    int main()
    {
        int arr[100];
        int n = input(arr);
        for(int i= n-1; i>=0; --i)
        {
            if(i != n-1)
                cout << " ";
            cout << arr[i];
        }
        return 0;
    }
    View Code

      6、输出数组元素值

    #include <iostream>
    using namespace std;
    
    int input(int *arr){
        int index = 0, x;
        for(; cin >> x, x != -9999; )
            arr[index++] = x;
        return index;
    }
    
    void output(int *arr, int n){
        for(int i= 0; i<n; ++i)
        {
            if(i)
                cout << " ";
            cout << arr[i];
        }
    }
    
    int main()
    {
        int arr[100];
        int n = input(arr);
        output(arr, n);
        return 0;
    }
    View Code

      7、将数组元素逆序

    #include <iostream>
    using namespace std;
    
    int input(int *arr){
        int index = 0, x;
        for(; cin >> x, x != -9999; )
            arr[index++] = x;
        return index;
    }
    
    void output(int *arr, int n){
        for(int i= 0; i<n; ++i)
        {
            if(i)
                cout << " ";
            cout << arr[i];
        }
    }
    
    void reserve(int *arr, int n){
        for(int i=0, j=n-1; i<j; ++i, --j){
            int t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
        }
    }
    
    int main()
    {
        int arr[100];
        int n = input(arr);
        reserve( arr, n);
        output(arr, n);
        return 0;
    }
    View Code

      8、求数组元素的和

    #include <iostream>
    using namespace std;
    
    int input(int *arr){
        int index = 0, x;
        for(; cin >> x, x != -9999; )
            arr[index++] = x;
        return index;
    }
    
    int addArr(int *arr, int n){
        int sum = 0; 
        for(int i=0; i<n; ++i)
            sum += arr[i];
        return sum;
    }
    
    int main()
    {
        int arr[100];
        int n = input(arr);
        cout << addArr(arr, n) << endl;
        return 0;
    }
    View Code

      9、求字符串的长度的函数

    #include <iostream>
    using namespace std;
    
    int stringLength(char *p){
        int i = 0;
        while(p[i])
            i++;
        return i;
    }
    
    int main()
    {
        char str[100];
        cin >> str;
        cout << stringLength(str) << endl;
        return 0;
    }
    View Code

      10、字符串转大写

    #include <iostream>
    using namespace std;
    
    void upper(char *p){    
        while(*p){
            if(*p>='a'&&*p<='z')
                *p -= 32;
            p++;
        }        
    }
    
    int main()
    {
        char str[200];
        cin >> str;
        upper(str);
        cout << str << endl;
        return 0;
    }
    View Code

      11、字符串复制函数

    #include <iostream>
    using namespace std;
    
    void  mystrcpy(char s1[],char s2[]);
    
    int main()
    {
        char s1[100], s2[100];
        cin.getline(s1, 100);
        mystrcpy(s2,s1);
        cout << s2 << endl;
        return 0;
    }
    
    void  mystrcpy(char s1[],char s2[]){
        while(*s1++ = *s2++);
    }
    View Code

      12、字符串比较函数

    #include <iostream>
    using namespace std;
    
    int compare(char*, char*);
    
    int main()
    {
        char s1[200], s2[200];
        cin >> s1 >> s2;
        cout << compare(s1,s2) << endl;
        return 0;
    }
    
    int compare(char *s1, char *s2)
    {
        int i, ret = 0;
        while( ! (ret = *( unsigned char *)s1 - *(unsigned char *)s2) && *s2)
            s1++, s2++;
        if ( ret < 0 )
            ret = -1 ;
        else if ( ret > 0 )
            ret = 1 ;
        return ret;    
    }
    View Code

      第5周 中级练习

      1、比较字符串(不区分大小写),比较不应修改字符串,可以用2个变量对两个字符串的字母取值,转为小写,比较

    #include <iostream>
    using namespace std;
    int compare(const char*, const char*);
    int main()
    {
        char str1[100], str2[100];
        cin >> str1 >> str2;
        cout << compare(str1,str2) << endl;
        return 0;
    }
    int compare(const char *dst, const char *src)
    {
        int ch1, ch2, ret = 0;
        do
        {
            if ( ((ch1 = (unsigned char)(*(dst++))) >= 'A') &&(ch1 <= 'Z') )
                ch1 += 0x20;
            if ( ((ch2 = (unsigned char)(*(src++))) >= 'A') &&(ch2 <= 'Z') )
                ch2 += 0x20;
        } while ( ch1 && (ch1 == ch2) );
    
        if ( ch1-ch2 < 0)
            ret = -1 ;
        else if ( ch1-ch2 > 0 )
            ret = 1 ;
        return ret;    
    }
    View Code

      2、二分法解方程

    #include <iostream>
    #include <cmath>
    using namespace std;
    double fun(double x){
        return 2*x*x*x - 4*x*x + 3*x - 6;
    }
    int main()
    {
        double left, right, middle, eps;
        cin >> left >> right >> eps;
        
        do
        {
            middle = (left+right)/2;
            if(fun(middle)*fun(left)>0)
              left = middle;
            else
              right = middle;
        }
        while(fabs(fun(middle))>=eps);
        
        cout << middle << endl;
        return 0;
    }
    View Code

      3、牛顿法解方程,求导,3*a*x*x + 2*b*x + c

    #include <iostream>
    using namespace std;
    
    double fun(double x){
        return x*x*x + 2*x*x + 3*x + 4;//系数a,b,c,d
    }
    
    double fun1(double x){
        return  3*x*x + 4*x + 3; //3*a*x*x + 2*b*x + c
    }
    
    double fabs(double x){
        *(((int *) &x) + 1) &= 0x7fffffff; //最高位, 置0
        return x;
    }
    
    int main()
    {
        double x0, eps;
        cin >> x0 >> eps;
    
        while(fabs(fun(x0))>eps)
            x0 -= fun(x0)/fun1(x0);     
    
        cout << x0 << endl;    
        return 0;
    }
    View Code

      4、输入、排序、查找

    #include <iostream>
    using namespace std;
    
    int input(int *arr){ //输入
        int x, index = 0;
        for( ;cin >> x, x != -9999; )
            arr[index++] = x; 
        return index;
    }
    
    void insertSort(int *arr, int n){//插入排序
        int i, j, t;
        for (j=1; j<n; j++ ) { 
            t = arr[j];
            for ( i=j; i>0 && arr[i-1]>t; i-- )
                arr[i] = arr[i-1]; 
            arr[i] = t; 
        }
    }
    
    int findX(int *arr, int n, int x){//二分查找
        int left = 0, right = n-1, mid = -1;
        while(left<=right)
        {
            mid = (right-left)/2 + left; //防溢出
            if(arr[mid]>x)
                right = mid - 1;
            else if(arr[mid]<x)
                left = mid + 1;
            else
                break;
        }
        if(left>right) mid = -1;
        return mid;
    }
    
    void find5X(int *arr, int n){ //查找5个元素
        int x;
        for(int i=0; i<5; ++i)
        {
            cin >> x;
            cout << findX(arr, n, x) << endl;    
        }
    }
    
    int main()
    {
        int arr[100], n;
        n = input( arr );
        insertSort( arr, n );
        find5X( arr, n );    
        return 0;
    }
    View Code

      5、单词排序,用不到复制、大小写转换函数

    #include <iostream>
    using namespace std;
    
    void insertSort(char **arr, int n);
    int compare(const char *dst, const char *src);
    
    int main()
    {
        char **str = new char*[100];
        for(int i=0; i<100; ++i)
            str[i] = new char[20];
        int n;
        cin >> n;
        for(int i=0; i<n; ++i){
            cin >> str[i];
        }
        insertSort( str, n );
        for(int j=0; j<n; ++j)
            cout << str[j] << endl;
        return 0;
    }
    
    void insertSort(char **arr, int n){//插入排序
        int i, j, t;
        for (j=1; j<n; j++ ) { 
            char *t = arr[j];
            for ( i=j; i>0 && 1 == compare(arr[i-1],t); i-- )
                arr[i] = arr[i-1]; 
            arr[i] = t; 
        }
    }
    
    int compare(const char *dst, const char *src)
    {
        int ch1, ch2, ret = 0;
        do
        {
            if ( ((ch1 = (unsigned char)(*(dst++))) >= 'A') &&(ch1 <= 'Z') )
                ch1 += 0x20;
            if ( ((ch2 = (unsigned char)(*(src++))) >= 'A') &&(ch2 <= 'Z') )
                ch2 += 0x20;
        } while ( ch1 && (ch1 == ch2) );
    
        if ( ch1-ch2 < 0)
            ret = -1 ;
        else if ( ch1-ch2 > 0 )
            ret = 1 ;
        return ret;    
    }
    View Code

      6、识别数字,数字逐字累加后乘以符号存储数字;还可以数字累加后存储数字,符号也可以存储

    #include <iostream>
    using namespace std;
    
    void solution(char *p)
    {
        int i, arr[256], index = 0;
        int flag = 0, sum = 0, sign = 1; 
        
        for(i=0; p[i]; ++i)
        {
            if( p[i]>='0' && p[i]<='9' )
            {        
                sum = sum*10 + p[i] - '0';
                if( i && p[i-1] == '-' )
                    sign = -1;
                flag = 1;
                continue;
            }    
            if(flag)
            {    
                arr[index++] = sum*sign;
                sign = 1; //符号置 1
                sum = 0;  //sum置 0
                flag = 0; //数字标记置 0
            }            
        }
        
        if(flag){ //字符串最后为数字
            arr[index++] = sum*sign;
        }
        
        for(i=0; i<index; ++i)
        {
            if(i) 
                cout << " ";
            cout << arr[i];    
        }
    }
    
    int main()
    {
        char str[256];
        cin.getline(str,256);    
        solution(str);
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    
    typedef struct {    
        short tag;//判断类型
        union {
            int num;
            char ch;
        }u; //联合体       
    }SignedNumber;
    
    void solution(char *p)
    {
        SignedNumber arr[128]; //结构体数组
        int index = 0;
        int flag = 0, sum = 0; 
        
        for( ; *p; p++)
        {
            //与数字相关
            if( *p>='0' && *p<='9' )
            {        
                sum = sum*10 + *p - '0';    
                flag = 1;
                continue;
            }       
            if(flag)
            {    
                arr[index].u.num = sum;
                arr[index++].tag = 0;
                sum = 0; 
                flag = 0;
            }  
            //符号相关
            if(*p=='-' && *(p+1) && *(p+1)>='0'&&*(p+1)<='9'){
                arr[index].u.ch = *p;  
                arr[index++].tag = 1;
            }           
        }
        
        if(flag){ //字符串最后是数字
            arr[index].u.num = sum;
            arr[index++].tag = 0;
        }
        
        for(int i=0; i<index; ++i)
        {
            if(i && !arr[i-1].tag) //前面的元素是数字打印空格
                cout << " ";
            if(!arr[i].tag){
                cout << arr[i].u.num;
            }    
            else if(arr[i+1].u.num){//0前不打印符号
                cout << arr[i].u.ch;
            }        
        }
    }
    
    int main()
    {
        char str[256];
        cin.getline(str,256);    
        solution(str);
        return 0;
    }
    View Code

      第5周 编程作业

      1、编写字符串反转函数mystrrev

    #include <iostream>
    using namespace std;
    
    void mystrrev(char str[]);
    
    int main()
    {
        char s[100];
        cin.getline(s, 100);
        mystrrev(s);
        cout << s << endl;
        return 0;
    }
    
    void mystrrev(char str[]) 
    {
        int i, j, len = 0;
        while(str[len]) len++;
        for(i=0, j=len-1; i<j; ++i, --j)
        {
            char t = str[i];
            str[i] = str[j];
            str[j] = t;
        }
    }
    View Code

      2、编写一组求数组中最大最小元素的函数

    #include <iostream>
    using namespace std;
    
    int imax(int array[], int count);
    int imin(int array[], int count); 
    
    int main()
    {
        int arr[100];
        int n;
        cin >> n;
        for(int i=0; i<n; ++i)
            cin >> arr[i];
        cout << imax(arr, n) << endl;
        cout << imin(arr, n) << endl;
        return 0;
    }
    
    int imax(int array[], int count){
        int max = array[0];
        for(int i=1; i<count; ++i)
            if(max<array[i])
                max = array[i];
        return max;
    }
    int imin(int array[], int count){
        int min = array[0];
        for(int i=1; i<count; ++i)
            if(min>array[i])
                min = array[i];
        return min;
    }
    View Code

      3、编写函数判断一个整数是否为素数,高效的算法是用一个数组存储素数,被素数整除的不是素数

    #include <iostream>
    using namespace std;
    
    int isprime(int a);
    
    int main()
    {
        int a, flag = 0;
        for(; cin >> a, a; ){        
            if(isprime(a)){
                if(flag) cout << " ";
                cout << a;
                flag = 1;
            }
        }        
        return 0;
    }
    
    int isprime(int a){
        int ret = 1;
        if(a==1) ret = 0;
        for(int i=2; i<=a/i; ++i){
            if(a%i==0){
                ret = 0; 
                break;
            }
        }    
        return ret;
    }
    View Code

      4、编写函数去除字符串中包含的非字母字符(不包括空格),并将小写字母转换成大写字母

    #include <iostream>
    using namespace std;
    
    void upperStr(char*);
    
    int main()
    {
        char str[200];
        cin.getline(str, 200);
        upperStr(str);
        cout << str << endl;
        return 0;
    }
    
    void upperStr(char* str){
        char *p = str;
        while(*p){
            if(*p>='a'&&*p<='z')
                *str++ = *p - 32;
            else if(*p>='A'&&*p<='Z'||*p == ' ')
                *str++ = *p;
            p++;
        }
        *str = *p;
    }
    View Code

      5、编写函数计算一个英文句子中的单词个数,忽略非字母,发现字母计数,忽略字母,重复这个过程;一层循环,二个 if

    #include <iostream>
    using namespace std;
    
    int countWords(char*);
    
    int main()
    {
        char str[500];
        cin.getline(str, 500);
        cout << countWords(str) << endl;
        return 0;
    }
    
    int countWords(char* p){
        int count = 0, flag = 1;//如果只一个字母
        for( ; *p; p++){
            if(!(*p>='a'&&*p<='z'||*p>='A'&&*p<='Z')){
                flag = 1; //之后是字母
                continue; //没有字母
            }
            if(flag){
                count++;
                flag = 0;
            }
        }
        return count;
    }
    View Code

      第6周 基础练习

      1、递归计算n!

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

      2、递归计算1+2+3+…+n

    #include <iostream>
    using namespace std;
    
    int sum( int n){
        if(n==0) //n 为 0 和为 0
            return 0;
        if(n==1)
            return 1;
        return n+sum(n-1);
    }
    
    int main()
    {
        int n; 
        cin >> n;
        cout << sum( n ) << endl;
        return 0;
    }
    View Code

      3、递归求数组元素的最大值

    #include <iostream>
    using namespace std;
    
    int arrMax( int *arr, int n){
        int max;
        if(n==1)
            return arr[0];            
        max = arrMax(arr, n-1);
        if(arr[n-1]>max)
            max = arr[n-1];    
        return max;
    }
    
    int main()
    {
        int n; 
        int arr[100];
        cin >> n;
        for(int i=0; i<n; ++i)
            cin >> arr[i];
        cout << arrMax( arr, n ) << endl;
        return 0;
    }
    View Code

      4、递归求数组元素的和

    #include <iostream>
    using namespace std;
    
    int arrSum( int *arr, int n){
        int sum;
        if(n==0)
            return 0; //n为0时返回0
        if(n==1)
            return arr[0];
        sum = arrSum(arr, n-1);
        sum += arr[n-1];
        return sum;
    }
    
    int main()
    {
        int n; 
        int arr[100];
        cin >> n;
        for(int i=0; i<n; ++i)
            cin >> arr[i];
        cout << arrSum( arr, n ) << endl;
        return 0;
    }
    View Code

      5、递归求Fibonacci序列的第n项

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

      6、递归逆序数组元素

    #include <iostream>
    using namespace std;
    
    void reverse(int *arr, int i, int j){
        if( i < j ){
            int t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
            reverse(arr, i+1, j-1);        
        }
    }
    
    int main()
    {  
        int n;
        int arr[100];
        cin >> n;
        for(int i=0; i<n; ++i)
            cin >> arr[i];
        reverse(arr,0,n-1);
        for(int i=0; i<n; ++i){
            if(i) cout << " ";
            cout << arr[i];
        }
        cout << endl;    
        return 0;
    }
    View Code

      7、输入输出数组元素的函数重载

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int input( int *ai );
    int input( double *ad );
    void print( int *ai, int ni );
    void print( double *ad, int nd );
    
    int main()
    {  
        int ai[100];
        double ad[100];
        int ni, nd;
        ni = input( ai );
        nd = input( ad );
        print( ai, ni );
        print( ad, nd );
        return 0;
    }
    
    int input( int *ai ){
        int i = 0, x;
        for(; cin >> x, x != -9999;)
            ai[i++] = x;
        return i;
    }
    int input( double *ad ){
        int i = 0;
        double x;
        for(; cin >> x, fabs(x - (-9999))>1e-7; )
            ad[i++] = x;
        return i;
    }
    void print( int *ai, int ni ){
        for(int i=0; i<ni; ++i)
        {
            if(i) cout << " ";
            cout << ai[i];
        }
        cout << endl;
    }
    void print( double *ad, int nd ){
        for(int i=0; i<nd; ++i){
            if(i) cout << " ";
            cout << ad[i];
        }
        cout << endl;
    }
    View Code

      8、逆序函数重载

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int input( int *ai );
    int input( double *ad );
    void print( int *ai, int ni );
    void print( double *ad, int nd );
    void reverse( int *ai, int ni );
    void reverse( double *ad, int nd );
    
    int main()
    {  
        int ai[100];
        double ad[100];
        int ni, nd;
        ni = input( ai );
        nd = input( ad );
        reverse( ai, ni );
        reverse( ad, nd );
        print( ai, ni );
        print( ad, nd );
        return 0;
    }
    
    int input( int *ai ){
        int i = 0, x;
        for(; cin >> x, x != -9999;)
            ai[i++] = x;
        return i;
    }
    int input( double *ad ){
        int i = 0;
        double x;
        for(; cin >> x, fabs(x - (-9999))>1e-7; )
            ad[i++] = x;
        return i;
    }
    void print( int *ai, int ni ){
        for(int i=0; i<ni; ++i)
        {
            if(i) cout << " ";
            cout << ai[i];
        }
        cout << endl;
    }
    void print( double *ad, int nd ){
        for(int i=0; i<nd; ++i){
            if(i) cout << " ";
            cout << ad[i];
        }
        cout << endl;
    }
    
    void reverse( int *ai, int ni ){
        int i, j;
        for(i=0, j = ni-1; i<j; ++i, --j){
            int t = ai[i];
            ai[i] = ai[j];
            ai[j] = t;
        }
    }
    void reverse( double *ad, int nd ){
        int i, j;
        for(i=0, j = nd-1; i<j; ++i, --j){
            double t = ad[i];
            ad[i] = ad[j];
            ad[j] = t;
        }
    }
    View Code

      9、数组元素求和函数的重载

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int input( int *ai );
    int input( double *ad );
    int mysum( int *ai, int ni );
    double mysum(double *ad, int nd );
    
    int main()
    {  
        int ai[100];
        double ad[100];
        int sumi;
        double sumd;
        int ni, nd;
        ni = input( ai );
        nd = input( ad );
        sumi = mysum( ai, ni );
        sumd = mysum(ad, nd );
        cout << sumi << " " << sumd << endl;
        return 0;
    }
    
    int input( int *ai ){
        int i = 0, x;
        for(; cin >> x, x != -9999;)
            ai[i++] = x;
        return i;
    }
    int input( double *ad ){
        int i = 0;
        double x;
        for(; cin >> x, fabs(x - (-9999))>1e-7; )
            ad[i++] = x;
        return i;
    }
    int mysum( int *ai, int ni ){
        int sum = 0;
        for(int i=0; i<ni; ++i)
            sum += ai[i];
        return sum;
    }
    double mysum(double *ad, int nd ){
        double sum = 0.0;
        for(int i=0; i<nd; ++i)
            sum += ad[i];
        return sum;
    }
    View Code

      10、交换两个元素值的重载函数

    #include <iostream>
    //#include <cstring>
    using namespace std;
    
    void swap( int &a, int &b);
    void swap( double &a, double &b);
    void swap ( int *a, int n, int *b, int m);
    void swap ( char *a, char *b);
    void print(int *arr, int n);
    
    int main()
    {
        int a, b;
        double da, db;
        int aa[100], ab[100];
        char s1[100], s2[100];
        int i, n, m;
        cin >> a >> b;
        cin >> da >> db;
        cin >> n;
        for(i=0; i<n; i++){
            cin >> aa[i];
        }
        cin >> m;
        for(i=0; i<m; i++){
            cin >> ab[i];
        }
        cin >> s1 >> s2;
        swap( a, b );
        swap( da, db );
        swap( aa, n, ab, m );
        swap( s1, s2);
        
        cout << a << " " << b << endl;
        cout << da << " " << db << endl;
        print( aa, m ); //交换
        print( ab, n );
        cout << s1 << " " << s2 << endl;
        return 0;
    } 
    
    void swap( int &a, int &b){
        int t = a;
        a = b;
        b = t;
    }
    
    void swap( double &a, double &b){
        double t = a;
        a = b;
        b = t;
    }
    
    void swap ( int *a, int n, int *b, int m){    
        int i;
        int count = n<m ? n : m;
        for(i=0; i<count; ++i){
            int t = a[i];
            a[i] = b[i];
            b[i] = t;
        }
        if(n<m){
            for(; i<m; ++i)
                a[i] = b[i];
        }
        if(n>m){
            for(; i<n; ++i)
                b[i] = a[i];
        }
    }
    
    void swap ( char *a, char *b){
        /* char *t = new char[100];
        strcpy( t, a );
        strcpy( a, b );
        strcpy( b, t );*/
        for(int i=0; a[i] || b[i]; ++i){
            char t = a[i];
            a[i] = b[i];
            b[i] = t;
        }
    }
    
    void print(int *arr, int n){
        for(int i=0; i<n; ++i){
            if(i) cout << " ";
            cout << arr[i];
        }
        cout << endl;
    }
    View Code

      第6周 中级练习

      1、递归插入排序

    #include <iostream>
    using namespace std;
    
    int input(int *a);
    void Insert( int *a, int n );  //插入
    void InsertSort( int *a, int n ); //递归插入
    void print(int *arr, int n);
    
    int main()
    {
        int data[100];
        int n;   
        n = input( data );
        InsertSort( data, n );
        print( data, n);
          
        return 0;
    }
    
    int input(int *a){
        int i = 0, x; // i = 0;
        for( ; cin >> x, x != -9999; )
            a[i++] = x;
        return i;
    }
    
    void print(int *arr, int n){
        for(int i=0; i<n; ++i){
            if(i) cout << " ";
            cout << arr[i];
        }
        cout << endl;
    }
    
    void Insert( int *a, int n )//把数组a的第n个数插入前n-1个数中
    {
        int i = n-1;    //插入前n-1个元素中
        int key = a[n]; //保存插入元素
        while( (i>=0) && (key<a[i]) )
        {
            a[i+1] = a[i]; //前面元素后移
            i--;
        }
        a[i+1] = key; //找到位置
    }
    
    void InsertSort( int *a, int n )
    {
        if( n>0 )
        {        
            InsertSort( a, n-1 ); // n 递减   
            Insert( a, n-1 );
        }
    }
    View Code

      2、递归求两个数的最大公因数

    #include <iostream>
    using namespace std;
    
    int gcd( int a, int b, int n ){
        if(n==1||a%n==0 && b%n==0)
            return n;        
        return gcd( a, b, n-1 );
    }
    
    int main()
    {
        int a, b;
        cin >> a >> b;
        int n = a<b ? a : b;
        cout << gcd( a, b, n ) << endl;
        return 0;
    }
    View Code

      3、全排列

    #include <iostream>
    using namespace std;
    
    void print(int array[], int end);
    void swap(int &a, int &b);
    void permute(int array[], int begin, int end);
    
    int main()
    {
        int array[100];
        int n;
        cin >> n;
        for(int i=0; i<n; ++i)
            array[i] = i+1;
        permute( array, 0, n-1 );
        return 0;
    }
    
    void permute(int array[], int begin, int end)
    {
        if (begin == end)
        {
            print(array, end);
        }
        else
        {
            for (int j = begin; j <= end; ++j)
            {
                swap(array[j], array[begin]);
                permute
                (array, begin+1, end); 
                swap(array[j], array[begin]); 
            }
        }
    }
    
    void print(int array[], int end)
    {
        for (int i = 0; i <= end; ++i)
        {
           if(i) cout << " ";
           cout << array[i]; 
        }
        cout << endl;
    }
    
    void swap(int &a, int &b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    View Code

      4、递归构造可重复字符串

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void solution(int pos, char *p, int m, int n)
    {
         if( pos == n ){
            cout << p << endl;
            return ;
         }
         for(int i='A'; i<'A'+m; i++)
         {
            p[pos] = i;
            solution( pos+1, p, m, n );
         }
    }
    
    int main()
    {
         int m, n;
         cin >> m >> n;
         char *p = new char [n+1]; 
         p[n]='';
         solution( 0, p, m, n );
         delete []p;
         return 0;
    }
    View Code

      5、自然数的拆分

    #include <iostream>
    using namespace std;
    
    int n;
    int a[10001] = {1};
    int print( int );
    int search( int s, int t );//回溯算法
    
    int main()
    {
       cin >> n;
       search( n, 1 );        //将要拆分的数n传递给s
       return 0;
    }
    
    int search(int s,int t)
    {
        int i;
        for(i=a[t-1]; i<=s; i++)
            if(i<=n)                //当前数i要大于等于前1位数,且等于n
            {
                a[t] = i;            //保存当前拆分的数i
                s -= i;              //s减去i,s的值继续拆分
                if(s==0) print(t);  //s==0  拆分结束输出结果
                else search( s, t+1 ); //s>0,继续递归
                s += i;               //回溯:加上拆分的数,以便产分所有可能的拆分
            }
        return 0;
    }
    
    int print(int t)
    {
       cout << n << "=";
       for(int i=1;i<=t;i++)
       {
            if(i==t)
            {
                cout << a[i];
                cout << endl;
            }
            else  cout << a[i] << "+";
       }
    }
    View Code

      第6周 编程作业

      1、递归猴子摘桃

    #include <iostream>
    using namespace std;
    
    int   monkeyandPeak(int k,int n);
    
    int main()
    {
        int n = 10;
        cin >> n;
        int count = monkeyandPeak(1,n);
        cout << count << endl;
    
        return 0;
    } 
    
    int monkeyandPeak(int k,int n){
        if(n==1) return k;    
        /* k = monkeyandPeak(2*(k+1),n-1);
        return k; */
        return monkeyandPeak(2*(k+1),n-1);
    }
    View Code

      2、编写内联函数求矩形的面积和周长

    #include <iostream>
    using namespace std;
    inline void fun(int length, int width){
        cout << length*width << " " << 2*(length+width) << endl;
    }
    int main()
    {
        int length, width;
        cin >> length >> width;
        fun(length, width);
        return 0;
    }
    View Code

      3、编写重载函数显示字符串

    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    
    void print_spaced(char* str);
    void print_spaced(string str);
    
    int main()
    {
        char str1[100];
        string str2;
        cin >> str1 >> str2;
        print_spaced( str1 );
        print_spaced( str2 );
        return 0;
    }
    
    void print_spaced(char* str){
        for(int i=0; i<strlen(str); ++i)
        {
            if(i) cout << " ";
            cout << str[i];
        }
        cout << endl;
    }
    
    void print_spaced(string str){
        for(int i=0; i<str.length(); ++i)
        {
            if(i) cout << " ";
            cout << str[i];
        }
        cout << endl;
    }
    View Code

      4、排序函数重载

    #include <iostream>
    using namespace std;
    
    void sort(int &a, int &b);
    void sort(int &a, int &b, int &c);
    void sort(int &a, int &b, int &c, int &d);
    
    void Insert( int *a, int n );
    void sort( int *a, int n );
    void print(int *arr, int n);
    
    int main()
    {
        int a,b,c,d;
        int data[100];
        int k,n,i;
    
        cin >> k;
        
        switch(k)
        {
        case 1:
            cin >> a >> b;
            sort( a, b );
            cout <<a << " " << b <<endl;
            break;
        case 2:
            cin >> a >> b >> c;
            sort( a, b, c);
            cout << a << " " << b << " " << c << endl;          
            break;      
        case 3:
            cin >> a >> b >> c >> d;
            sort( a, b, c, d );
            cout << a << " " << b << " " << c << " " << d << endl;
            break;  
        case 4:
            cin >> n;
            for(i=0; i<n; i++)
            {
                cin >> data[i];
            } 
            sort( data, n );
            print( data, n);
            break;      
        }
        return 0;
    }
    
    void sort(int &a, int &b){
        if(a < b)
        {
            int t = a;
            a = b; 
            b = t;      
        }
    }
    
    void sort(int &a, int &b, int &c){
        sort(a,b);
        sort(a,c);
        sort(b,c);
    }
    
    void sort(int &a, int &b, int &c, int &d){
        sort(a,b,c);
        sort(a,d);
        sort(b,d);
        sort(c,d);
    }
    
    void print(int *arr, int n){
        for(int i=0; i<n; ++i){
            if(i) cout << " ";
            cout << arr[i];
        }
        cout << endl;
    }
    
    void Insert( int *a, int n )//把数组a的第n个数插入前n-1个数中
    {
        int i = n-1;    //插入前n-1个元素中
        int key = a[n]; //保存插入元素
        while( (i>=0) && (key>a[i]) )
        {
            a[i+1] = a[i]; //前面元素后移
            i--;
        }
        a[i+1] = key; //找到位置
    }
    
    void sort( int *a, int n )
    {
        if( n>0 )
        {        
            sort( a, n-1 ); // n 递减   
            Insert( a, n-1 );
        }
    } 
    View Code

      5、编写递归函数来使字符串逆序

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void reverse(char *str, int i, int j){
        if( i < j ){
            char ch = str[i];
            str[i] = str[j];
            str[j] = ch;
            reverse(str, i+1, j-1);        
        }
    }
    
    int main()
    {
        char str[100];
        cin.getline(str,100);  
        int len = strlen(str);
        reverse(str,0,len-1);
        cout << str;
        return 0;
    }
    View Code

      第7周 基础练习

      1、两个数的排序

    #include <iostream>
    using namespace std;
    
    void sort(int *a, int *b){
        if( *a > *b ){
            int t = *a;
            *a = *b; 
            *b = t;
        }    
    }
    
    int main()
    {
        int a, b;
        cin >> a >> b;
        sort( &a, &b );
        cout << a << " " << b << endl;
        return 0;
    }
    View Code

      2、三个数的排序

    #include <iostream>
    using namespace std;
    
    void sort(int *a, int *b);
    void sort(int *a, int *b, int *c);
    
    int main()
    {
        int a, b, c;
        cin >> a >> b >> c;
        sort( &a, &b, &c );
        cout << a << " " << b << " " << c << endl;
        return 0;
    }
    
    void sort(int *a, int *b){
        if( *a > *b ){
            int t = *a;
            *a = *b; 
            *b = t;
        }    
    }
    void sort(int *a, int *b, int *c){
        sort(a,b);
        sort(a,c);
        sort(b,c);
    }
    View Code

      3、返回数组的统计值(最大、最小、平均值、标准差)

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int input(double *a);
    void statistics(double a[],int n,double *max,double *min,double *avg,double *stdev);
    
    int main()
    {
        double a[100];
        double max, min, avg, stdev;
        int n = input( a );
        statistics(a,n,&max,&min,&avg,&stdev);
        cout << max << " " << min << " " << avg << " " << stdev << endl;
        return 0;
    }
    
    int input(double *a){
        int n = 0;
        double x;
        for( ; cin >> x, fabs(x+9999)>1e-6; )
            a[n++] = x;
        return n;
    }
    
    void statistics(double a[],int n,double *max,double *min,double *avg,double *stdev){
        double sum1 = a[0], sum2 = 0.0;
        *max = *min = a[0];
        for(int i=1; i<n; ++i){
            if(a[i]>*max)
                *max = a[i];
            if(a[i]<*min)
                *min = a[i];
            sum1 += a[i];
        }
        *avg = sum1/n;
        for(int i=0; i<n; ++i){
            sum2 += (a[i]-*avg)*(a[i]-*avg);
        }
        *stdev = sqrt(1.0/n*sum2);
    }
    View Code

      4、通过指向函数的指针调用函数

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    double x2(double x);
    double mysin(double x);
    
    int main()
    {
        double (*f)(double);
        double x;
        cin >> x;
        
        f = x2;
        cout << f(x) << " ";
        f = mysin;
        cout << f(x) << endl;
        
        return 0;
    }
    
    double x2(double x){     
        return x*x;
    }
    
    double mysin(double x){     
        return 2*sin(2*3.14*2*x+3.14);
    }
    View Code

      5、计算任意一元函数值的通用函数

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    double x2(double x);
    double mysin(double x);
    double  anyfun(double (*f)(double),double x);
    
    int main()
    {
        double x;
        cin >> x;
        cout << anyfun(x2,x) << " ";
        cout << anyfun(mysin,x) << endl;
        return 0;
    }
    
    double x2(double x){     
        return x*x;
    }
    
    double mysin(double x){     
        return 2*sin(2*3.14*2*x+3.14);
    }
    
    double  anyfun(double (*f)(double),double x){
        return f(x);
    }
    View Code

      6、计算函数在指定区间的近似平均值

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    double funavg(double (*f)(double x), double a,double b,int n);
    
    int main()
    {
        const int n = 1000;
        double a, b;
        cin >> a >> b;
        cout << funavg( exp, a, b, n ) << " ";
        cout << funavg( sin, a, b, n ) << " ";
        cout << funavg( cos, a, b, n ) << endl;
        return 0;
    }
    
    double funavg(double (*f)(double x), double a,double b,int n){
        double h = (b-a)/n, sum = f(a);
        double x = a;
        for(int i=1; fabs(b-x)>1e-6; ++i){
            x = a+i*h;
            sum += f(x);    
        }
        return sum/(n+1);
    }
    View Code

      第7周 中级练习

      1、指针实现向量的内积计算

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int input(double *a);
    double solution(double *a, double *b, int n);
    
    int main()
    {
        double a[100], b[100];
        int n = input( a );
        input( b );
        cout << solution( a, b, n ) << endl;
        return 0;
    }
    
    int input(double *a){
        double *t = a, x;
        for( ; cin >> x, fabs(x+9999)>1e-6; )
            *a++ = x;
        return a - t;
    }
    
    double solution(double *a, double *b, int n){
        double sum = 0.0;
        for(int i=0; i<n; ++i)
            sum += (*a++) * (*b++);
        return sum;
    }
    View Code

      2、使用指针的插入排序

    #include <iostream>
    using namespace std;
    
    int input(int *a);
    void InsertSort(int *arr, int n);
    
    int main()
    {
        int a[100];
        int n = input( a );
        InsertSort( a, n );
        for(int i=0; i<n; ++i){
            if(i) cout << " ";
            cout << *(a+i);
        }
        return 0;
    }
    
    int input(int *a){
        int *t = a, x;
        for( ; cin >> x, x != -9999; )
            *a++ = x;
        return a - t;
    }
    
    void InsertSort(int *arr, int n)
    {
        int i;
        for(i=1; i<n; ++i){/*第0个元素有序,从第1个元素向右无序*/
            int j=i-1,key=*(arr+i);/*保存第i个元素,左边的元素i-1*/
            while(j>=0 && key<*(arr+j)){/*保存的元素key与之前的元素从右向左逐个比较*/
                *(arr+j+1)=*(arr+j);/*移动(向后赋值)*/
                j--;
            }
            *(arr+j+1)=key;/*j--退出,恢复正确值j+1*/
        }
    }
    View Code

      3、指针实现成绩排序

    #include <iostream>
    using namespace std;
    
    struct Student{
        int no;
        int achievement;
    };
    
    int input(Student *s);
    void BubbleSort(Student *a,int left, int right);
    
    int main()
    {
        Student s[100];
        int n = input( s );
        BubbleSort( s, 0, n-1 );
        for(int i=0; i<n; ++i){
            cout << (s+i)->no << " " << (s+i)->achievement << endl;
        }
        return 0;
    }
    
    int input(Student *s){
        Student *t = s;
        int x, y;
        for( ; cin >> x >> y, x != 0; ){
            s->no = x;
            s->achievement = y;
            s++;
        }        
        return s - t;
    }
    /*冒泡排序--递归*/
    void BubbleSort(Student *a,int left, int right)
    {
        if(left<right){
            int j;
            Student t; 
            for(j=right; left<j; j--){
                if((a+j-1)->achievement>(a+j)->achievement)/*相邻比较*/ 
                    t=*(a+j),*(a+j)=*(a+j-1),*(a+j-1)=t;  
            }
            BubbleSort(a,j+1,right);/*递归*/
        }
    }
    View Code

      4、计算函数在某点的近似导数

    #include <iostream>
    #include <cmath>
    using namespace std;
    #define DELTA 0.001  //德尔塔
    
    double solution(double (*fun)(double x), double x){     
        return ((*fun)(x+DELTA)-(*fun)(x-DELTA))/(2*DELTA); //导数公式
    }
     
    int main()
    {
        double x;
        cin >> x;
        
        cout << solution(sin,x) << " ";
        cout << solution(cos,x) << " ";
        cout << solution(sin,x)+solution(cos,x) << endl;
        
        return 0;
    }
    View Code

      5、计算函数在指定区间的近似积分

    #include <iostream>
    #include <cmath>
    using namespace std;
    #define N 100  // δ = (b-a)/n, n=100
    
    double solution(double (*fun)(double x), double a, double b){ 
        double sum = 0.0;
        for(int i=0; i<N; ++i)
            sum += fun(a+i*(b-a)/N);
        return sum*(b-a)/N;
    }
     
    int main()
    {
        double a, b;
        cin >> a >> b;
        
        cout << solution(sin,a,b) << " ";
        cout << solution(cos,a,b) << " ";
        cout << solution(sin,a,b)+solution(cos,a,b) << endl;
        
        return 0;
    }
    View Code

      第7周 编程作业

      1、编写函数重置两个变量的值

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void reset(int *a, int *b);
    
    int main()
    {
        int a, b;
        cin >> a >> b;
        reset( &a, &b );
        cout << a << " " << b << endl;
        return 0;
    }
    
    void reset(int *a, int *b){
        *a = (*a+*b)/2.0 + 0.5;
        *b = *a;
    }
    View Code

      2、编写函数对数组中的元素求和

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int input( int *array);
    void add_array(int a, int *sum);
    
    int main()
    {
        int array[100], sum = 0;
        int n = input( array );
        for(int i=0; i<n; ++i)
            add_array(array[i], &sum);
        cout << sum << endl;
        return 0;
    }
    
    int input( int *array){
        int x, i = 0;
        for( ; cin >> x, x != -1; )
            array[i++] = x; 
        return i;
    }
    
    void add_array(int a, int *sum){
        *sum += a;
    }
    View Code

      3、数组清零

    #include <iostream>
    using namespace std;
    
    int input( int *array);
    void solution( int *array, int n);
    void output(int *p, int count);
    
    int main()
    {
        int a[100], * p= a, n;
        int count = input( p );
        cin >> n;
        solution( p, n );
        output(p, count);
        return 0;
    }
    
    void solution( int *array, int n){
        for( int i=0; i<n; ++i )
            array[i] = 0;
    }
    
    int input( int *array){
        int x, i = 0;
        for( ; cin >> x, x != -1; )
            array[i++] = x; 
        return i;
    }
    
    void output(int *p, int count){
        for(int i=0; i<count; ++i)
        {
            if(i) cout << " ";
            cout << p[i];
        }
        cout << endl;
    }
    View Code

      4、使用函数指针切换加密方法

    #include <iostream>
    using namespace std;
    
    void caesar(char s[]);
    void oddeven(char s[]);
    void cipher(void (*f)(char s[]),char s[]);//形参为指向函数的指针,对应实参可为相应格式的函数名。
    
    int main()
    {
        char str[256];
        int n;
        cin >> str >> n;
        switch(n){
            case 1: cipher( caesar, str ); break;
            case 2: cipher( oddeven, str ); break;
        }
        return 0;
    }
    
    void caesar(char s[]){
        for(int i=0; s[i]; ++i)
        {        
            char ch = 'a';    
            if(s[i]>='a' && s[i]<='z'){
                s[i] -= 32; //转大写
                ch = 'A';
            }
            else{
                s[i] += 32; //转小写
            }
            s[i] = ( s[i] + 3 - ch ) % 26 + ch;
        }
        cout << s << endl;
    }
    void oddeven(char s[]){
        char str[256], index1 = 0, index2 = 0, i;
        for(i=0; s[i]; ++i){
            if(i%2) //下标为奇数的保存到str
                str[index1++] = s[i];
            else    //下标为偶数的保存到s
                s[index2++] = s[i];
        }
        for(i=0; i<index1; ++i)
            s[index2++] = str[i]; //把奇数的复制到s
        cout << s << endl;
    }
    
    void cipher(void (*f)(char s[]),char s[]){
        f(s);
    }
    View Code

      5、编写求函数区间平均值的通用函数

    #include <iostream>
    using namespace std;
    
    int a, b, c, m;
    int  func1(int x);
    int  func2(int x);
    int avg( int (*f)(int),int x1,int x2);
    
    int main()
    {
        int x1, x2;
        cin >> a >> b >> c;
        cin >> m;
        cin >> x1 >> x2;
        cout << avg( func1, x1, x2 ) << endl;
        cout << avg( func2, x1, x2 ) << endl; 
        return 0;
    }
    
    int  func1(int x){
        return a*x*x+b*x+c;
    }
    int  func2(int x){
        int ret = 1;
        for(int i=0; i<m; ++i)
            ret *= x;
        return ret;
    }
    
    int avg( int (*f)(int),int x1,int x2){
        int sum = 0; 
        for(int i=x1; i<=x2; ++i){  //x1,x2 区间 
            sum += f(i);
        }
        return sum/(x2-x1+1);
    }
    View Code

      第8周 基础练习

      1、使用指针输出数组元素

    #include <iostream>
    using namespace std;
    int main()
    {
        const int N = 20;
        int *arr = new int[N]; //没有判断申请空间是否成功
        
        int *p = arr;
        int x, i = 0;
        for( ; cin >> x, x != 9999 && i<N ; i++ )
            *p++ = x;
        
        p = arr;
        
        /* for(int j=0; j<i; ++j)
        {
            if(j) cout << " "; //判断若干次
            cout << *p++;
        } 
        cout << endl;*/
        
        if(i) {  //判断一次
            cout << *p++;
            while(--i) 
                cout << " " << *p++;
            cout << endl;
        }
         return 0;
    }
    View Code

      2、通过指针输入输出数组元素的函数

    #include <iostream>
    using namespace std;
    
    int input(int *p);          //输入,返回输入的元素个数
    void print(int *p,int n);   //显示数组元素,n为元素个数
    
    int main()
    {
        const int N = 100;
        int *arr = new int[N];
        
        int n = input( arr );
        print( arr, n );
        
         return 0;
    }
    
    int input(int *p){
        int x, i = 0; 
        for( ; cin >> x, x != 9999; ++i )
            *p++ = x;
        return i;
    }
    
    void print(int *p,int n){
        if(n) { 
            cout << *p++;
            while(--n) 
                cout << " " << *p++;
            cout << endl;
        }
    }
    View Code

      3、指针实现字符串复制函数

    #include <iostream>
    using namespace std;
    
    char * mystrcpy(char *s1,char *s2); //将s2中的内容复制到s1中,返回s1首地址
    
    int main()
    {
        char s1[100], s2[100];
        cin >> s2;
        cout << mystrcpy(s1,s2) << endl;
         return 0;
    }
    
    char * mystrcpy(char *s1,char *s2){
        char *s = s1;
        while( *s1++ = *s2++ );
        return s;
    }
    View Code

      4、指针实现字符串比较,不区分大小写

    #include <iostream>
    using namespace std;
    
    int mystrcmp(char *s1,char *s2);
    
    int main()
    {
        char s1[100], s2[100];
        cin >> s1 >> s2;
        cout << mystrcmp(s1,s2) << endl;
         return 0;
    }
    
    int mystrcmp(char *s1,char *s2)
    {
        int ch1, ch2, ret = 0;
        do
        {
            if ( ((ch1 = (unsigned char)(*(s1++))) >= 'A') &&(ch1 <= 'Z') )
                ch1 += 0x20;
            if ( ((ch2 = (unsigned char)(*(s2++))) >= 'A') &&(ch2 <= 'Z') )
                ch2 += 0x20;
        } while ( ch1 && (ch1 == ch2) );
    
        if ( ch1-ch2 < 0)
            ret = -1 ;
        else if ( ch1-ch2 > 0 )
            ret = 1 ;
        return ret;    
    }
    View Code

      5、通过指针访问结构体变量

    #include <iostream>
    using namespace std;
    struct PERSON{
        char name[40], gender[10];
        int age;
    };
    int main()
    {
        PERSON one;
        PERSON *p = &one;
        
        cin >> p->name >> p->gender >> p->age;
        
        cout << p->name << " " << p->gender << " " << p->age << endl;
        
         return 0;
    }
    View Code

      6、通过指针访问结构体数组

    #include <iostream>
    #include <cstring>
    using namespace std;
    struct PERSON{
        char name[40], gender[10];
        int age;
    };
    int main()
    {
        PERSON aclass[50];
        PERSON *p = aclass;
        int n = 0; //
        
        cin >> p->name >> p->gender >> p->age;
        while(strcmp(p->name,"0") && strcmp(p->gender,"0") && p->age)
        {
            n++;
            p++;
            cin >> p->name >> p->gender >> p->age;
        }
        
        p = aclass+n-1;
        while( p>=aclass ){
            cout << p->name << " " << p->gender << " " << p->age << endl;
            p--;
        }
        
         return 0;
    }
    View Code

      7、动态申请变量,别忘了释放内存

    #include <iostream>
    using namespace std;
    int main()
    {
        int *a = new int;
        int *b = new int ;
        int *c = new int;
        
        cin >> *a >> *b >> *c;
        cout << (*a+*b+*c) << endl;
        delete a, b, c;
        
         return 0;
    }
    View Code

      8、动态申请数组

    #include <iostream>
    using namespace std;
    int main()
    {
        double x, *p, *q;
        int n, k = 0;
        
        cin >> n;
        q = p = new double[n];
        
        for( ; cin >> x, x != 9999; k++)
            *q++ = x;
        
        q = p+k-1;
        cout << *q--;
        while( q>=p )
            cout << " " << *q--;
        cout << endl;
        delete []p;
        
         return 0;
    }
    View Code

      9、动态申请结构体数组

    #include <iostream>
    #include <cstring>
    using namespace std;
    struct PERSON{
        char name[40], gender[10];
        int age;
    };
    int main()
    {
        int i, n;
        PERSON *t, *p;
        cin >> n;
        t = p = new PERSON[n];
        
        cin >> t->name >> t->gender >> t->age;
        for( i=0; strcmp(t->name,"0") && strcmp(t->gender,"0") && t->age; ++i ){
            t++; //开始输入的不是 "0", t++ , 再次输入
            cin >> t->name >> t->gender >> t->age;
        }
    
        t = p+i-1;
        while( t>=p ){
            cout << t->name << " " << t->gender << " " << t->age << endl;
            t--;
        }
        
        delete []p;
         return 0;
    }
    View Code

      10、动态申请结构体数组空间和结构体数组复制

    #include <iostream>
    #include <cstring>
    using namespace std;
    struct PERSON{
        char name[40], gender[10];
        int age;
    };
    int main()
    {
        int i, n;
        PERSON *t, *p;
        cin >> n;
        t = p = new PERSON[n];
        
        cin >> t->name >> t->gender >> t->age;
        for( i=0; strcmp(t->name,"0") && strcmp(t->gender,"0") && t->age; ++i ){
            t++; //开始输入的不是 "0", t++ , 再次输入
            cin >> t->name >> t->gender >> t->age;
        }
        
        t = p+i-1;
        PERSON *t1, *p1;
        t1 = p1 = new PERSON[i];
        while(t>=p) *t1++ = *t--;
        
        t1 = p1;     //p1已经是逆序的
        while( t1<p1+i ){ //不越界, 小于p1+i
            cout << t1->name << " " << t1->gender << " " << t1->age << endl;
            t1++;
        }
        
        delete []p;
        delete []p1;
         return 0;
    }
    View Code

      第8周 中级练习

      1、IP地址转换1

    #include <iostream>
    using namespace std;
    
    void InitBinStr(char *BinStr);
    void NumToBinStr(char *BinStr, int n, int index);
    void solution( const char *IpStr, char *BinStr );
    
    int main()
    {
        char IpStr[33],BinStr[33];
        cin >> IpStr;
        
        InitBinStr( BinStr );
        solution( IpStr, BinStr );
        
         return 0;
    }
    
    void InitBinStr(char *BinStr){ 
        char *p = BinStr; //为了更好理解 其实形参可以直接使用p
        for(int i=0; i<32; ++i) //赋值
            *p++ = '0';
        *p = '';  //保存二进制的字符数组,最后有个字符串结束符
    }
    
    void NumToBinStr(char *BinStr, int n, int index){
        while(n){
            BinStr[index--] = n%2 + '0';
            n /= 2;
        }
    }
    
    void solution( const char *IpStr, char *BinStr ){
        const char *p = IpStr; // 为了更容易书写
        for(int n=0,index=7; index<32; p++ )
        {    
            if(*p && *p != '.')//读数字
            {
                n = n*10 + *p - '0'; 
                continue;    
            }            
            NumToBinStr(BinStr,n,index);//遇到'.'或字符串读完 转换二进制
            index += 8;    //步长8
            n = 0;      //数字归零
        }
        cout << BinStr << endl;    
    }
    View Code

      2、IP地址转换2

    #include <iostream>
    using namespace std;
    
    int solution( const char * );
    
    int main()
    {
        char BinStr[33];
        cin >> BinStr;
    
        cout << solution( BinStr ); 
        for(int i=8; i<32; i+=8)
            cout << "." << solution( BinStr+i ); 
        
        return 0;
    }
    
    int solution( const char *p ){   
        int t = 8, n = 0;
        while(t--) 
            n += (*p++ - '0') << t;    
        return n;
    }
    View Code
    #include <iostream>
    using namespace std;
    
    void solution( const char *BinStr );
    
    int main()
    {
        char BinStr[33];
        cin >> BinStr;
    
        solution( BinStr ); 
    
        return 0;
    }
    
    void solution( const char *BinStr ){
        const char *p = BinStr; // 为了更容易书写
        for(int n=0,t=8,cnt=0; cnt<4; p++ )
        {    
            if(t--)//读数字
            {
                n += (*p - '0') << t;
                continue;    
            }
            if(cnt) cout << ".";
            cout << n;
            cnt++;
            p--;    //t==0的时候p多++一次
            t = 8;
            n = 0;      //数字归零     
        }   
    }
    View Code
    #include <iostream>
    using namespace std;
    
    void solution( char *IpStr, const char *BinStr );
    
    int main()
    {
        char IpStr[33],BinStr[33];
        cin >> BinStr;
    
        solution( IpStr, BinStr ); 
    
        return 0;
    }
    
    void NumToIpStr(char *IpStr,int n)
    {
        static int index = 0;
        static int cnt = 0;
        
        if(n==0){
            IpStr[index++] = '0';
        }
          
        char tmp[3];
        int i = 0;
        while(n){        
            tmp[i++] = n%10 + '0';
            n /= 10;
        }
        
        while(i--)
            IpStr[index++] = tmp[i];
        
        if(++cnt<4){
            IpStr[index++] = '.';
        }
        else{
            IpStr[index] = '';
        }    
    }
    
    void solution( char *IpStr, const char *BinStr ){
        const char *p = BinStr; // 为了更容易书写
        for(int n=0,t=8,cnt=0; cnt<4; p++ )
        {    
            if(t--)//读数字
            {
                n += (*p - '0') << t;
                continue;    
            }
            NumToIpStr(IpStr,n);
            cnt++;
            p--;    //t==0的时候p多++一次
            t = 8;
            n = 0;      //数字归零     
        }   
        cout << IpStr << endl;    
    }
    View Code

      3、找数据

    #include <iostream>
    using namespace std;
    
    void solution(char *p)
    {
        int i, index = 0;    
        int flag = 0, sum = 0, sign = 1;
        int dot = 0;
        
        for(i=0; ; ++i)
        {
            if( p[i]>='0' && p[i]<='9'|| p[i]=='.' )
            { 
                if( p[i] == '.' ){
                    dot = 1;
                    continue;
                }
                if(dot){ //记录小数点之后的位数
                    dot *= 10;
                }
                sum = sum*10 + p[i] - '0';
                if( i && p[i-1] == '-' )
                    sign = -1;
                flag = 1;
                continue;
            }    
            if(flag)
            {   
                if(dot){
                    cout << 1.0*(sum*sign)/dot+9 << endl;
                }
                else{
                    cout << sum*sign+9 << endl;
                }
                sign = 1; //符号置 1
                sum = 0;  //sum置 0
                flag = 0; //数字标记置 0
                dot = 0;
            }         
            if(!p[i]) 
                break;  //最后是数字可以输出
        }
    }
    
    int main()
    {
        char str[256];
        cin.getline(str,256);    
        solution(str);
        return 0;
    }
    View Code
    #include <iostream>
    using namespace std;
    
    struct IntAndDoubleArr{
        bool tag;
        union{
            double num1;
            int num2;
        }u;
    };
    
    void solution(char *p)
    {
        IntAndDoubleArr arr[256];
        int i, index = 0;    
        int flag = 0, sum = 0, sign = 1;
        int dot = 0;
        
        for(i=0;  ; ++i)
        {
            if( p[i]>='0' && p[i]<='9'|| p[i]=='.' )
            { 
                if( p[i] == '.' ){
                    dot = 1;
                    continue;
                }
                if(dot){ //记录小数点之后的位数
                    dot *= 10;
                }
                sum = sum*10 + p[i] - '0';
                if( i && p[i-1] == '-' )
                    sign = -1;
                flag = 1;
                continue;
            }    
            if(flag)
            {   
                if(dot){
                    arr[index].tag = true;
                    arr[index++].u.num1 = 1.0*(sum*sign)/dot+9;
                }
                else{
                    arr[index].tag = false;
                    arr[index++].u.num2 = sum*sign+9;
                }    
                sign = 1; //符号置 1
                sum = 0;  //sum置 0
                flag = 0; //数字标记置 0
                dot = 0;
            }
            if(!p[i])
                break;            
        }
        
        for(i=0; i<index; ++i)
        {
            if(arr[i].tag)
                cout << arr[i].u.num1 << endl; 
            else
                cout << arr[i].u.num2 << endl;    
        }
    }
    
    int main()
    {
        char str[256];
        cin.getline(str,256);    
        solution(str);
        return 0;
    }
    View Code

      4、指针实现矩阵相加

    #include <iostream>
    using namespace std;
    
    void solution( int *, int *, int, int );
    
    int main()
    {
        int i, j, k, index;
        cin >> i >> j;
        int *arr1 = new int[i*j];
        int *arr2 = new int[i*j];
        
        for( k=0,index=0; k<i*j; ++k )
            cin >> arr1[index++];
        for( k=0,index=0; k<i*j; ++k )
            cin >> arr2[index++];
        
        solution( arr1, arr2, i, j );
            
        return 0;
    }
    
    void solution( int *arr1, int *arr2, int i, int j)
    {
        for( int k=0; k<i*j; ++k )
        {
            if( k && k%j==0 ) cout << endl;
            else if( k && k%j) cout << " ";
            cout << (*arr1++) + (*arr2++);
        }
        cout << endl;
    }
    View Code

      5、指针实现矩阵相乘

    #include <iostream>
    using namespace std;
    
    /*** 
        1、当矩阵A的列数(column)等于矩阵B的行数(row)时,A与B可以相乘;
        2、矩阵C的行数等于矩阵A的行数,C的列数等于B的列数;
        3、乘积C的第m行第n列的元素等于矩阵A的第m行的元素与矩阵B的第n列对应元素乘积之和; 
    ***/
    
    void solution( int *, int *, int, int, int );
    
    int main()
    {
        int row1, column1, k, index;
        cin >> row1 >> column1;
        int *arr1 = new int[row1*column1];
        
        for( k=0,index=0; k<row1*column1; ++k )
            cin >> arr1[index++];
        
        int row2, column2;
        cin >> row2 >> column2;
        int *arr2 = new int[row2*column2];
        
        for( k=0,index=0; k<row2*column2; ++k )
            cin >> arr2[index++];
        
        //solution( arr1, arr2, row1, column1, row2, column2 ); //row2==column1
        solution( arr1, arr2, row1, column1, column2 );
            
        return 0;
    }
    
    void solution( int *arr1, int *arr2, int row1, int column1, int column2 )                               
    {
        int *t1, *t2, k, row, column, sum = 0;  
        for( row=0; row<row1; row++ ) // row1
        {
            for( column=0; column<column2; column++ ) // column2
            {      
                t1 = arr1 + row * column1;
                t2 = arr2 + column;
                sum = 0;
    
                for( k=0; k<column1; k++ ) 
                {
                    sum += (*t1) * (*t2); 
                    t1 += 1;           //arr1数组的行
                    t2 += column2;     //arr2数组的列 
                }
                
                if(column) cout << " ";
                cout << sum; 
            }
            cout << endl;
        }
    }
    View Code

      第8周 编程作业

      1、输出数字的英文名称

    #include <iostream>
    using namespace std;
    
    const char * digitName(int n);
    
    int main()
    {
        int n;
        cin >> n;
        cout << digitName( n ) << endl;
            
        return 0;
    }
    
    const char * digitName(int n){
        const char *digitName[] = {"zero","one", "two", "three", "four", "five", "six", 
                                   "seven", "eight", "nine", "ten", "eleven", "twelve"};
        return  digitName[n];
    }
    View Code

      2、去除字符串首尾多余的空格

    #include <iostream>
    using namespace std;
    int main( )
    {
        char str[100];
        cin.get(str,100);
        
        char *p = str, *q = str;
        while( *p != '#' ) p++;
        while( *--p == ' ' );
        *(p+1) = '#';
        *(p+2) = '';
        
        p = str; 
        while( *p == ' ') p++;
        while( *q++ = *p++);
        
        cout << str << endl;
        return 0;
    }
    View Code

      3、遍历二维数组

    #include <iostream>
    using namespace std;
    int main()
    {   
        int **a;  //指向指针的指针 
        int n, m;//n行 m列 
        int i, j;
        int sum = 0;
        cin >> n >> m; //输入行数和列数
     
        //申请空间
        a = new int * [n];  //n个 int 指针 数组
        for( i=0; i<n; i++ )    //n个大小为m的一维数组
        {
            a[i] = new int[m]; //1个大小为m的一维数组,a[i]是int指针
        } 
     
        //输入数据
        for( i=0; i<n; i++ )
        {
            for( j=0; j<m; j++ )
            {
                //cin>>a[i][j];//输入 
                cin>> *(*(a+i)+j);//同上a相当于行指针 
                sum += *(*(a+i)+j);
            }
        }
     
        cout << sum << endl;
        
        /*  //输出数据
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                //cout<<a[i][j]<<"	";//输出 
                cout<< *(*(a+i)+j)<<"	";
            }
            cout<<endl;
        } */
        
        //释放申请的空间!!!
        for(i=0;i<n;i++) //释放n个大小为m的一维数组
        {
            delete []a[i]; 
        }
        delete []a;//释放int指针数组
        
        return 0;
    }
    View Code

      4、动态申请大数组

    #include <iostream>
    using namespace std;
    
    int *new_array(int n); 
    void init_array(int *p, int n,int c); 
    
    int main()
    {   
        int n, c;
        cin >> n >> c;
        int *p = new_array( n );
        init_array( p, n, c);
        for( int i=0; i<n; ++i )
        {
            if(i) cout << " ";
            cout << *p;
        }
        delete []p;
        return 0;
    }
    
    int *new_array(int n){
        int *p = new int[n];
        return p;
    }
    
    void init_array(int *p, int n,int c){
        for( int i=0; i<n; ++i )
            *p++ = c;
    }
    View Code

      5、矩阵对角线元素之和

    #include <iostream>
    using namespace std;
    int main()
    {   
        int n, sum = 0;
        cin >> n;
        int *p = new int[n*n];
        
        for( int i=0; i<n*n; ++i )
        {
            cin >> *p;
            if(i%(n+1)==0) sum += *p;
        }
        
        cout << sum << endl;
        delete []p;
        
        return 0;
    }
    View Code

      6、十进制点分IP转换为32位二进制IP,IP合法性是没有连续的 '.'

    #include <iostream>
    using namespace std;
    
    void InitBinStr(char *BinStr);
    void NumToBinStr(char *BinStr, int n, int index);
    void solution( const char *IpStr, char *BinStr );
    
    int main()
    {
        char IpStr[33],BinStr[33];
        cin >> IpStr;
        
        InitBinStr( BinStr );
        solution( IpStr, BinStr );
        
         return 0;
    }
    
    void InitBinStr(char *BinStr){ 
        char *p = BinStr; //为了更好理解 其实形参可以直接使用p
        for(int i=0; i<32; ++i) //赋值
            *p++ = '0';
        *p = '';  //保存二进制的字符数组,最后有个字符串结束符
    }
    
    void NumToBinStr(char *BinStr, int n, int index){
        while(n){
            BinStr[index--] = n%2 + '0';
            n /= 2;
        }
    }
    
    void solution( const char *IpStr, char *BinStr ){
        const char *p = IpStr; // 为了更容易书写
        //for(int n=0,index=7,flag=0; index<32; p++ )
        for(int n=0,index=7; index<32; p++ )
        {        
            if(*p>='0' && *p<='9') //是数字
            {
                n = n*10 + *p - '0'; 
                //flag = 1;
                continue;    
            }
            
            if(*(p+1)=='.'){ //经过测试题目要求的合法性是没有连续的'.'
                cout<<"data error"<<endl;  
                return;
            }
            //还有数字是否超过255 字符串是否超短 超长的情况
            /* if(*p && *p!='.' || !flag || *(p+1)=='.'){ //不是数字也不是 '.' 或第一个就是'.' 或连续的'.'
                cout<<"data error"<<endl;  
                return;
            }  */
            
            NumToBinStr(BinStr,n,index);//遇到'.'或字符串读完 转换二进制
            index += 8;    //步长8
            n = 0;      //数字归零
        }
        /* if(*(p-1)){  //字符串超长
            cout<<"data error"<<endl;  
            return;
        } */
        cout << BinStr << endl;    
    }
    View Code
    #include <iostream>
    using namespace std;
    
    void InitBinStr(char *BinStr);
    void NumToBinStr(char *BinStr, int n, int index);
    void solution( const char *IpStr, char *BinStr );
    
    int main()
    {
        char IpStr[33],BinStr[33];
        cin >> IpStr;
        
        InitBinStr( BinStr );
        solution( IpStr, BinStr );
        
         return 0;
    }
    
    void InitBinStr(char *BinStr){ 
        char *p = BinStr; //为了更好理解 其实形参可以直接使用p
        for(int i=0; i<32; ++i) //赋值
            *p++ = '0';
        *p = '';  //保存二进制的字符数组,最后有个字符串结束符
    }
    
    void NumToBinStr(char *BinStr, int n, int index){
        while(n){
            BinStr[index--] = n%2 + '0';
            n /= 2;
        }
    }
    
    void solution( const char *IpStr, char *BinStr ){
        const char *p = IpStr; // 为了更容易书写
        for(int n=0,index=7; index<32; p++ )
        {        
            if(*p>='0' && *p<='9') //是数字
            {
                n = n*10 + *p - '0'; 
                continue;    
            }
            
            if(*(p+1)=='.'){ //经过测试题目要求的合法性是没有连续的'.'
                cout<<"data error"<<endl;  
                return;
            }
            
            NumToBinStr(BinStr,n,index);//遇到'.'或字符串读完 转换二进制
            index += 8;    //步长8
            n = 0;      //数字归零
        }
        cout << BinStr << endl;    
    }
    View Code
  • 相关阅读:
    Selenium开发环境搭建
    如何抓取移动端崩溃日志?
    html+ashx + NPOI 实现导出Excel文件并且预览和下载
    oss 文件上传:Web端上传介绍
    事务控制和锁定语句
    索引的设计和使用
    最近几年读过的书籍
    053.NET5_EFCoreMigration
    052.NET5_EFCoreDbFirst
    051.NET5_中间件的多种引用方式
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/13552774.html
Copyright © 2020-2023  润新知