码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 笔记

时间:2017-07-21 22:04:49      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:lib   least   tput   c++   辗转相除法   生成   and   ons   tip   

辗转相除法

求最小公倍数

冒泡排序

 

1、辗转相除法求最大公约数,简写为gcd。应用:化简分数,即分子分母同时除以最大公约数。

#include <iostream>
#include <cstdio>
using namespace std;

int gcd(int, int);

int main()
{
    cout << gcd(6, 9) << endl; // output=3
    return 0; 
}

int gcd(int m, int n) {
    for (int r; (r=m%n) != 0; m=n, n=r);
    return n;
}

 

2、最小公倍数,least common multiple,简写为lcm。

#include <iostream>
#include <cstdio>
using namespace std;

int gcd(int, int);
int lcm(int, int);

int main()
{
    cout << gcd(6, 9) << endl; // output=3
    cout << lcm(6, 9) << endl; // output=18
    
    return 0; 
}

int gcd(int m, int n) {
    for (int r; (r=m%n) != 0; m=n, n=r);
    return n;
}

int lcm(int m, int n) {
    return m*n / gcd(m, n);
}

 

3、排序之对数字

#include <cstdio>
#include <cstdlib>
#include <ctime>
#define N 1000

void swap(int& a, int& b) {
    a = a + b;
    b = a - b;
    a = a - b;
}

void sort(int *arr, int len) {
    for (int i = 0; i != len-1; ++i) // len个元素的数组需要排(len-1)趟
        for (int j = 0; j != len-i-1; ++j) 
            if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
}

int arr[N];

int main()
{
    srand(time(0));

    for (int i = 0; i < N; i++)
        printf("%4d",arr[i]=rand()%100+1); // 生成N个1-100的随机整数。
    printf("\n");

    sort(arr, N);

    for (i = 0; i < N; ++i)
        printf("%4d", arr[i]);
    printf("\n");

    return 0;
}

 

#include <cstdio>
#include <cstdlib>
#include <ctime>
#define N 100

void swap(int &a, int &b) {
    a = a + b;
    b = a - b;
    a = a - b;
}

void sort(int *arr, int len) {
    for (int i = 0; i != len - 1; ++i) 
        for (int j = 0; j != len - i - 1; ++j)
            if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
}

int main()
{    
    /*
    int a = 3, b = 4;
    swap(a, b);
    printf("%d %d\n", a, b);
    */

    srand((int)time(0));

    int arr[N];

    for (int i = 0; i != N; ++i) {
        printf("%4d", arr[i] = rand()%100 + 1);
    }

    printf("\n");

    sort(arr, N);

    for (i = 0; i != N; ++i) {
        printf("%4d", arr[i]);
    }

    printf("\n");

    return 0;
}

 

排序对象, c++创建对象的方式:http://blog.csdn.net/azhexg/article/details/14225545

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

#define N 10

/*

void swap(int &a, int &b) {
    a = a + b;
    b = a - b;
    a = a - b;
}

void sort(int *arr, int len) {
    for (int i = 0; i != len - 1; ++i) 
        for (int j = 0; j != len - i - 1; ++j)
            if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
}

*/

class Student {
friend ostream& print(ostream&, const Student&);
friend void sort(Student **arr, int len);

public:
    Student(const string& name, int score): name(name), score(score) {}

private:
    string name;
    int score;
};

void swap(Student* &left, Student* &right) {
    Student *temp;
    temp = left;
    left = right;
    right = temp;
}

void sort(Student **arr, int len) {
    for (int i = 0; i != len - 1; ++i) 
        for (int j = 0; j != len - i - 1; ++j)
            if (arr[j]->score > arr[j+1]->score) swap(arr[j], arr[j+1]);
}

ostream& print(ostream& os, const Student& stu) {
    os << stu.name << " " << stu.score;
    return os;
}

int main()
{    
    /*
    int a = 3, b = 4;
    swap(a, b);
    printf("%d %d\n", a, b);
    */

    srand((int)time(0));

    /*

    int arr[N];

    for (int i = 0; i != N; ++i) {
        printf("%4d", arr[i] = rand()%100 + 1);
    }

    printf("\n");

    sort(arr, N);

    for (i = 0; i != N; ++i) {
        printf("%4d", arr[i]);
    }

    printf("\n");

    */

    Student *stu[N];

    for (int i = 0; i != N; ++i) {
        print(cout, *(stu[i] = new Student("jack", rand()%100 + 1))) << endl;
    }

    printf("******************************************\n");

    sort(stu, N);

    for (i = 0; i != N; ++i) {
        print(cout, *stu[i]) << endl;
    }

    return 0;
}

 

c++ 笔记

标签:lib   least   tput   c++   辗转相除法   生成   and   ons   tip   

原文地址:http://www.cnblogs.com/xkxf/p/6928317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!