C++练习

#include <iostream>
using namespace std;

int main() {
    //eg1
    //编写程序,从键盘输入姓名,如“Kobe”,屏幕显示“Hello, Kobe!”。
    //重复输入不同的姓名XX 并显示“Hello, XX!”,直到输入“##”时结束。
    string name;
    while(cin>>name)
    {
        if(name!="##")
            cout<<"Hi!"<<name<<endl;
        else
            break;
    }
    
    //eg2 编写程序,打印1到9的阶梯
    const int max=10;
    cout<<endl;
    for(int num=1;num<max;num++)
    {
        for(int j=max-num;j>0;j--)
            cout<<" ";
        for(int j=1;j<=num;j++)
            cout<<num<<" ";
        cout<<endl;
    }
    //eg3 编写程序,打印100以内的素数
    const int max = 100;
    int i,j;
    for(i=2;i<=max;i++)
    {
        for(j=2;j<i;j++)
        {
            if(i%j)
                continue;
            else
                break;
        }
        if(i == j)
        {
            cout<<i<<" ";
        }
    }
    
    //eg4 编写程序,计算1!+2!+......+10!
    const int max = 10;
    long result = 0;
    long tempTotal = 1;
    for(int i = 1;i<=max;i++)
    {
        tempTotal *= i;
        result += tempTotal;
    }
    cout<<"1!+2!+......+10!="<<result<<endl;
    
    //eg5 编写程序 打印九九乘法表
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=i;j++)
        {
            cout<<j<<"*"<<i<<"="<<j*i<<"\t";
        }
        cout<<endl;
    }
    //eg6 编写程序 反向打印九九乘法表
    for(int i=9;i>=1;i--)
    {
        for(int k=9;k>i;k--)
        {
            cout<<"      "<<"\t";
        }
        for(int j=i;j>=1;j--)
        {
            cout<<j<<"*"<<i<<"="<<j*i<<"\t";
        }
        cout<<endl;
    }
    
    
    
    
    return 0;
}

 

posted @ 2017-09-13 18:13  云海畅游  阅读(136)  评论(0)    收藏  举报