4周目:構造を選択するプログラム設計の練習問題(北理)
23960 ワード
入力したa,b,cの3つの整数について最小値を求める.以下のプログラムは間違っていますが、どのように変更しますか?
以下のプログラム実行後tの値が4であれば、実行時にa,bの値範囲を入力する.
次のプログラムは、aの値が014およびox 14の場合、実行結果はそれぞれ次のとおりです.
3.1実現経路におけるプログラム選択
次のセグメントを実行すると、x、y、zの値はそれぞれ20 30 30です.
一元二次方程式を解く
3.2パスの再選択-ネスト判定
任意の3つの数の大きいものから小さいものへのソート
3つの数のソート後のオプティマイザ
自分で作った間違ったプログラムは、どのように変更しますか?
3.3マルチレベル選択問題のCプログラム
マルチレベル選択マルチレベル選択とネスト選択の違いは何ですか?授業からの「討論問題2」
switch switchについてネストして使用できますか?授業からの「討論問題3」
3.4マルチブランチ問題のプログラム選択
(一)switchでマルチブランチ問題を解決する
(二)加減練習問題を実現してプログラムを作成する.簡単な加減乗除を実現する
(三)switchの構造
3.5 goto文の適切な使用
goto文の使用を提唱しない
goto文の単純なインスタンス単純なインスタンス
3.6構造を選択するプログラムの例
計算する税金は現在の国家の税収の標準によって、もし月収の1.58万元ならば、国家の税金を納めるべきなのはいくらですか?
授業からの「討論問題4」
出力奇数は「xの値が[-1100]または[2002 10]の範囲内の奇数である場合、xを出力する」if文を正しく表すことができます.授業からの「討論問題5」
ある年のある月のある日を入力して、この日がこの年の何日目であるかを判断して出力する問題分析:年月日を入力して、現在の月の前の日数+当月の日数を計算します注意:どのように閏年が閏年であることを判断して、しかも月が2月を超えると総日数に更に1を加えます.
int main()
{
int a,b,c;
scanf(”%d%d%d”,a,b,c);
if((a>b)&&(a>c))
if(bprintf(”min=%d
”,b);
else
printf(”min=%d
”,c);
if((aprintf(”min=%d
”,a);
}
以下のプログラム実行後tの値が4であれば、実行時にa,bの値範囲を入力する.
#include
int main()
{
int a, b, s = 1,t = 1;
scanf("%d ,%d",&a,&b);
if (a > 0)
s += 1;
if (a > b)
t += s;
else if (a == b)
t = 5;
else
t = 2 * s;
printf("s=%d,t=%d
",s,t);
}
次のプログラムは、aの値が014およびox 14の場合、実行結果はそれぞれ次のとおりです.
#include
int main()
{
int a;
scanf("%o", a);
if (a = 0xA | a > 12)
if (011 & 10 == a)
printf("%d!
", a);
else
printf("Right!%d
", a);
else
printf("Wrong!%d
",a);
return 0;
}
3.1実現経路におけるプログラム選択
次のセグメントを実行すると、x、y、zの値はそれぞれ20 30 30です.
int x=10,y=20,z=30;
if(x>y) z=x;x=y;y=z;
************************
int x = 10, y = 20, z = 30;
if (x > y)
z = x;
x = y;
y = z;
,
一元二次方程式を解く
#include
#include
int main()
{
float a, b, c, d, x1, x2, q, m, n;
printf("Please input a,b,c
");
scanf("%f,%f,%f",&a,&b,&c);
d = b*b - 4*a*c;
if (d >= 0)
{
q = sqrt(d);
x1 = (-b + q) / (2 * a);
x2 = (-b - q) / (2 * a);
printf("x1 =%.2f,x2=%.2f
",x1,x2);
}
else
{
m = b / (2 * a);
n = sqrt(-d) / (2 * a);
printf("x1 = %.2f + %.2fi",m,n);
printf("x1 = %.2f + %.2fi",m,n);
}
}
3.2パスの再選択-ネスト判定
任意の3つの数の大きいものから小さいものへのソート
#include
int main()
{
int a, b, c;
printf(" ");
scanf("%d %d %d",&a,&b,&c);
if (a > b)
{
if (c > a)
printf("%d %d %d
",c,a,b);
else //c b
{
if (b > c)
printf("%d %d %d
", a, b, c);
else
printf("%d %d %d
", a, c, b);
}
}
else//b>a
{
if (c > b)
printf("%d %d %d
",c,b,a);
else
{
if (a > c)
printf("%d %d %d
", b, a, c);
else
printf("%d %d %d
",b,c,a);
}
}
}
3つの数のソート後のオプティマイザ
#include
int main()
{
int a, b, c,t;
printf(" :");
scanf("%d %d %d %d",&a,&b,&c);
if (a < b)
{
t = a;
a = b;
b = t;
}
if (b < c)
{
t = b;
b = c;
c = t;
if (a > b)
{
t = a;
a = b;
b = t;
}
}
printf(" :%d,%d,%d",a,b,c);
return 0;
}
自分で作った間違ったプログラムは、どのように変更しますか?
#include
int main()
{
int a, b, c,t;
printf(" :");
scanf("%d %d %d",&a,&b,&c);
if (a > b)// a,b
{
t = a;
b = a;
a = t;
}
else
{
if (b > c)
{
t = b;
b = a;
a = t;
}
else
{
if (a > b)
{
t = a;
a = b;
b = t;
}
}
}
}
3.3マルチレベル選択問題のCプログラム
マルチレベル選択マルチレベル選択とネスト選択の違いは何ですか?授業からの「討論問題2」
switch switchについてネストして使用できますか?授業からの「討論問題3」
#include
int main()
{
int year;
float money, rate, total;
printf("Input money and year: ");
scanf("%f %d",&money,&year);
if (year == 1)
rate = 0.0032;
else if (year == 2)
rate = 0.0041;
else if (year == 3)
rate = 0.005;
else if (year == 5)
rate = 0.0055;
else if (year == 6)
rate = 0.0065;
else
rate = 0.0;
total = money + money*rate * 12 * year;
printf("Total = %.2f
",total);
return 0;
}
3.4マルチブランチ問題のプログラム選択
(一)switchでマルチブランチ問題を解決する
(二)加減練習問題を実現してプログラムを作成する.簡単な加減乗除を実現する
#include
int main()
{
float d1, d2;//
char op; //
scanf("%f %c %f",&d1,&op,&d2);
switch (op)//
{
case '+':
printf("%.2f +%.2f = %.2f
",d1,d2,d1+d2);
break;
case '-':
printf("%.2f - %.2f =%.2f
",d1,d2,d1-d2);
break;
case '*':
printf("%.2f - %.2f =%.2f
",d1,d2,d1*d2);
break;
case '/':
if (d2 == 0)// 0
printf("Division by zero.
");
else
printf("%.2f/%.2f=%.2f
",d1,d2,d1/d2);
break;
}
}
(三)switchの構造
, switch
A. switch (a/b)
{ case 1: case 3.2: y=a+b; break ;
case 0: case 5: y=a-b;
}
B. switch (a*a+b*b);
{case 3:
case 1: y=a+b; break ;
case 0: y=b-a; break;
}
C. switch a
{ default : x=a+b;
case 10 : y=a-b;break;
case 11 : y=a*d; break;
}
D. switch(a+b)
{case 10: x=a+b; break;
case 11: y=a-b; break;
}
3.5 goto文の適切な使用
goto文の使用を提唱しない
goto文の単純なインスタンス単純なインスタンス
#include
int main()
{
int n = 0;
printf("input a string:
");
loop:if (getchar() != '
')
{
n++;
goto loop;
}
printf("%d",n);
}
#include
int main()
{
int n = 0;
printf("input a string:
");
while (getchar() != '
')
{
n++;
}
printf("%d
",n);
}
3.6構造を選択するプログラムの例
計算する税金は現在の国家の税収の標準によって、もし月収の1.58万元ならば、国家の税金を納めるべきなのはいくらですか?
授業からの「討論問題4」
出力奇数は「xの値が[-1100]または[2002 10]の範囲内の奇数である場合、xを出力する」if文を正しく表すことができます.授業からの「討論問題5」
ある年のある月のある日を入力して、この日がこの年の何日目であるかを判断して出力する問題分析:年月日を入力して、現在の月の前の日数+当月の日数を計算します注意:どのように閏年が閏年であることを判断して、しかも月が2月を超えると総日数に更に1を加えます.
#include
int main()
{
int day, month, year,sum;
printf(" :");
scanf("%d %d %d",&year,&month,&day);
switch (month)//
{
case 1:
sum = 0;
break;
case 2:
sum = 31;
break;
case 3:
sum = 59;
break;
case 4 :
sum = 90;
break;
case 5:
sum = 120;
break;
case 6:
sum = 151;
break;
case 7:
sum = 181;
break;
case 8:
sum = 212;
break;
case 9:
sum = 243;
break;
case 10:
sum = 273;
break;
case 11:
sum = 304;
break;
case 12:
sum = 334;
break;
}
//
if (((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) && month >2)
sum = sum + 1; ,
sum = sum + day;
printf(" %d day",sum);
}
#include
int main()
{
int year;
float money, rate, total;
printf("Input money and year:");
scanf("%f %d",&money,&year);
switch (year)
{
case 1:
rate = 0.0032;
case 2:
rate = 0.0041;
break;
case 3:
rate = 0.005;
break;
case 5:
rate = 0.0055;
break;
case 8:
rate = 0.0065;
break;
default:
rate = 0.0;
}
total = money + money*rate * 12 * year;
printf("Total = %.2f
",total);
}