第1部分语言篇-第2章循环结构程序设计-2.5习题

1.1 习题2-1 水仙花数(daffodil)

题目描述:
输出 100 ∼ 999 100 \sim 999 100999 中的所有水仙花数。若 3 位数 A B C A B C ABC 满足 A B C = A 3 + B 3 + C 3 A B C=A^{3}+B^{3}+C^{3} ABC=A3+B3+C3, 则称其为水仙花 数。例如 153 = 1 3 + 5 3 + 3 3 153=1^{3}+5^{3}+3^{3} 153=13+53+33, 所以 153 是水仙花数。

1.2 C++语言实现

/**
* @author AlbertDarren
* @contact [email protected]
*/
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    
    
    int hundreds_digit,tens_digit,ones_digit;
    for (int i=100;i<1000 ;++i )
    {
    
    
        hundreds_digit=i/100;
        tens_digit=(i/10)%10;
        ones_digit=i%10;
        if (pow(hundreds_digit,3)+pow(tens_digit,3)+pow(ones_digit,3)==i)
        {
    
    
            printf("%d\n",i);
        }
    }
	return 0;
}

2.1 习题2-2 韩信点兵(hanxin)

题目描述:
相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排、五人 一排、七人一排地变换队形,而他每次只掠一眼队伍的排尾就知道总人数了。输入包含多组 数据,每组数据包含3个非负整数a,b,c,表示每种队形排尾的人数(a<3,b<5,c< 7),输出总人数的最小值(或报告无解)。已知总人数不小于10,不超过100。输入到文件 结束为止。
样例输入:
2 1 6
2 1 3
样例输出:
Case 1: 41
Case 2: No answer

2.2 C++语言实现

/**
* @author AlbertDarren
* @contact [email protected]
*/
#include <iostream>

using namespace std;

int main()
{
    
    
    freopen("ch2-loopStructureProgramming-ex2-2-hanxin.in","r",stdin);
    freopen("ch2-loopStructureProgramming-ex2-2-hanxin.out","w",stdout);
    int a,b,c,kase=0;
    while (scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
    
    
        bool flag=false;
        int i=10;
        for (;i<=100 ;++i )
        {
    
    
            if ((i%3==a)&&(i%5==b)&&(i%7==c))
            {
    
    
                flag=true;
                break;
            }
        }
        ++kase;
        if (flag)
        {
    
    
            printf("Case %d: %d\n",kase,i);
        }
        else
        {
    
    
            printf("Case %d: No answer\n",kase);
        }
    }
	return 0;
}

3.1 习题2-3 倒三角形(triangle)

题目描述:
输入正整数n≤20,输出一个n层的倒三角形。例如,n=5时输出如下:
#########
 #######
  #####
   ###
    #

3.2 C++语言实现

/**
* @author AlbertDarren
* @contact [email protected]
*/
#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    short n;
    scanf("%d",&n);
    for (short i=n; i>=1 ; --i )
    {
    
    
        string line="";
        line+=string(n-i,' ');
        line+=string(2*i-1,'#');
        line.append("\n");
        printf(line.c_str());
    }
    return 0;
}

4.1 习题2-4 子序列的和(subsequence)

题目描述:
输入两个正整数n<m< 1 0 6 10^6 106,输出 1 n 2 + 1 ( n + 1 ) 2 + ⋯ + 1 m 2 \dfrac{1}{n^{2}}+\dfrac{1}{(n+1)^{2}}+\cdots+\dfrac{1}{m^{2}} n21+(n+1)21++m21,保留5位小数。输入包含多组数据, 结束标记为n=m=0。提示:本题有陷阱。
样例输入:
2 4
65536 655360
0 0
样例输出:
Case 1: 0.42361
Case 2: 0.00001

4.2 C++语言实现

/**
* @author AlbertDarren
* @contact [email protected]
*/
#include <iostream>
using namespace std;

int main()
{
    
    
    freopen("ch2-loopStructureProgramming-ex2-4-subsequence.in","r",stdin);
    freopen("ch2-loopStructureProgramming-ex2-4-subsequence.out","w",stdout);
    int n,m,kase=0;
    double sum;
    while (scanf("%d%d",&n,&m)!=EOF&&n&&m)
    {
    
    
        sum=0.0;
        for (int i=n;i<=m ;++i )
        {
    
    
            sum+=(double)1/i/i;//avoid integer overflow
        }
        printf("Case %d:%.5lf\n",++kase,sum);
    }
	return 0;
}

5.1 习题2-5 分数化小数(decimal)

题目描述:
输入正整数 a , b , c a, b, c a,b,c, 输出 a / b a / b a/b 的小数形式, 精确到小数点后 c c c 位。 a , b ≤ 1 0 6 , c ≤ 100 a, b \leq 10^{6}, c \leq 100 a,b106,c100 。输 入包含多组数据, 结束标记为 a = b = c = 0 a=b=c=0 a=b=c=0
样例输入:
1 6 4
0 0 0
样例输出:
Case 1: 0.1667

5.2 C++语言实现

/**
* @author AlbertDarren
* @contact [email protected]
*/
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
double round2digit(double decimal,int digit)
{
    
    
    return floor((decimal*pow(10,digit)+0.5))/pow(10,digit);
}
int main()
{
    
    
    freopen("ch2-loopStructureProgramming-ex2-5-decimal.in","r",stdin);
    freopen("ch2-loopStructureProgramming-ex2-5-decimal.out","w",stdout);
    int a,b,c,kase=0;
    while (scanf("%d%d%d",&a,&b,&c)==3&&a&&b&&c)
    {
    
    
       string output="Case %d:%.";
       output.append(to_string(c));
       output.append("lf\n");
       printf(output.c_str(),++kase,round2digit((double)a/b,c));
    }
	return 0;
}

6.1 习题2-6 排列(permutation)

题目描述:
1 , 2 , 3 , … , 9 1,2,3, \ldots, 9 1,2,3,,9 组成 3 个三位数 a b c \mathrm{abc} abc, d e f \mathrm{def} def g h i \mathrm{ghi} ghi, 每个数字恰好使用一次, 要求 a b c \mathrm{abc} abc : d e f \mathrm{def} def: g h i = 1 : 2 : 3 \mathrm{ghi}=1: 2: 3 ghi=1:2:3 。按照“ a b c   d e f   g h i \mathrm{abc~def~ghi} abc def ghi”的格式输出所有解,每行一个解。提示:不必太动脑筋。

6.2 C++语言实现

/**
* @author AlbertDarren
* @contact [email protected]
*/
#include <iostream>
#include <set>
using namespace std;

int main()
{
    
    
    short three_digit,hundreds_digit,tens_digit,ones_digit;
    for (short i=123; i<=987 ; ++i )
    {
    
    
        bool digit_unique_in=true;//default
        set<short> digits_set;
        digits_set.insert(0);
        for (short j=1; j<=3 ; ++j )
        {
    
    
            three_digit=j*i;
            hundreds_digit=three_digit/100;
            tens_digit=(three_digit/10)%10;
            ones_digit=three_digit%10;
            if (!(digits_set.insert(hundreds_digit).second&&digits_set.insert(tens_digit).second&&
                    digits_set.insert(ones_digit).second&&(to_string(three_digit).size()==3)))
            {
    
    
                digit_unique_in=false;
                break;
            }
        }
        if (digit_unique_in)
        {
    
    
            printf("%d %d %d\n",i,2*i,3*i);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_46223009/article/details/123944903