信息学奥林匹克竞赛程序设计快速入门篇

资源下载
  1. 二一教育资源

信息学奥林匹克竞赛程序设计快速入门篇

资源简介

信息学奥林匹克竞赛程序设计快速入门篇(C语言)
实验一、 顺序结构程序设计
编写一个小程序,在屏幕上显示:
*
***
*****
*******
#include"stdio.h"
main()
{
printf(" *\n");
printf(" ***\n");
printf(" *****\n");
printf("*******\n");
getchar();
return 0;
}
求两个整数之和;
#include"stdio.h"
main()
{
int a,b,c;
a=23;
b=322;
c=a+b;
printf("%d",c);
getchar();
return 0;
}
拓展:求两个整数之商;
求半径为10的圆面积。
#include "stdio.h"
main()
{
float r,s;
printf("\nr:");
scanf("%f",&r);
s=3.14*r*r;
printf("s=%.2f",s);
getchar();
}
拓展:求园的周长;
已知三角形两边及夹角的角度,求三角形的面积。
#include "stdio.h"
#include "math.h"
#define pi 3.1415926
main()
{
float a,b,c,s;
printf("\na,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
s=a*b*sin(c*pi/180)/2;
printf("s=%.2f",s);
}
输入一个百位整数,反序输出。
#include "stdio.h"
main()
{
int x,y,gw,sw,bw;
printf("\nx:");
scanf("%d",&x);
bw=x/100;
sw=(x/10)%10;
gw=x%10;
y=gw*100+sw*10+bw;
printf("%d",y);
}
交换两个变量中的值。
[·方法1·]
#include "stdio.h"
main()
{
int a=1,b=2,t;
t=a; a=b; b=t;
printf("\na=%d,b=%d",a,b);
}
[·方法2·]
#include "stdio.h"
main()
{
int a=1,b=2;
a=a+b; b=a-b; a=a-b;
printf("\na=%d,b=%d",a,b);
}
输入一个小写字母,输出一个相应的大写字母。
#include"stdio.h"
main()
{
char ch;
scanf("%c",&ch);
printf("%c",ch-32);
getchar();
getchar();
return 0;
}
实验二 选择结构程序设计
求半径为10的圆面积。(if结构)
#include "stdio.h"
main()
{
float r,s;
printf("\nr:");
scanf("%f",&r);
if (r>0)
{
s=3.14*r*r;
printf("s=%.2f",s);
}
else
printf("r wrong!");
}
输入一个三位整数,反序输出。(if结构)
#include "stdio.h"
main()
{
int x,y,gw,sw,bw;
printf("\nx:");
scanf("%d",&x);
if (x>=100 && x<1000)
{
if (x%10!=0)
{
bw=x/100;
sw=(x/10)%10;
gw=x%10;
y=gw*100+sw*10+bw;
printf("%d",y);
}
else
printf("wrong 0");
}
else
printf("out range!");
}
求两个数的大数。
[·方法1·]
#include "stdio.h"
main()
{
int a,b,c;
printf("\na,b:");
scanf("%d,%d",&a,&b);
if (a>b)
c=a;
else
c=b;
printf("c=%d",c);
}
[·方法2·]
#include "stdio.h"
main()
{
int a,b,c;
printf("\na,b:");
scanf("%d,%d",&a,&b);
c=a;
if (b>a)
c=b;
printf("c=%d",c);
}
输入三个数,降序输出。
#include "stdio.h"
main()
{
int a,b,c,t;
printf("\na,b,c:");
scanf("%d,%d,%d",&a,&b,&c);
if (aif (aif (bprintf("%d,%d,%d",a,b,c);
}
根据重量求运输费用(用三种方法)。
[·方法1·] 并列单分支
#include "stdio.h"
main()
{
float w,p;
printf("\nw:");
scanf("%f",&w);
if (w<=0)
printf("weight error");
if (w>0 && w<=20)
{p=2*w; printf("p=%f",p);}
if (w>20)
{p=2*20+(w-20)*3; printf("p=%f",p);}
}
[·方法2·] 分支嵌套
#include "stdio.h"
main()
{
float w,p;
printf("\nw:");
scanf("%f",&w);
if (w<=0)
printf("weight error");
else
{
if (w<=20)
p=2*w;
else
p=2*20+(w-20)*3;
printf("p=%f",p);
}
}
[·方法3·] 阶梯分支
#include "stdio.h"
main()
{
float w,p;
printf("\nw:");
scanf("%f",&w);
if (w>20)
{
p=2*20+(w-20)*3;
printf("p=%f",p);
}
else if (w>0)
{
p=2*w;
printf("p=%f",p);
}
else
printf("weight error");
}
输入一个日期,判别该日期为当年的第几天。
#include "stdio.h"
main()
{
int year,month,day,s;
printf("\nyear,month,day:");
scanf("%d,%d,%d",&year,&month,&day);
s=day;
switch(month-1)
{
case 11: s=s+30;
case 10: s=s+31;
case 9: s=s+30;
case 8: s=s+31;
case 7: s=s+31;
case 6: s=s+30;
case 5: s=s+31;
case 4: s=s+30;
case 3: s=s+31;
case 2: if ((year%4==0 && year%100!=0)||year%400==0)
s=s+29;
else
s=s+28;
case 1: s=s+31;
}
printf("\ns=%d",s);
}
实验三、循环结构
连续输出15个“*”;
#include"stdio.h"
main()
{
int i;
for(i=1;i<=15;i++)
{
printf("*");
}
getchar();
return 0;
}
求 s=1+2+3+4+5+...+100 的和。
#include "stdio.h"
main()
{
int i,s;
s=0;
for(i=1;i<=100;i++)
s=s+i;
printf("\ns=%d",s);
getchar();
return 0;
}
拓展:求5!值。
设计程序求 s=1!+2!+3!+...+10! 的和。
·方法1:通项法
#include "stdio.h"
main()
{
int i,j; long s,t;
s=0;
for(i=1;i<=10;i++)
{
t=1;
for(j=1;j<=i;j++)
t=t*j;
s=s+t;
}
printf("\ns=%ld",s);
}
·方法2:递推法
#include "stdio.h"
main()
{
int i; long s,t;
s=0; t=1;
for(i=1;i<=10;i++)
{
t=t*i;
s=s+t;
}
printf("\ns=%ld",s);
}
输出100-999之间的所有水仙花数,并求和。(水仙花数:153=1*1*1+5*5*5+3*3*3)
main()
{
int i,j,k,s,t;
s=0;
for(i=1;i<=9;i++)
for(j=0;j<=9;j++)
for(k=0;k<=9;k++)
{
t=i*100+j*10+k;
if (i*i*i+j*j*j+k*k*k==t)
{
printf("\n%d",t);
s=s+t;
}
}
printf("\ns=%d",s);
}
求两个整数的最大公约数和最小公倍数。
main()
{
int a,b,x,s;
printf("\na,b:");
scanf("%d,%d",&a,&b);
s=a*b;
while (a%b!=0)
{
x=a%b;
a=b;
b=x;
}
printf("\ngys=%d,gbs=%d",b,s/b);
}
运用循环结构打印出n行:
*
**
***
****
*****
#include"stdio.h"
main()
{
int i,j,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getchar();
getchar();
return 0;
}
拓展:使用循环嵌套完成第一题。
实验四、一维数值型数组
求任意10个数的平均值。
#define N 10
main()
{
int i,s;
int a[N]={1,2,3,4,5,6,7,8,9,10}; /* 也可以利用输入语句赋值 */
s=0;
for(i=0;i{
printf("%3d",a[i]);
s=s+a[i];
}
printf("\ns=%d",s/N);
}
数组元素的输入:
printf("\n%d number:",N);
for(i=0;iscanf("%d",&a[i]);
在任意10个数中求下标为偶数的元素和。
#define N 10
main()
{
int i,s;
int a[N]={12,2,3,4,13,5,4,6,12,4}; /* 也可以利用输入语句赋值,见题1。*/
s=0;
for(i=0;i{
printf("%3d",a[i]);
if (i%2==0)
s=s+a[i];
}
printf("\ns=%d",s);
}
实验五、 二维数值型数组、字符型数组
求二维数组所有元素和,并按行输出该数组。
#define M 5
#define N 4
main()
{
int a[M][N]={{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9,10,11,12 },
{13,14,15,16 },
{17,18,19,20 }};
int i,j,s;
s=0;
for(i=0;ifor(j=0;js=s+a[i][j];
printf("\ns=%d",s);
for(i=0;i{
printf("\n");
for(j=0;jprintf("%3d",a[i][j]);
}
}
拓展:打印杨辉三角。
求任意字符串的长度。
#define N 80
main()
{
char a[N]; int i,n;
printf("\ns:"); gets(a);
n=0;
for(i=0;a[i]!='\0';i++)
n++;
printf("n=%d",n);
}
将任意字符串的小写字母转换成大写字母后输出(其它字符不变)。
#define N 80
main()
{
char a[N]; int i;
printf("\ns:"); gets(a);
for(i=0;a[i]!='\0';i++)
if (a[i]>='a' && a[i]<='z') a[i]=a[i]-32;
puts(a);
}
实验六、函数应用
利用函数统计任意字符串中数字的个数。
#define N 80
main()
{ int hs(char a[]);
char a[N]; int s;
printf("\na:");
gets(a);
s=hs(a);
printf("s=%d",s);
}
int hs(char a[])
{int i,n=0;
for(i=0;a[i]!='\0';i++)
if (a[i]>='0' && a[i]<='9')
n++;
return(n);
}
利用函数设计,求任意一组数据的平均值。
float avg(int a[],int n)
{ int i; float s=0;
for(i=0;is=s+a[i];
return s/n;
}
#define N 10
main()
{int a[N]={1,2,3,4,5,6,7,8,9,10};
printf("\n%f",avg(a,N));
}
实验七、结构体
利用结构体求“平面上两点间的距离”。
#include "math.h"
struct point
{ float x;
float y;
};
main()
{ struct point p1,p2;
float d,dx,dy;
printf("\np1(x,y):"); scanf("%f,%f",&p1.x,&p1.y);
printf("\np2(x,y):"); scanf("%f,%f",&p2.x,&p2.y);
dx=p1.x-p2.x; dy=p1.y-p2.y;
d=sqrt(dx*dx+dy*dy);
printf("\nd=%f",d);
}
实验八、文件指针
采用文件输入、输出的形式,求两个数的和。
//输入输出文件开始都是以写的形式产生的
#include "stdio.h" //包含基本的输入输出头文件
main() //主函数 ,标准C++,要求有返回值,不能是void
{
FILE *fin,*fout; //定义输入输出文件指针
int a,b,c; //定义相关变量
fin=fopen("filename.in","r"); //以读取的方式打开输入文件,指向fin
fout=fopen("filename.out","w");//以写入的方式打开输出文件,指向fout
fscanf(fin,"%d%d",&a,&b); //读取输入文件中的数值,存入变量 a,b中
c=a+b; //计算
fprintf(fout,"%d",c); //在输出计算结果到输出 文件中
fclose(fin); //关闭输入文件指针
fclose(fout); //关闭输出文件指针
return 0; // 返回主函数值
}
拓展:已知三角形的三边,求其面积。(if结构判断三边符合三角形要求)
(文件名:sans,输入文件:sans.in,输出文件:sans.out)
如:输入:3 4 5
输出:6
再如:输入:3 7 3
输出:不能构成三角形

展开更多......

收起↑

资源预览