Coder Social home page Coder Social logo

happy-cplusplus's Introduction

happy-cplusplus

记录我用c/c++实现的程序

#感悟

#学习一门程序语言,需要获得,knowledge and skill,知识可以通过讲授传递,但技巧只能通过训练获得。 #所有复杂的问题,都是简单问题的组合。

#给你40分钟的时间,你可以思考十分钟,然后用三十分钟的时间来写代码,最后浪费在无谓的调试上;你也可以思考半个小时,彻底弄清问题的本质与程序的脉络,然后用十分钟的时间来编写代码

Q&A:(建议阅读相关程序后再看答案)

1.奇偶排序(1)和奇偶排序(2),哪一段程序更好呢?
答:奇偶排序(1),因为(1)的程序可以说是由若干个“模块"组成,
模块之内”高内聚“,模块之间”低耦合“。

2.B05_6 复杂度为n*3的算法,现在的是n*m。

3.B05_7 这是也一道经典的算法问题,在企业面试里出现概率很高,是“找到第K大的数”的变种。先排序再找中位数自然
是很直接的做法,但排序本身很慢。我们只想找到第n/2大的数,对于其他数的顺序我们并不关心。那么怎么在不
排序的前提下找到第n/2大的数呢?

happy-cplusplus's People

Contributors

quoniammm avatar

Watchers

 avatar  avatar

Forkers

huoxingyongren

happy-cplusplus's Issues

次幂取模

//因为如果a ≡ A (mod c)且b ≡ B (mod c),则a × b ≡ A × B (mod c)通过把a、b、A、B全写成
//nc+m(0 ≤ m < c)的形式,很容易证明这一点。
/*
因为mod有可乘性
设 a=mq+r, b=nq+s. 则
(a*b) mod q = (mnq^2+msq+nrq+rs) mod q = rs mod q = ((a mod q) * (b mod q)) mod q
*/
#include<iostream>
using namespace std;
int main()
{
    int a,b,mod;
    while(cin >> a >> b >> mod) {
        int ans = 1;
        while(b--) {
            ans = (ans*a)%mod;
        }
        cout << ans << endl;
    }
}

精度控制

#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
    float A,B,C,D;
    A=B=C=D=0;
    int n,a[1000];
    cin >> n;
    for(int i =0;i < n;i ++) {
        cin >> a[i];
    }
    for(int i = 0;i < n;i ++) {
        if(a[i] > 0 && a[i] < 19)
            A++;
        else if(a[i] >=19 && a[i] < 36)
            B++;
        else if(a[i] >=36 && a[i] < 61)
            C++;
        else if(a[i] >= 60)
            D++;
    }
        //实现精度控制
    cout <<  "1-18:  "   << setiosflags(ios::fixed) << setprecision(2) << A/n*100 << "%" << endl;
    cout <<  "19-35:  "   << B/n*100 << "%" << endl;
    cout <<  "36-60:  "   << C/n*100 << "%" << endl ;
    cout <<  "60-:  "   << D/n*100 << "%" << endl;
}

关注字符串输入的程序

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    char a[10][10];
    int n;
    cin >> n;
    cin.get(); //因为使用getline加的
//  for(int i = 0;i < n;i ++) {
//      cin >> a[i];
//  }
    for(int i = 0;i < n;i ++) {
        cin.getline(a[i], 10);
    }
    for(int i = 0;i < n;i ++) {
            cout <<  a[i] << endl;
    }
    return 0;
}
//Q:如果getline改为get会怎样?

筛选素数通过数组

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int sum = 0,a[100] = {0};
    for(int i = 2;i < sqrt(100);i ++){
        sum = i;
        if(a[sum] == 0) {
            while(sum < 100) {
                sum += i;
                if(sum < 100) a[sum] = 1;
            }
        }
    }
    for(int i = 2;i < 100;i ++) {
        if(a[i] == 0) cout << i << " ";
    }
    return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.