第1部 言語 - 第2章 ループ構造のプログラム設計 - 2.5演習

1.1 演習 2-1 スイセンの数 (スイセン)

タイトル説明:
出力100 ∼ 999 100 \sim 9991 0 0すべての水仙の数は9 9 9です3桁のABCABCA B C满足ABC = A 3 + B 3 + C 3 ABC=A^{3}+B^{3}+C^{3}A B C=3+B3+3、それは水仙の数と呼ばれます。たとえば、153 = 1 3 + 5 3 + 3 3 153=1^{3}+5^{3}+3^{3}1 5 3=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 ハンシンの兵隊 (ハンシン)

トピックの説明:
ハン・シンは非常に才能があり、軍隊の数を直接数えることはなく、兵士に3、5、7の順番でフォーメーションを変更するように頼んだだけで、ちらっと見ただけだったと言われています毎回チームで. 合計人数は列の最後に表示されます. 入力には複数のデータ セットが含まれ、各データ セットには 3 つの非負の整数 a、b、c が含まれ、各フォーメーションの最後にいる人数を示します (a < 3、b < 5、c < 7)。総人数の最小値を出力します(または解決策を報告しません)。総人数は10人以上100人以下であることがわかっています。ファイルの最後まで入力してください。
入力例:
2 1 6
2 1 3
出力例:
ケース 1: 41
ケース 2: 無回答

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 逆三角形(三角形)

タイトルの説明:
正の整数 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)

タイトルの説明:
2 つの正の整数を入力してください n<m< 1 0 6 10^61 06、出力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++メートル21、小数点以下 5 桁を保持します。入力には複数のデータ セットが含まれており、エンド マークは n=m=0 です。ヒント: この質問には落とし穴があります。
サンプル入力:
2 4
65536 655360
0 0
サンプル出力:
ケース 1: 0.42361
ケース 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 分数小数 (10 進数)

タイトルの説明:
正の整数a、b、ca、b、cを入力してください_b c、出力a/ba/ba / bの小数点以下ccまでの精度のc位。a , b ≤ 1 0 6 , c ≤ 100 a, b \leq 10^{6}, c \leq 100_b1 06 ,c1 0 0 . 入力には複数のデータ セットが含まれ、終了タグはa = b = c = 0 a=b=c=0a=b=c=0 .
サンプル入力:
1 6 4
0 0 0
サンプル出力:
ケース 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 順列

タイトルの説明:
use 1 , 2 , 3 , … , 9 1,2,3, \ldots, 91 2 3 9 は3 つの 3 桁のabc \mathrm{abc}a b c ,def \mathrm{def}d e fghi \mathrm{ghi}g h i、各数字は 1 回だけ使用され、abc \mathrm{abc}a b c :def \mathrm{def}d e f :ghi = 1 : 2 : 3 \mathrm{ghi}=1: 2: 3_=1:2:3 . " abc def ghi \mathrm{abc~def~ghi}によるとa b c d e f g h i   " すべての解を 1 行に 1 つずつ出力します。ヒント: 難しく考えすぎないでください。

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