演習問題3解答例

問1 算術式の計算
問2 台形の面積
問2 台形の面積(Java)
問3 円の円周と面積
問3 円の円周と面積(Java)
問4 式の計算
問4 式の計算(Java)
問5 商と余り
問5 商と余り(Java)
問6 お釣りの計算
問6 お釣りの計算(Java)
問7 切り捨て,切り上げ,及び,四捨五入
問7 切り捨て,切り上げ,及び,四捨五入(Java)

[問1]以下の演算を C/C++ のプログラム内で実行した結果を答えよ.
(1)4.0 / 5.0
(2)4 / 5
(3)(double)4 / 5
(4)(double)(4 / 5)
(5)(int)(4.0 / 5.0)
(6)(20 / 5) * (19 / 4)
(7)(20.0 / 5.0) * (19.0 / 4.0)
(8)20 / 5 * 19 / 4

(1)0.8
(2)0
(3)0.8
(4)0.0
(5)0
(6)16
(7)19.0
(8)19

[問2]台形の上底 t1,下底 t2,及び,高さ h の値を,それぞれ,3.5,1.0,及び,0.7 としたとき(これらの値は,各変数に代入または初期設定せよ),台形の面積を計算し,出力するプログラムを書け.
/********************************/
/*     台形の面積の計算         */
/*          coded by Y.Suganuma */
/********************************/
#include <stdio.h>

int main()
{
	double t1, t2, h, men;

	t1  = 3.5;
	t2  = 1.0;
	h   = 0.7;

	men = 0.5 * h * (t1 + t2);

	printf("面積=%f\n", men);

	return 0;
}
    
+++ Javaの場合 +++
/********************************/
/*     台形の面積の計算         */
/*          coded by Y.Suganuma */
/********************************/
import java.io.*;

public class Test {
	public static void main(String args[])
	{
		double t1, t2, h, men;

		t1  = 3.5;
		t2  = 1.0;
		h   = 0.7;

		men = 0.5 * h * (t1 + t2);

		System.out.println("面積=" + men);
	}
}
    

[問3]円の半径を読み込み,その円の円周と面積を出力するプログラムを書け.
/********************************/
/*     円周と面積の計算         */
/*          coded by Y.Suganuma */
/********************************/
#include <stdio.h>

int main()
{
	double r, ens, men, pi;
/*
     πの値
*/
	pi = 3.141592654;
/*
     半径の読込
*/
	printf("円の半径は? ");
	scanf("%lf", &r);
/*
     円周と面積の計算
*/
	ens = 2.0 * pi * r;
	men = pi * r * r;
/*
     出力
*/
	printf("円周=%f    面積=%f\n", ens, men);

	return 0;
}
    
+++ Javaの場合 +++
/********************************/
/*     円周と面積の計算         */
/*          coded by Y.Suganuma */
/********************************/
import java.io.*;

public class Test {
	public static void main(String args[]) throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		double r, ens, men, pi;
/*
     πの値
*/
		pi = 3.141592654;
/*
     半径の読込
*/
		System.out.print("円の半径は? ");
		r = Double.parseDouble(in.readLine());
/*
     円周と面積の計算
*/
		ens = 2.0 * pi * r;
		men = pi * r * r;
/*
     出力
*/
		System.out.println("円周=" + ens + "    面積=" + men);
	}
}
    

[問4]x の値を読み込み,次の各関数の値( y )を出力するプログラムを書け.
  (1)y = x3 + 3x2 + 2
  (2)y = (x + 3)2 / 3 + 5
/********************************/
/*     式の値の計算             */
/*          coded by Y.Suganuma */
/********************************/
#include <stdio.h>

int main()
{
	double s1, s2, x, x1;
/*
     xの読込
*/
	printf("xは? ");
	scanf("%lf", &x);
/*
     式の計算
*/
	x1 = x + 3.0;
	s1 = x * x * x + 3.0 * x * x + 2.0;
	s2 = x1 * x1 / 3.0 + 5.0;
/*
     出力
*/
	printf("式1=%f    式2=%f\n", s1, s2);

	return 0;
}
    
+++ Javaの場合 +++
/********************************/
/*     式の値の計算             */
/*          coded by Y.Suganuma */
/********************************/
import java.io.*;

public class Test {
	public static void main(String args[]) throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		double s1, s2, x, x1;
/*
     xの読込
*/
		System.out.print("xは? ");
		x = Double.parseDouble(in.readLine());
/*
     式の計算
*/
		x1 = x + 3.0;
		s1 = x * x * x + 3.0 * x * x + 2.0;
		s2 = x1 * x1 / 3.0 + 5.0;
/*
     出力
*/
		System.out.println("式1=" + s1 + "    式2=" + s2);
	}
}
    

[問5]2 つの整数データを読み込み,商と余りを出力するプログラムを書け.
/********************************/
/*     商と余り                 */
/*          coded by Y.Suganuma */
/********************************/
#include <stdio.h>

int main()
{
	int n1, n2, sho, amari;
/*
     データの読込
*/
	printf("2つの整数を入力して下さい ");
	scanf("%d %d", &n1, &n2);
/*
     計算
*/
	sho   = n1 / n2;
	amari = n1 % n2;
/*
     出力
*/
	printf("商=%d    余り=%d\n", sho, amari);

	return 0;
}
    
+++ Javaの場合 +++
/********************************/
/*     商と余り                 */
/*          coded by Y.Suganuma */
/********************************/
import java.io.*;

