Part 1 Language - Chapter 2 Program Design of Loop Structure - 2.5 Exercises

1.1 Exercise 2-1 Number of daffodils (daffodil)

Title description:
Output 100 ∼ 999 100 \sim 999100All daffodil numbers in 9 9 9 . If the 3-digitABCABCABC 满足 A B C = A 3 + B 3 + C 3 A B C=A^{3}+B^{3}+C^{3} ABC=A3+B3+C3 , it is called the number of daffodils. For example153 = 1 3 + 5 3 + 3 3 153=1^{3}+5^{3}+3^{3}153=13+53+33 , so 153 is the number of daffodils.

1.2 C++ language implementation

/**
* @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 Exercise 2-2 Hanxin's order of soldiers (hanxin)

Description of the topic:
It is said that Han Xin was so talented that he never directly counted the number of his troops. He just asked the soldiers to change their formation in a row of three, five, and seven, and he only glanced at the team each time. The total number of people will be known at the end of the row. The input contains multiple sets of data, and each set of data contains 3 non-negative integers a, b, c, indicating the number of people at the end of each formation (a < 3, b < 5, c < 7), and output the minimum value of the total number of people (or report no solution). It is known that the total number of people is not less than 10 and not more than 100. Input until end of file.
Sample input:
2 1 6
2 1 3
Sample output:
Case 1: 41
Case 2: No answer

2.2 C++ language implementation

/**
* @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 Exercise 2-3 Inverted triangle (triangle)

Title description:
Input a positive integer n≤20, and output an n-layer inverted triangle. For example, when n=5, the output is as follows:
#########
 #######
  #####
   ###
    #

3.2 C++ Language Implementation

/**
* @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 Exercise 2-4 The sum of subsequences (subsequence)

Title description:
Enter two positive integers n<m< 1 0 6 10^6106 , output1 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, keep 5 decimal places. The input contains multiple sets of data, and the end mark is n=m=0. Tip: There are pitfalls in this question.
Sample input:
2 4
65536 655360
0 0
Sample output:
Case 1: 0.42361
Case 2: 0.00001

4.2 C++ Language Implementation

/**
* @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 Exercise 2-5 Fractional decimals (decimal)

Title description:
Enter positive integers a, b, ca, b, ca,b,c , outputa/ba/bThe decimal form of a / b , accurate toccc 位。 a , b ≤ 1 0 6 , c ≤ 100 a, b \leq 10^{6}, c \leq 100 a,b106,c1 0 0 . The input contains multiple sets of data, and the end tag isa = b = c = 0 a=b=c=0a=b=c=0 .
Sample input:
1 6 4
0 0 0
Sample output:
Case 1: 0.1667

5.2 C++ Language Implementation

/**
* @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 Exercises 2-6 permutation

Title description:
use 1 , 2 , 3 , … , 9 1,2,3, \ldots, 91,2,3,,9 forms three three-digitabc \mathrm{abc}abc, d e f \mathrm{def} def g h i \mathrm{ghi} g h i , each digit used exactly once, requiresabc \mathrm{abc}abc : d e f \mathrm{def} def: g h i = 1 : 2 : 3 \mathrm{ghi}=1: 2: 3 g h i=1:2:3 . According to "abc def ghi \mathrm{abc~def~ghi}a b c d e f g h i   " output all solutions, one solution per line. Hint: don't think too hard.

6.2 C++ Language Implementation

/**
* @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;
}

Guess you like

Origin blog.csdn.net/m0_46223009/article/details/123944903