sj6
sj6_1
/*将键盘输入的两个整数按大到小输出,单分支,分析结果*/
#include <stdio.h>
main()
{
int a,b,t;
scanf("%d%d",&a,&b);
if(a<b)
{t=a;a=b;b=t;}
printf("%d,%d\n",a,b);
}
sj6_2
/*双分支,分析结果*/
#include <stdio.h>
main()
{
int a;
scanf("%d",&a);
if(a++<5)
printf("%d\n",a);
else
printf("%d\n",++a);
}
/* 4->5
5->7 */
sj6_3
/* 04年全国考题 分支嵌套,分析结果 */
#include <stdio.h>
main()
{
int a=3,b=4,c=5,d=2;
if(a<b) //若a>b 结果如何
if(b>c)
printf("%d",d++ +1);
else
printf("%d",++d +1);
printf("%d\n",d);
}
/* 43
若a>b 结果如何?*/
sj6_4
/*p111_4*/
#include <stdio.h>
void main()
{
int a,b,c,s;
scanf("%d%d%d",&a,&b,&c);
if(a>b&&(a>c||b>c)) s=a;
else
if(b>c) s=b;
else s=c;
printf("%d\n",s);
}
sj6_4_1
/*p111_4*/
#include <stdio.h>
void main()
{
int a,b,c,s;
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(b>c) s=a; //a>b b>c -> a
else
if(a>c) s=a; //a>b b<c a>c -> a
else s=c; //a>b b<c a<c -> c
else
if(b>c) s=b; //a<b b>c -> b
else s=c; //a<b b<c ->c
printf("%d\n",s);
}
sj6_5
/*p111_5*/
#include <stdio.h>
void main()
{
int x,y;
scanf("%d",&x);
if(x<1) y=x;
else
if(x>=1&&x<10) y=2*x-1;
else y=3*x-11;
printf("%d\n",y);
}
sj6_6
/*分析结果*/
#include <stdio.h>
main()
{
int a=1,b=0;
switch(a)
{
case 1:switch(b)
{
case 0:printf("**0**\n");break;
case 1:printf("**1**\n");break;
}
default:printf("**2**\n");
}
}
sj6_7
/*p111_6修改程序,使输入:87,要求输出'A'*/
#include <stdio.h>
main()
{
int x,temp;
char grade;
scanf("%d",&x);
temp=(x-x%10)/10;
if(x==100) temp=9;
else
switch(temp){
case 9: grade='A';
case 8: grade='B';
case 7: grade='C';
case 6: grade='D';
default: grade='E';
}
printf("%c\n",grade);
}
sj6_7_1
/*p111_6 if-else实现*/
#include <stdio.h>
main()
{
int x;
char grade;
scanf("%d",&x);
if(x>=90) grade='A';
else
if(x<900&&x>=80) grade='B';
else
if(x<80&&x>=70) grade='C';
else
if(x<70&&x>=60) grade='D';
else grade='E';
printf("%c\n",grade);
}
sj6_8
/*将此程序改为输入三个数,按小到大输出*/
#include <stdio.h>
void main()
{
int x1,x2,x3,x4,y;
scanf("%d%d%d%d",&x1,&x2,&x3,&x4);
if(x1>x2)
{
y=x1; x1=x2; x2=y;
}
if(x1>x3)
{
y=x1; x1=x3; x3=y;
}
if(x1>x4)
{
y=x1; x1=x4; x4=y;
}
printf("%d,",x1);
if(x2>x3)
{
y=x2; x2=x3; x3=y;
}
if(x2>x4)
{
y=x2; x2=x4; x4=y;
}
printf("%d,",x2);
if(x3>x4)
{
y=x3; x3=x4; x4=y;
}
printf("%d,%d\n",x3,x4);
}
sj6_9
/*分析结果*/
#include <stdio.h>
main()
{
int a=0,b=1;
switch(a)
{
case 0:printf("a=0 ");
switch(b)
{
case 0:printf("b=0 ");break;
case 1:printf("b=1 ");break;
case 2:printf("b=2 ");break;
}
case 1:printf("a=1 ");
default:printf("\n");
}
}