中文字幕精品无码一区二区,成全视频在线播放观看方法,大伊人青草狠狠久久,亚洲一区影音先锋色资源

信息學奧林匹克競賽程序設計快速入門篇

資源下載
  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
輸出:不能構成三角形

展開更多......

收起↑

資源預覽

<pre id="tfb94"><li id="tfb94"></li></pre>

<bdo id="tfb94"><rt id="tfb94"></rt></bdo>
  • <menu id="tfb94"><dl id="tfb94"></dl></menu><i id="tfb94"><acronym id="tfb94"><sub id="tfb94"></sub></acronym></i>

    1. 主站蜘蛛池模板: 上杭县| 卢氏县| 滕州市| 肇源县| 虹口区| 禄丰县| 华容县| 汪清县| 尤溪县| 阜新| 汕尾市| 乌兰浩特市| 甘孜县| 九台市| 改则县| 顺义区| 准格尔旗| 库车县| 正安县| 永胜县| 清远市| 绥棱县| 林州市| 沈阳市| 陆良县| 江都市| 玛纳斯县| 江华| 广东省| 承德市| 蒲城县| 霸州市| 乳山市| 浦北县| 商南县| 天津市| 平舆县| 白山市| 丰顺县| 大庆市| 麦盖提县|