C++程序设计教程 第3版——习题四有关9-16题的个人解答

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

有关9-16题的个人代码解答


一、题目

二、代码解答

1.9、10、11、13

代码如下(示例):

9.#include<iostream>
using namespace std;
int main (void)
{
    int a=0,b=0,c=0,e=0,f=0,g=0,h=0;
    while(a<=6)
    {
        cout<<'*';
        a=a++;
    }
    cout<<endl;
    while(b<=6)
    {
        if (b==0||b==6) cout<<' ';
        else cout<<'*';
        b=b++;
    }
    cout<<endl;
    while(c<=6)
    {
        if (c>=2&&c<=4) cout<<'*';
        else cout<<' ';
        c=c++;
    }
    cout<<endl;
    while(e<=6)
    {
        if (e==3) cout<<'*';
        else cout<<' ';
        e=e++;
    }
    cout<<endl;
    while(f<=6)
    {    if (f>=2&&
    f<=4) cout<<'*';
        else cout<<' ';
        f=f++;
    }
    cout<<endl;
    while(g<=6)
    {    if (g==0||g==6) cout<<' ';
        else cout<<'*';
        g=g++;
    }
    cout<<endl;
    while(h<=6)
    {
        cout<<'*';
        h=h++;
    }
    cout<<endl;
    return 0;
}
10.#include<iostream>
using namespace std;
int main(void)
{
    int i,x,y,z;
    for(x=1;x<10;x=x+1)
    {
        for(y=0;y<10;y=y+1)
        {
            for(z=0;z<10;z=z+1)
            {
                if(x!=y&&y!=z&&x!=z) i=x*100+y*10+z;
                if(i%11==0) cout<<i<<endl;}
        }
    }
    return 0;
}
11.#include<iostream>
using namespace std;
int main (void)
{
    double a=1.0,b=2.0,c,sum=0;
    int k=1;
    while(k<=30)
    {
        c=b/a;
        a=b;
        b=b+1;
        sum=sum+c;
        k=k+1;
}
    cout<<sum<<endl;
     return 0;
}
13.#include<iostream>
using namespace std;
int main(void)
{
    double a=3,k=1;
    while(a<50)
    {
        k=k*a;
        a=a+3;
    }
    cout<<k<<endl;
    return 0;
}

2.12、14、15、16

代码如下(示例):

12.#include<iostream>
using namespace std;
int main (void)
{
     int N,a,b,c,d,e,f,g,h=1,i=0;
     cin>>N;
     if(N<1 || N>1000) cout<<"请重新输入数据"<<endl;
     else if(N<=7) cout<<"组合数为0"<<endl;
     else{ a=N%5;
           if (a==0) 
           {
             b=N/5;
             while(b>=2)
             {
                 b=b-1;
                 c=N-b*5;
                 d=c%2;
                 if(d==0)
                 {
                     e=c/2;
                     while(e>1)
                     {
                         e=e-1;
                         f=c-2*e;
                         cout<<"1元纸币数:"<<f<<' '<<"2元纸币数:"<<e<<' '<<"5元纸币数:"<<b<<endl;
                         i=i+1;}
                 }
                 else 
                 { e=c-1;
                   f=e%2;
                   g=e/2;
                   cout<<"1元纸币数:"<<1<<' '<<"2元纸币数:"<<g<<' '<<"5元纸币数:"<<b<<endl;
                   i=i+1;
                   while(g>1)
                   {
                       g=g-1;
                       h=h+2;
                       cout<<"1元纸币数:"<<h<<' '<<"2元纸币数:"<<g<<' '<<"5元纸币数:"<<b<<endl;
                       i=i+1;
                   }
                 }
             }
           }
           else
            {
                b=N/5;
             while(b>=1)
             {
                 c=N-5*b;
                 d=c%2;
                if (d==0)
                {
                 e=c/2;
                 while(e>1)
                 {
                     e=e-1;
                     f=c-2*e;
                     cout<<"1元纸币数:"<<f<<' '<<"2元纸币数:"<<e<<' '<<"5元纸币数:"<<b<<endl;
                     i=i+1;
                 }
                }
                else
                 {   
                     e=c-1;
                     f=e%2;
                     g=e/2;
                     cout<<"1元纸币数:"<<1<<' '<<"2元纸币数:"<<g<<' '<<"5元纸币数:"<<b<<endl;
                     i=i+1;
                     while(g>1)
                   { 
                       g=g-1;
                     h=h+2;
                     cout<<"1元纸币数:"<<h<<' '<<"2元纸币数:"<<g<<' '<<"5元纸币数:"<<b<<endl;
                     i=i+1;
                   }
                }
                 b=b-1;
             }
            }
           }
     cout<<"总组合数为:"<<i<<endl;
         return 0;
}
14.#include<iostream>
using namespace std;
int main(void)
{
    char ch;
    int i=0;
    do
    {cin.get(ch);
    if (ch==99) i=i++;}while(ch!='\n');
    cout<<"c的数目为:"<<i<<endl;
    return 0;
}
15.#include<iostream>
using namespace std;
int main(void)
{
    int a,n,i=2,t,sum=0;
    cin>>a>>n;
    if (n==1) cout<<a<<endl;
    else while(i<=n)
    {
        t=a*10+a;
        sum=sum+t+a;
        i=i+1;
        cout<<sum<<endl;
    }
    return 0;
}
16.#include<iostream>
#include<cctype>
using namespace std;
int main(void)
{
    char ch;
    int a=0,b=0,d=0,e=0,f=0;
    cin.get(ch);
    while(ch!='\n')
    {
        if (isupper(ch)) a=a++;
        if (islower(ch)) b=b++;
        if (isdigit(ch)) d=d++;
        if (isspace(ch)) e=e++;
        if (ispunct(ch)) f=f++;
        cin.get(ch);

        cout<<ch<<' '<<e<<endl;
    }
    cout<<"大写英文字母数"<<a<<endl;
    cout<<"小写英文字母数"<<b<<endl;
    cout<<"数字字符数"<<d<<endl;
    cout<<"空格数"<<e<<endl;
    cout<<"其他字符数"<<f<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值