C++程序题

1.判断是否是闰年(多行输入)

分析:是闰年的条件:year%4==0 && year%100 !=0 或者 year%400==0

#include <iostream>
using namespace std;
int main(){
    int year;
    bool isLeapYear;
    cout<<"Enter the year:"<<endl;
    cin>>year;
    while(year!=-1){
        isLeapYear = ((year%400==0)||(year%100!=0 && year%4==0 ));
        if(isLeapYear)
            cout<<year<<" is a leap year"<<endl;
        else
            cout<<year<<" is not a leap year"<<endl;
        cout<<"Enter the year:"<<endl;
        cin>>year;
    
    }
    return 0;
}

 2.输入一个整数,将各位数字反转后输出

分析:num%10求出低位,求出一位就立即输出一位。

#include <iostream>
using namespace std;
int main(){
    int num,temp;
    cout<<"Enter a number:"<<endl;
    cin>>num;
    while(num!=0){
        temp = num%10;        
        cout<<temp;        
        num /= 10;    
    }    
    cout<<endl;
    return 0;
}

 3.口袋中有红、黄、黑、白、蓝5种颜色的球若干个,每次从口袋中取出3个不同颜色的球,问有多少种取法?

分析:3层for循环

#include <iostream>
using namespace std;
enum Color {RED,YELLOW,BLUE,WHITE,BLACK};
int main(){
    int count=0;
    for(int i=RED;i<=BLACK;i++){
        for(int j=i+1;j<=BLACK;j++){
            for(int k=j+1;k<=BLACK;k++){
                if(i!=j && j!=k && k!=i){
                    cout<<i<<" "<<j<<" "<<k<<endl;
                    count++;
                }                    
            }
        }
    }                
    cout<<"总共有"<<count<<"种组合"<<endl; 
    return 0;
}

同类扩展题:递归解决从n个人中选择k个人组合成一个委员会的不同组合数。

分析:从n个人中选取k个人的组合数=从n-1个人里选k个人的组合数 + 从n-1个人里选 k-1 个人的组合数

#include <iostream>
using namespace std;
int comm(int n,int k){
    if(k>n) return 0;
    else if(k==n || k==0) return 1;
    else
        return comm(n-1,k)+comm(n-1,k-1);
}

int main(){
    int n,k;
    cout<<"Enter n,k:";
    cin>>n>>k;
    cout<<"从n中取k个人,有"<<comm(n,k)<<"种组合"<<endl;
    return 0;
}

4.输入一个8位二进制数,将其转换为二进制数输出。

分析:本题的关键是这个8位二进制是每输入一位就处理,其结果。

#include <iostream>
#include <math.h>
using namespace std;
int main(){
    int value = 0;
    cout<<"Enter a bit binary number:";
    for(int i=7;i>=0;i--){
        char ch;
        cin>>ch;
        if(ch=='1'){
            value+=(int)pow(2,i);
        }
    }
    cout<<"Decimal value is "<<value<<endl;
    return 0;
}

5.寻找并输出11--999之间的数m,它满足m,m2,m3,均为回文数。

例如:m=11,m2=121,m3=1331

分析:判读回文的方法,模10除以10,然后低位充当高位,按反序重新构成新的函数,再与原数比较是否相等。若等,则为回文数。

#include <iostream>
using namespace std;
//判断是否是回文
bool symm(unsigned n){
    unsigned i = n;
    unsigned m = 0;
    while(i>0){
        m = m*10+i%10;
        i/=10;
    }
    return m==n;
}
int main(){
    for(unsigned n=11;n<1000;n++){
        if(symm(n) && symm(n*n) && symm(n*n*n)){
            cout<<"n = "<<n;
            cout<<"\t n*n = "<<n*n;
            cout<<"\t n*n*n = "<<n*n*n<<endl;
        }
    }
    return 0;
}

 6.掷筛子的随机游戏

游戏规则:每轮投两次筛子,第一轮如果和为7或11则为胜,游戏结束。和为2,3或12则为负,则将此值作为自己的点数,继续第二轮、第三轮...直到某轮的和数等于点数则胜,若在此之前出现和为7则为负。

分析:首先掷筛子两次,使用rand() 产生,但若不为它设置种子,rand 总是默认种子为1。

#include <iostream>
using namespace std;

int rollDice(){
    int die1 = 1 + rand()%6;
    int die2 = 1 + rand()%6;
    int sum = die1 + die2;
    cout<<"player rolled "<<die1<<"+"<<die2<<"="<<sum<<endl;
    return sum;
}
enum GameStatus {WIN,LOSE,PLAYING};
int main(){
    int sum,myPoint;
    GameStatus status;
    unsigned seed;
    cout<<"Please enter an unsigned integer:";
    cin>>seed;
    srand(seed); //将种子传递给rand()
    
    sum = rollDice();
    switch(sum){
    case 7:
    case 11:
        status = WIN;
        break;
    case 2:
    case 3:
    case 12:
        status = LOSE;
        break;
    default:
        myPoint = sum;
        break;
    }
    while(status==PLAYING){
        sum = rollDice();
        if(sum==myPoint)
            status = WIN;
        else if(sum==7)
            status = LOSE;
    }
    if(status==WIN)
        cout<<"player wins"<<endl;
    else
        cout<<"player loses"<<endl;

    return 0;
}

 

posted @ 2014-09-28 15:12  Immanuely  阅读(222)  评论(0编辑  收藏  举报