close

數字排序與NxN乘法表


(1)寫出一個程式,”輸入四個數字,比較並輸出由大到小的順序”?
例:請輸入四個數字:21,2,34,77
大小順序為:77 > 34 > 21 > 2
(2)請撰寫一個程式,可以列印出N x N的乘法表?
例:請輸入乘法表大小?3
輸出:
1 23
1123
2246
3369

#include<iostream>
#include<stdlib.h>

using namespace std;

int main()
{
    int num[4];
    int tmp;
    cout << "輸入四個數字 ";
    for(int i=0;i<4;i++)
        cin >> num[i];
    for(int i=0;i<4-1;i++){
        for(int j=i+1;j<4;j++){
            if(num[i]<num[j]){
                tmp = num[i];
                num[i] = num[j];
                num[j] = tmp;
            }
        }
    }
    for(int i=0;i<4;i++){
        cout << num[i];
        if(i+1<4)
            cout << " > ";
    }
    system("pause");
    return 0;
}


#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
    int n;
    do{
        cout << "input n: ";
        cin >> n;
    }while(n<=0 || n>9);
   
    //印標頭
    for(int i=0;i<=n;i++){
        if(i!=0)
            cout << i;
        else
            cout << " ";
        cout << "t";
    }
    cout << endl;
   
    //印NxN乘法表
    for(int i=1;i<=n;i++){
        cout << i << "t";
        for(int j=1;j<=n;j++)
            cout << i*j << "t";
        cout << endl;
    }
    system("pause");
    return 0;
}

http://tw.knowledge.yahoo.com/question/question?qid=1106111507316


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 chph 的頭像
    chph

    Afutseng's Blog

    chph 發表在 痞客邦 留言(0) 人氣()