[進階程設] C++基礎

Posted on Thu, Sep 23, 2021 進階程設課程 C++

用高一資訊課的題目來複習C++

bmi計算
#include <iostream>

using namespace std;

int main()
{
    float kg,cm,bmi,b_kg;
    cout<<"cm"<<endl;
    cin>>cm;
    cout<<"kg"<<endl;
    cin>>kg;
    bmi = kg/((cm/100)*(cm/100));
    b_kg = 21*((cm/100)*(cm/100));
    cout<<"bmi "<<bmi<<endl;
    if(b_kg>kg) {cout<<"+"<<b_kg-kg<<"kg"<<endl;}
    else if(b_kg<kg) {cout<<"-"<<kg-b_kg<<"kg"<<endl;}
    else {cout<<"great"<<endl;}


    return 0;
}
#include <iostream>

using namespace std;

int main()
{
    int money=2000,spend;
    while(money>0){
        cout<<"ªáŠh€Ö"<<endl;
        cin>>spend;
        money-=spend;
        if(money>0){
        cout<<"³Ñ"<<money<<"€ž"<<endl;
        }
    }
    cout<<"no money  "<<money<<endl;
    return 0;
}
7科加權平均
#include <iostream>

using namespace std;

int main()
{
    int a[2][7],sum=0,i,val=0;
    float avg;
    cout<<"輸入7科成績"<<endl;
    for(i=0;i<7;i++){
        cin>>a[0][i];
    }
    cout<<"輸入7科權數"<<endl;
    for(i=0;i<7;i++){
        cin>>a[1][i];
	val+=a[1][i];
    }
    for(i=0;i<7;i++){
        sum+=a[0][i]*a[1][i];
    }
    avg=sum/val;
    cout<<"sum:"<<sum<<endl;
    cout<<"avg:"<<avg<<endl;
    return 0;
}
聖誕樹星星
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n-i;j++){
            cout<<" ";
        }
        for(int j=0;j<2*(i+1)-1;j++){
            cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}
手動猜數字
#include <iostream>

using namespace std;

int main()
{
    int ans,up=100,down=1,guess;
    cout<<"input code"<<endl;
    while(cin>>ans){
        if(ans>0&&ans<=100) break;
        else cout<<"out of range"<<endl;;
    }
    while(guess!=ans){
        cin>>guess;
        if(guess>ans){
            up=guess;
            cout<<"range: "<<down<<"~"<<up<<endl;
        }
        if(guess<ans){
            down=guess;
            cout<<"range: "<<down<<"~"<<up<<endl;
        }
    }
    cout<<"good"<<endl;
    return 0;
}