public class Test {
	public static void main(String args[]) throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n1, n2, sho, amari;
/*
     データの読込
*/
		System.out.print("最初の整数を入力して下さい ");
		n1 = Integer.parseInt(in.readLine());
		System.out.print("2番目の整数を入力して下さい ");
		n2 = Integer.parseInt(in.readLine());
/*
     計算
*/
		sho   = n1 / n2;
		amari = n1 % n2;
/*
     出力
*/
		System.out.println("商=" + sho + "    余り=" + amari);
	}
}
    

[問6]千円札だけを使用でき,かつ,千円以下の切符を売る自動販売機がある.任意の料金( 10 円単位)の切符を買った(切符の料金を入力データとして読み込む)とき,その釣り銭を出力するプログラムを書け.ただし,釣り銭の総額だけでなく,必要な硬貨の枚数,例えば,500 円硬貨 1 枚,100 円硬貨 2 枚,50 円硬貨 1 枚,10 円硬貨 3 枚という結果も出力すること.
  (ヒント)例えば,「 turi / 500 」という演算により,500 円硬貨の枚数が得られる
/********************************/
/*     釣り銭の計算             */
/*          coded by Y.Suganuma */
/********************************/
#include <stdio.h>

int main()
{
	int kippu, turi, money, i500, i100, i50, i10;
/*
          切符の値段の読込
*/
	printf("切符の値段は? ");
	scanf("%d", &kippu);
/*
          釣り銭の額
*/
	turi  = 1000 - kippu;
	money = turi;
/*
          釣り銭の内訳の計算

               500円
*/
	i500  = money / 500;
	money = money - 500 * i500;
/*
               100円
*/
	i100  = money / 100;
	money = money - 100 * i100;
/*
               50円
*/
	i50   = money / 50;
	money = money - 50 * i50;
/*
               10円
*/
	i10   = money / 10;
/*
          結果の出力
*/
	printf("釣り銭 %d\n", turi);
	printf("500円 %d  100円 %d  50円 %d  10円 %d\n", i500, i100, i50, i10);

	return 0;
}
    
+++ Javaの場合 +++
/********************************/
/*     釣り銭の計算             */
/*          coded by Y.Suganuma */
/********************************/
import java.io.*;

public class Test {
	public static void main(String args[]) throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int kippu, turi, money, i500, i100, i50, i10;
/*
          切符の値段の読込
*/
		System.out.print("切符の値段は? ");
		kippu = Integer.parseInt(in.readLine());
/*
          釣り銭の額
*/
		turi  = 1000 - kippu;
		money = turi;
/*
          釣り銭の内訳の計算

               500円
*/
		i500  = money / 500;
		money = money - 500 * i500;
/*
               100円
*/
		i100  = money / 100;
		money = money - 100 * i100;
/*
               50円
*/
		i50   = money / 50;
		money = money - 50 * i50;
/*
               10円
*/
		i10   = money / 10;
/*
          結果の出力
*/
		System.out.println("釣り銭 " +  turi);
		System.out.println("500円 " + i500 + "  100円 " + i100 + "  50円 " + i50 + "  10円 " + i10);
	}
}
    

[問7]ある値段(入力)の商品を購入したとき,消費税( 3 %)を含めた実際の支払価格を以下の 3 つの場合について計算し,それぞれの値を出力するプログラムを書け.ただし,実数を使用すると誤差が生じる可能性があるので,すべて整数を使用して計算せよ.
  (1)1 円未満を切り捨てる
  (2)1 円未満を切り上げる
  (3)1 円未満を 4 捨 5 入する
  (ヒント)例えば,切り捨ての演算は,「 nedan * 103 / 100 」という演算により可能
/********************************/
/*     消費税の計算             */
/*          coded by Y.Suganuma */
/********************************/
#include <stdio.h>

int main()
{
	long nedan0, nedan1, nedan2, nedan3;
/*
          値段の入力
*/
	printf("商品の値段は? ");
	scanf("%ld", &nedan0);

	nedan0 = nedan0 * 103;
/*
          切り捨て
*/
	nedan1 = nedan0 / 100;
/*
          切り上げ
*/
	nedan2 = (nedan0 + 99) / 100;
/*
          4捨5入
*/
	nedan3 = (nedan0 + 50) / 100;
/*
          出力
*/
	printf("切り捨て %ld  切り上げ %ld  4捨5入 %ld\n", nedan1, nedan2, nedan3);

	return 0;
}
    
+++ Javaの場合 +++
/********************************/
/*     消費税の計算             */
/*          coded by Y.Suganuma */
/********************************/
import java.io.*;

public class Test {
	public static void main(String args[]) throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int nedan0, nedan1, nedan2, nedan3;
/*
          値段の入力
*/
		System.out.print("商品の値段は? ");
		nedan0 = Integer.parseInt(in.readLine());

		nedan0 = nedan0 * 103;
/*
          切り捨て
*/
		nedan1 = nedan0 / 100;
/*
          切り上げ
*/
		nedan2 = (nedan0 + 99) / 100;
/*
          4捨5入
*/
		nedan3 = (nedan0 + 50) / 100;
/*
          出力
*/
		System.out.println("切り捨て " + nedan1 + "  切り上げ " + nedan2 + "  4捨5入 " + nedan3);
	}
}
    

ホームページ 目次 演習解答例目次 付録目次 索引