實驗四 選擇結(jié)構(gòu)
堂前習(xí)題
1018 數(shù)的排序
Description
由鍵盤輸入三個整數(shù)a、b、c,按從小到大的順序輸出這三個數(shù)。
#include <stdio.h>
int main()
{
int a,b,c,t;
scanf("%d,%d,%d",&a,&b,&c);
if(a>b){
t=a;
a=b;
b=t;
}
if(a>c){
t=a;
a=c;
c=t;
}
if(b>c){
t=b;
b=c;
c=t;
}
printf("%d,%d,%d",a,b,c);
return 0;
}
1016 字符變換
Description
由鍵盤輸入5個字符,將其中的大寫字符變成小寫(其它類型的字符不變),最后,按輸入順序輸出這5個字符。
#include <stdio.h>
int main()
{
char c;
c=getchar();
int i;
for(i=0;i<5;i++){
if(c>='A'&&c<='Z'){
c+=32;
}
printf("%c",c);
c=getchar();
}
return 0;
}
1019 數(shù)的整除
Descrption
由鍵盤輸入5個整數(shù),逐個判斷它們能否被27整除,能的輸出“YES”,不能的輸出“NO”(注意,輸出時,一個判斷結(jié)果占一行,5個數(shù)的判斷共占5行)。
#include<stdio.h>
int main()
{
int i,a;
for(i=0; i<5; i++)
{
scanf("%d",&a);
if(a%27==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
1020 正負(fù)奇偶判斷
Description
由鍵盤輸入非零整數(shù)x,判斷該數(shù)正負(fù),正數(shù)輸出positive,負(fù)數(shù)輸出negative,接著判斷該數(shù)的奇偶性,奇數(shù)輸出odd,偶數(shù)輸出even。
輸出格式
注意,正負(fù)判斷結(jié)果與奇偶判斷結(jié)果之間用回車符分隔
#include <stdio.h>
int main()
{
int x;
scanf("%d",&x);
if(x>0){
printf("positive\n");
}else{
printf("negative\n");
}
if(x%2==0){
printf("even\n");
}else{
printf("odd\n");
}
return 0;
}
1023 簡單計算器
Description
下面程序是實現(xiàn)一個簡單的運算器(保留兩位小數(shù)點),如果由鍵盤輸入10+50,計算機可以輸出結(jié)果60.00;如果輸入8*6,計算機輸出48.00;如果輸入20/4,計算機輸出5.00;如果輸入8-6,計算機輸出2.00,請在空處填上適當(dāng)?shù)拇a,運行通過后并提交。
#include <stdio.h>
#include<stdlib.h>
int main()
{
float a,b,c;
char op;
scanf("%f%c%f",&a,&op,&b);
switch(op){
case'+':c=a+b;
break;
case'-':c=a-b;
break;
case'*':c=a*b;
break;
case'/':c=a/b;
break;
default:printf("error");
exit(0);
}
printf("result=%.2f",c);
return 0;
}
堂上練習(xí)
1007 判斷平方數(shù)
Description
由鍵盤輸入一個正整數(shù),判斷該數(shù)是否為平方數(shù),是輸出Y,否則輸出N
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n,x;
scanf("%d",&n);
x=sqrt(n);
if(x*x==n)
printf("Y\n");
else
printf("N\n");
return 0;
}
1017 求數(shù)的位數(shù)
Description
由鍵盤輸入一個不多于9位的正整數(shù),要求輸出它是幾位數(shù)。
#include <stdio.h>
#include<math.h>
int main()
{
int a,cnt=0;
scanf("%d",&a);
while(a>0)
{
cnt++;
a/=10;
}
printf("%d",cnt);
return 0;
}
1120 判斷點是否在圓上
Description
由鍵盤輸入一個點的坐標(biāo), 要求編程判斷這個點是否在單位圓(圓心在坐標(biāo)0,0)上,點在圓上輸出Y, 不在圓上輸出N。
使用小數(shù)點后3位精度進(jìn)行判斷。
#include <stdio.h>
#include<math.h>
int main()
{
float x,y;
scanf("%f,%f",&x,&y);
if(fabs(sqrt(x*x+y*y)-1)<1e-3){
printf("Y\n");
}else{
printf("N\n");
}
return 0;
}
單元測試
1 長方體與圓球
Time Limit:1000MS Memory Limit:65536K
題型: 編程題 語言: G++;GCC
描述
由鍵盤輸入一個形如長方體的盒子的長、寬、高,以及一個圓球的半徑,判斷該盒子能否完全裝下圓球,能輸出Y,否則輸出N.
輸入格式
第一行長方體的三邊長
第二行圓球的半徑
輸出格式
Y或N
#include <stdio.h>
#include <math.h>
int main()
{
double a,b,c,r;
scanf("%lf %lf %lf %lf",&a,&b,&c,&r);
if((a>2*r)&&(b>2*r)&&(c>2*r)){
printf("Y\n");
}else printf("N\n");
return 0;
}
實驗五 循環(huán)結(jié)構(gòu)(一)
堂前習(xí)題
1024 計算階乘
Description
輸入正整數(shù)n(n<12),計算n!(注n!=123*…*n)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n,sum=1;
scanf("%d",&n);
for(i=1; i<=n; i++)
sum*=i;
printf("%d",sum);
return 0;
}
1025計算簡單數(shù)列和
Description
有數(shù)列1,3,5,7,9,11,……
現(xiàn)要求由鍵盤輸入n,計算輸出該數(shù)列的前n項和。(給的n不會超過10000)
#include <stdio.h>
#include<math.h>
int main()
{
int n;
scanf("%d",&n);
printf("%d",n*n);
return 0;
}
1044 輸出最小值
Description
從鍵盤輸入十個整數(shù),輸出最小值
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,a[10],min=10000;
for(i=0; i<10; i++)
{
scanf("%d",&a[i]);
if(a[i]<min)
{
min=a[i];
}
}
printf("%d",min);
return 0;
}
堂上練習(xí)
1030 字符變換
Description
由鍵盤輸入一個句子(字符個數(shù)不定,最多不超過80個,以’\n’結(jié)束),將其中的大寫字符變成小寫(其它類型的字符不變),
最后輸出變換后的句子。
#include <stdio.h>
int main()
{
char a;
a=getchar();
while(a!='\n'){
if(a>='A'&&a<='Z'){
a+=32;
}
printf("%c",a);
a=getchar();
}
return 0;
}
1037 計算數(shù)列和
Description
有數(shù)列:編程實現(xiàn),由鍵盤輸入n,計算輸出數(shù)列前n項和。(結(jié)果保留四位小數(shù),提示:要使用double,否則精度不夠)
#include <stdio.h>
int main()
{
double a1=1,a2=2,b1,a3;
double t,n,sum=0;
int i;
scanf("%lf",&n);
sum=a2/a1;
for(i=2;i<=n;i++){
t=a1+a2;
sum+=t/a2;
a1=a2;
a2=t;
}
printf("%.4lf",sum);
return 0;
}
1029 求最大公約數(shù)
Description
由鍵盤輸入兩個正整數(shù)m、n(m、n<1000000),計算它們的最大公約數(shù)。
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int m,n,t;
scanf("%d,%d",&m,&n);
while(n>0)
{
if(m%n==0)
break;
t=m%n;
m=n;
n=t;
}
printf("%d",n);
return 0;
}
1031 統(tǒng)計單詞個數(shù)
Description
寫一個函數(shù)實現(xiàn):輸入一行字符,以空格分割單詞,回車結(jié)束輸入,輸出單詞的個數(shù)。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int a=1,cnt=1;
c=getchar();
while(c!='\n'){
if(c==' ') {
a=0;
}else if(a==0){
a=1;
cnt++;
}
c=getchar();
}
printf("%d",cnt);
return 0;
}
1042 百萬富翁
Description
一個百萬富翁遇到一個陌生人,陌生人找他談了一個換錢的計劃。該計劃如下:我每天給你m元,
而你第一天只需給我一分錢。第二天我仍給你m元,你給我2分錢。第三天,我仍給你m元,
你給我4分錢。依次類推,你每天給我的錢是前一天的兩倍,直到一個月(30天)。
百萬富翁很高興,欣然接受這個契約?,F(xiàn)要求,編寫一個程序,由鍵盤輸入m,
計算多少天后,百萬富翁開始虧錢。
輸入樣例
100
輸出樣例
18
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int m,n;
float a,b,c;
int i;
scanf("%d",&m);
c=0.01;
for(i=1;i<=30;i++){
a+=m;
b+=c;
c*=2;
if(a<b){
printf("%d",i);
break;
}
}
return 0;
}
單元測試
1 求因子個數(shù)
描述
由鍵盤輸入一個int類型的正整數(shù)n,求n有多少個不同的正整數(shù)因子。
注:能整除N的數(shù)稱為N的因子
#include <stdio.h>
#include <math.h>
int main()
{
int n,i,num=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
if(n%i==0){
num++;
}
}
printf("%d",num);
return 0;
}
實驗六 循環(huán)結(jié)構(gòu)(二)
堂前習(xí)題
1035 打印菱形圖案
Description
由鍵盤輸入正數(shù)n(n<30),要求輸出如下2*n+1行的菱形圖案。
輸出格式
菱形右邊不留多余空格
輸入樣例
2
輸出樣例
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=-n; i<=n; i++)
{
for(j=-n; j<=n; j++)
{
if(abs(i)+abs(j)<=n)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
堂上練習(xí)
1028 求素數(shù)
Description
輸出2到200之間(包括2、200)的所有素數(shù)(注:要求1行1個素數(shù),按由小到大的順序輸出)。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,n;
for(i=2;i<=200;i++){
for(n=2;n<sqrt(i);n++){
if(i%n==0){
break;
}
}
if (n>=sqrt(i)){
printf("%d\n",i);
}
}
return 0;
}
1137 找滿足要求的數(shù)字
Description
輸出1到9999中能被7整除,而且至少有一位數(shù)字是5的所有數(shù)字
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j;
for(i=7;i<=9999;i+=7){
j=i;
while(j!=0){
if(j%10==5){
printf("%d\n",i);
break;
}
j/=10;
}
}
return 0;
}
1038 打印圖案
Description
由鍵盤輸入正數(shù)n(n<10),要求輸出如下中間數(shù)字為n的菱形圖案。
輸出格式
菱形右邊不留多余空格
輸入樣例
4
輸出樣例
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,n;
scanf("%d",&n);
for(i=1-n;i<=n-1;i++){
for(j=0;j<abs(i);j++){
printf(" ");
}
for(k=1;k<=n-abs(i);k++){
printf("%d",k);
}
for(k=n-abs(i)-1;k>0;k--)
printf("%d",k);
printf("\n");
}
}
單元測試
1 打印星號空心菱形
描述
由鍵盤輸入n(n為正奇數(shù),n<=50),打印輸出如下圖n行的星號空心菱形
例n=7
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,k;
scanf("%d",&n);
for(i=-(n-1)/2;i<=(n-1)/2;i++){
for(j=0;j<abs(i);j++){
printf(" ");
}
printf("*");
if(abs(i)<(n-1)/2){
for(k=0;k<n-2-2*abs(i);k++){
printf(" ");
}
printf("*");
}
printf("\n");
}
return 0;
}
實驗七 數(shù)組的應(yīng)用
堂前習(xí)題
1039 倒序
Description
由鍵盤輸入10個整數(shù),倒序輸出。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10];
int i,j,t;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=9;i>=0;i--){
printf("%d\n",a[i]);
}
return 0;
}
1062 打印矩陣
Description
由鍵盤輸入一個3*4的矩陣,要求輸出它的轉(zhuǎn)置矩陣。
輸入樣例
1 6 9 3
1 1 0 2
1 9 8 9
輸出樣例
1 1 1
6 1 9
9 0 8
3 2 9
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3][4],b[4][3];
int i,j;
for(i=0;i<3;i++){
for(j=0;j<4;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++){
for(j=0;j<4;j++){
b[j][i]=a[i][j];
}
}
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("%d",b[i][j]);
printf(" ");
}
printf("\n");
}
return 0;
}
堂上練習(xí)
1047 冒泡排序
Description
由鍵盤輸入10個數(shù),用“冒泡法”對10個數(shù)從小到大排序,并按格式要求輸出。代碼如下,請?zhí)畛渫暾?/p>
輸入樣例
70 5 14 20 19 2 99 67 13 66
輸出樣例
2 5 13 14 19 20 66 67 70 99
#include "stdio.h"
main()
{ int a[10], i, j, t;
for(i=0;i<10;i++)
scanf("%d",_______________________) ;
for(_______________________)
{ for(j=0;j<_______________________;j++)
if (_______________________)
{_______________________}
}
for(i=0;i<10;i++)
printf("%d ",a[i]);
}
$line1$
&a[i]
$line2$
i=0;i<9;i++
$line3$
9-i
$line4$
a[j]>a[j+1]
$line5$
t=a[j+1];a[j+1]=a[j];a[j]=t;
1040 統(tǒng)計不同數(shù)字的個數(shù)
Description
由鍵盤輸入20個整數(shù),統(tǒng)計不同數(shù)字的個數(shù)。
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int i,j,cnt=0,num[20];
for(i=0; i<20; i++)
{
scanf("%d",&num[i]);
for(j=0; j<i; j++)
{
if(num[i]==num[j])
break;
}
if(i==j)
cnt++;
}
printf("%d",cnt);
return 0;
}
1051 找矩陣中的鞍點
Description
由鍵盤輸入一個3*4(3行4列)的數(shù)字矩陣,其中任意兩個數(shù)字均不相同。要求輸出該數(shù)字矩陣中的鞍點(即在矩陣行中最大,列中最小的數(shù))。
若沒有鞍點,輸出“NO”字樣。
輸入樣例
87 90 110 98
70 97 210 65
99 45 120 30
輸出樣例
110
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3][4];
int x,y,i,j,t;
int max,min;
x=y=0;
for(i=0;i<3;i++){
for(j=0;j<4;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++){
max=a[i][0];
for(j=0;j<4;j++){
if(a[i][j]>max){
max=a[i][j];
x=j;
}
}
min=a[0][x];
for(t=0;t<3;t++){
if(a[t][x]<min){
min=a[t][x];
y=t;
}
}
if(max==min){
printf("%d",a[y][x]);
break;
}
}
if(max!=min){
printf("NO");
}
return 0;
}
1046 計算高精度加法
由鍵盤輸入兩個位數(shù)很長的整數(shù)(一行一個,最多不超過80位),試計算并輸出這兩個數(shù)的和。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int a[100]={0},b[100]={0},c[100]={0},i,j=0,lx,ly,max;
char x[100],y[100];
scanf("%s",x);
lx=strlen(x);
for(i=0;i<lx;i++){
a[i]=x[lx-1-i]-'0';
}
scanf("%s",y);
ly=strlen(y);
for(i=0;i<ly;i++){
b[i]=y[ly-1-i]-'0';
}
if(lx>ly) max=lx;
else max=ly;
for(i=0;i<max;i++){
c[i]=(a[i]+b[i]+j)%10;
j=(a[i]+b[i]+j)/10;
}
if(j!=0){
max++;
c[max-1]=j;
}
for(i=max-1;i>=0;i--){
printf("%d",c[i]);
}
return 0;
}
單元測試
1 最小差值
描述
由鍵盤輸入10個浮點數(shù),任取其中兩數(shù)相減求絕對值,求其中最小值(保留兩位小數(shù))
輸入格式
10個浮點數(shù),由空格分隔
輸出格式
最小差值
輸入樣例
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 9
輸出樣例
0.20
浮點數(shù)絕對值用fabs?。?!
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
double a[10],min;
int i,j;
for(i=0;i<10;i++){
scanf("%lf",&a[i]);
}
min=abs(a[1]-a[0]);
for(i=0;i<10;i++){
for(j=i+1;j<10;j++){
if(fabs(a[i]-a[j])<min){
min=fabs(a[i]-a[j]);
}
}
}
printf("%.2lf",min);
return 0;
}
實驗八 字符數(shù)組及串
堂前練習(xí)
1121 定義存貯字符串的數(shù)組
Description
在下面程序中填充定義字符數(shù)組的語句,使程序完整。
#include "stdio.h"
#include "string.h"
int main()
{ _______________________/*define a array named s to store string*/
strcpy(s, "abcdefghijklmn");
printf("%s", s);
return 0;
}
$line1$
char s[80];
1122 字符串的合并
Description
從鍵盤輸入3個字符串(每個字符串以回車符做為結(jié)束標(biāo)志),將3個字符串以輸入先后順序合并到字符串s中,
請?zhí)羁帐褂贸绦蛲暾?/p>
#include "stdio.h"
#include "string.h"
main()
{
char s[100]="";
char a[30];
_______________________
printf("%s", s);
}
$block1$
gets(a);strcat(s,a);
gets(a);strcat(s,a);
gets(a);strcat(s,a);
$end1$
1123 字符串的輸入與輸出
Description
下面程序?qū)崿F(xiàn)從鍵盤讀入字符串,然后輸出到屏幕,請?zhí)畛浔匾恼Z句。
輸入樣例
Wang
輸出樣例
What’s your name?
Your name is Wang
#include "stdio.h"
main()
{ char s[50];
printf("What's your name?\n");
_______________________ /*iput your name from the keyboard*/
printf("Your name is ");
printf("_______________________", s); /*output your name*/
}
$line1$
gets(s);
$line2$
%s
堂上練習(xí)
1145 回文串
Description
讀入一行字符串(不多于80個字符,以回車結(jié)束),判斷該字符串是否為回文串(即從左向右拼寫與從
右向左拼寫是一樣的),是則輸出Y,不是則輸出N。
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int i,j,k;
char c[81];
scanf("%s",c);
k=strlen(c);
for(i=0; i<k/2; i++)
{
if(c[i]!=c[k-1-i])
break;
}
if(i>=k/2)
printf("Y\n");
else
printf("N\n");
return 0;
}
1050 尋找字符串
Description
由鍵盤輸入兩個字符串(假設(shè)第一個字符串必包含第二個字符串,如第一個字符串為ABCDEF,第二個為CDE,
則CDE包含在ABCDEF中),現(xiàn)要求編程輸出第二字符串在第一行字符串中出現(xiàn)的位置。
(如果第二個字符串在第一個字符串中出現(xiàn)多次,則以最前出現(xiàn)的為準(zhǔn))
輸入樣例
ABCDEFG
DE
輸出樣例
4
#include <stdio.h>
#include <string.h>
int main()
{
char a[100],b[100];
int i,j,x,y,k;
gets(a);
gets(b);
x=strlen(a);
y=strlen(b);
for(i=0;i<x;i++){
for(j=0;j<y;j++){
if (a[i+j]!=b[j])
break;
}
if (b[j]=='\0')
break;
}
if (a[i]!='\0')
printf("%d",i+1);
return 0;
}
單元測試
1 多少個Bubble
描述
讀入一行字符串(不多于800個字符,以回車結(jié)束),統(tǒng)計其中Bubble出現(xiàn)了多少次
輸入樣例
Bubble if only Bubble.
輸出樣例
2
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int i,j,cnt=0;
char a[800];
gets(a);
char b[7]="Bubble";
for(i=0;a[i]!='\0';i++){
for(j=0;b[j]!='\0';j++){
if(a[i+j]!=a[j])
break;
}
if(b[j]=='\0'){
cnt++;
i+=6;
}
}
printf("%d",cnt);
return 0;
}
實驗九 函數(shù)的應(yīng)用
堂前習(xí)題
1083 編寫函數(shù)計算階乘
Description
下面程序?qū)崿F(xiàn)由鍵盤讀入整數(shù)n,計算并輸出n!,請補充完整計算階乘的函數(shù)。
#include "stdio.h"
_______________________
main()
{ int n;
scanf("%d", &n);
printf("%ld", fanc(n));
}
$block1$
int fanc(int n){
int sum=1,i;
for(i=1;i<=n;i++){
sum*=i;
}
return sum;
}
$end1$
1124 函數(shù)中的變量
Description
寫出下面程序的運行結(jié)果:
int f1(int x)
{
static int z=3,y=0;
y++;
z++;
return(x+y+z);
}
main()
{
int a=1,k;
for(k=0;k<3;k++) printf("%4d",f1(a));
}
程序到此結(jié)束 請用下面程序輸出你的答案(注意轉(zhuǎn)義字符的正確表達(dá))
#include “stdio.h”
main()
{
printf(“_______________________”);
}
$line1$
6 8 10\n
堂上練習(xí)
1059 [填空題]函數(shù)定義
Description
下面是使用輾轉(zhuǎn)相除法,求最大公約數(shù)的程序,請補充完整程序中函數(shù)的定義與調(diào)用,運行通過后提交代碼。
輸入樣例
24 16
輸出樣例
8
#include "stdio.h"
_______________________
{
int r;
while ((r=m%n)!=0)
{
m=n;
n=r;
}
return n;
}
main()
{
int a, b, n;
scanf("%d%d", &a, &b);
printf("%d\n", _______________________);
}
$line1$
int GCM(int m,int n)
$line2$
GCM(a,b)
1084 [填空題]十進(jìn)制數(shù)轉(zhuǎn)二進(jìn)制數(shù)
Description
下面程序,實現(xiàn)由鍵盤輸入一個正整數(shù)(不大于100000000),輸出其對應(yīng)的二進(jìn)制數(shù)(原碼表示)。
請?zhí)羁眨?/p>
#include "stdio.h"
_______________________
main()
{
int n;
scanf("%d", &n);
binary(n);
}
輸入樣例
12
輸出樣例
1100
$block1$
void binary(int n){
if(n/2>0){
binary(n/2);
}
printf("%d",n%2);
}
$end1$
1151 求函數(shù)值
Description
輸入x(x為整數(shù)),求函數(shù)值
函數(shù)定義如下:
F(x)=x x小于3
F(x)=F(x/3)*2 x大于等于3且x為3的倍數(shù)
F(x)=F((x-1)/3)+1 x大于等于3且x除3余1
F(x)=F((x-2)/3)+2 x大于等于3且x除3余2
#include <stdio.h>
#include <stdlib.h>
int F(int x){
if (x<3) return x;
else if(x%3==0) return F(x/3)*2;
else if(x%3==1) return F((x-1)/3)+1;
else if(x%3==2) return F((x-2)/3)+2;
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",F(n));
return 0;
}
單元測試
1 求函數(shù)值2
描述
輸入x(x為整數(shù)),求函數(shù)值F(x)
函數(shù)定義如下:
F(x)=x x小于2
G(x)=x x小于2
F(x)=G(x/2)*2 x大于等于2且x為偶數(shù)
F(x)=G((x-1)/2) x大于等于2且x為奇數(shù)
G(x)=G(x/2)+1 x大于等于2且x為偶數(shù)
G(x)=x x為奇數(shù)
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int G(int x)
{
int gx;
if(x<2) gx=x;
else if(x>=2&&x%2==0) gx=G(x/2)+1;
else if(x>=2&&x%2!=0) gx=x;
return gx;
}
int F(int x)
{
int y;
if(x<2) y=x;
else if(x>=2&&x%2==0) y=G(x/2)*2;
else if(x>=2&&x%2!=0) y=G((x-1)/2);
return y;
}
int main()
{
int x;
scanf("%d",&x);
printf("%d",F(x));
return 0;
}
實驗十 指針與結(jié)構(gòu)體
堂前習(xí)題
1091 [填空]交換兩數(shù),由大到小輸出
Description
下面程序,交換兩數(shù),使兩數(shù)由大到小輸出,請?zhí)羁?/p>
#include "stdio.h"
void swap(_______________________)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
int main()
{ int a,b; int *pa,*pb;
scanf("%d%d", &a, &b);
pa=&a; pb=&b;
if(a<b) swap(_______________________);
printf("%d %d\n",a,b);
}
$line1$
int *p1,int*p2
$line2$
&a,&b
11128 字符串與指針
Descrption
請寫出下列程序的運行結(jié)果
#include<stdio.h>
int main( )
{ char string[30]="How_are_you" ;
char *p=&string[0], *p2=string+8;
printf("%s,%s\n" , p , p2 ) ;
}
程序運行結(jié)果為:
#include <stdio.h>
int main()
{
printf("_______________________");
}
$line1$
How_are_you,you
1125 定義結(jié)構(gòu)體類型
Description
要求定義一個名為student的結(jié)構(gòu)體類型,其包含如下成員:
(1)字符數(shù)組name,最多可存放10個字符;
(2)字符變量sex,用于記錄性別;
(3)整數(shù)類型變量num,用于記錄學(xué)號;
(4)float類型變量score,用于記錄成績;
并使下列代碼完整。
#include "stdio.h"
_______________________
int main()
{
struct student stu;
gets(stu.name);
scanf("%c", &stu.sex);
scanf("%d", &stu.num);
scanf("%f", &stu.score);
printf("%s\n", stu.name);
printf("%c\n", stu.sex);
printf("%d\n", stu.num);
printf("%f\n", stu.score);
return 0;
}
$block1$
struct student
{
char name[10];
char sex;
int num;
float score;
};
$end1$
堂上練習(xí)
1092 [填空]函數(shù)實現(xiàn)求字符串長度
Description
下面程序?qū)崿F(xiàn)由函數(shù)實現(xiàn)求字符串長度,再填空完成
#include "stdio.h"
/*create function f*/
_______________________
int main()
{
char s[80];
int i;
scanf("%s", s);
i=f(s);
printf("%d", i);
}
$block1$
int f(const char*p){
int cnt=0;
while(*(p++)){
cnt++;
}
return cnt;
}
$end1$
1065 數(shù)組中的指針
Description
設(shè)有如下數(shù)組定義:
int a[3][4]={{1,3,5,7},{9,11,13,15},{17,19,21,23}};
計算下面各項的值(設(shè)數(shù)組a的首地址為2000,一個int類型數(shù)占四個字節(jié))。
(1)a[2][1] (2)a[1] (3)a (4)a+1 (5)*a+1(6)*(a+1) (7)a[2]+1
(8)*(a+1)+1 (9)*(*(a+2)+2)
編寫一個程序直接輸出你的答案,一行一個。
提示
注意:地址則輸出地址,變量則輸出變量值;輸出格式,要求,一行一個答案,不允許多余空格
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("19\n2016\n2000\n2016\n2004\n2016\n2036\n2020\n21");
return 0;
}
實驗十一 鏈表操作
堂前習(xí)題
1099鏈表的合并
Description
下面程序創(chuàng)建兩個鏈表,然后將第二個鏈表合并到第一個鏈表未尾,但合并部分的代碼未完成,請你完成這部分代碼。
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
struct student *merge(struct student *head, struct student *head2)
{
_______________________
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
main()
{
struct student *head, *head2;
int n;
long del_num;
scanf("%d",&n);
head=create(n);
print(head);
scanf("%d",&n);
head2=create(n);
print(head2);
head = merge(head, head2);
print(head);
}
$block1$
struct student *p=NULL;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=head2;
return(head);
$end1$
堂上練習(xí)
1098 [填空]鏈表結(jié)點的插入
Description
完成插入鏈表結(jié)點的函數(shù)(按學(xué)號順序),并調(diào)試通過、提交。
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head, struct student *stud)
{
_______________________
}
main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
stu=(struct student *)malloc(LEN);
scanf("%ld",&stu->num);
scanf("%d",&stu->score);
stu->next = NULL;
head=insert(head,stu);
print(head);
}
$block1$
struct student *p1=NULL, *p2=NULL, *p3=NULL;
p1=head;
p2=stud;
if(head==NULL)
{
head=p2;
p2->next=NULL;
}
else
{
while(p1->num < p2->num && p1->next!=NULL)
{
p3=p1;
p1=p1->next;
}
if(p1->num >= p2->num)//在表頭或表中間
{
if(head==p1)//在表頭
head=p2;
else //表中間
{
p3->next = p2;
p2->next = p1;
}
}
else//表尾
{
p1->next=p2;
p2->next=NULL;
}
}
return(head);
$end1$
1104 [填空題]鏈表的倒序
Description
下面程序,先創(chuàng)建一個鏈表,然后調(diào)用reverse函數(shù),將鏈表中各結(jié)點變?yōu)榈剐蚺帕小U埻瓿蓃everse函數(shù),
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *reverse(struct student *head)
{
_______________________
}
main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
head=reverse(head);
print(head);
}
$block1$
struct student *p1=NULL, *p2=NULL, *p3=NULL;
p2=head;
p3=head->next;
do
{
p1=p2;
p2=p3;
p3=p2->next;
p2->next=p1;
}while(p3!=NULL);
head->next=NULL;
return (p2);
$end1$
1101 [填空題]鏈表的排序
Description
下面程序,先創(chuàng)建一個鏈表(鏈表中各結(jié)點未按學(xué)號由小到大排序),然后調(diào)用sort函數(shù),將鏈表中各結(jié)點按學(xué)號由小到大排序。
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head, struct student *stud)
{ struct student *p0,*p1,*p2;
p1=head; p0=stud;
if(head==NULL)
{head=p0;}
else
{ while( (p0->num > p1->num) && (p1->next!=NULL) )
{ p2=p1; p1=p1->next;}
if( p0->num <= p1->num )
{ if( head==p1 ) head=p0;
else p2->next=p0;
p0->next=p1; }
else { p1->next=p0;}
}
return(head);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
p1=head;
while(p1!=NULL)
{
if(p1->num == num)
{
if(p1 == head) head=p1->next;
else p2->next=p1->next;
free(p1);
break;
}
p2=p1;
p1=p1->next;
}
return(head);
}
struct student *sort(struct student *head)
{
_______________________
}
main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
head=sort(head);
print(head);
}
$block1$
struct student *p1,*p2;
p2=head;
p1=head;
p2=p2->next;
p1->next=NULL;
p1=p2;
while(p2->next!=NULL){
p2=p2->next;
p1->next=NULL;
head=insert(head,p1);
p1=p2;
}
head=insert(head,p1);
return (head);
$end1$
實驗十二 文件操作
堂前習(xí)題
1105 [填空]文本文件操作_字符讀入
Description
在當(dāng)前目錄中存在文件名為"case1.in"的文本文件,現(xiàn)要求你使用fopen函數(shù)命令打開該文件,讀出里面的所有字符, 遇到大寫字母的,將其變?yōu)樾懽帜?,其它字符不變,最后將所有字符按順序在屏幕上輸出。請?zhí)羁胀瓿沙绦颍?(注意,填空題,請不要使用return 0結(jié)束,否則會影響評判而判錯)
(如case1.in內(nèi)容如下)
Hello my Dear:
Have a GooD Time!
(在屏幕上輸出結(jié)果如下)
hello my dear:
have a good time!
(提示,在提交前要測試自己的代碼是否正確,可在源文件所有目錄自己創(chuàng)建一個名為case1.in的文本文件,
在文件中自己打入一些字母,以便測試自己的代碼是否正確)
#include "stdio.h"
main()
{
FILE *fp;
char ch;
if((_______________________)==NULL)
return 0;
while(_______________________)
{
if ('A'<=ch && ch<='Z')
ch = ch + 32;
_______________________;
}
fclose(fp);
}
$line1$
fp=fopen("case1.in","r")
$line2$
(ch=fgetc(fp)) !=EOF
$line3$
putchar(ch)
1106 文本文件操作_字符寫入
Description
由鍵盤輸入任意個字符(以連著的三個小寫字符bye做為結(jié)束標(biāo)志),將所有字符(包括bye),寫入新建的文件answer.txt中(注:文件放在當(dāng)前目錄)。 請完成該功能,(注意,填空題,請不要使用return 0結(jié)束,否則會影響評判而判錯)
(如鍵盤輸入內(nèi)容如下)
He, can you write the code?
Yes, you can.bye
No, you can’t.
(程序執(zhí)行后,在文件answer.txt中內(nèi)容如下)
He, can you write the code?
Yes, you can.bye
(注:因No, you can’t.在bye之后,所以不輸出)
(注:代碼中不要使用return及exit()函數(shù),以免誤判)
#include<stdio.h>
main()
{
______________________
}
$block1$
char ch,ch1=' ',ch2=' ',ch3=' ';
FILE *fp;
fp=fopen("answer.txt","w");
if(fp==NULL)
return 1;
while((ch=getchar())!=EOF)
{
fputc(ch,fp);
ch1=ch2;
ch2=ch3;
ch3=ch;
if(ch1=='b'&&ch2=='y'&&ch3=='e')
break;
}
fclose(fp);
$end1$
堂上練習(xí)
11129 文本文件操作_讀取與選擇顯示
Description
在當(dāng)前目錄中存在文件名為"case1.in"的文本文件,現(xiàn)要求打開該文件,讀出里面的所有字符,只將其中的數(shù)字字符按先后順序顯示在屏幕上。
(如case1.in內(nèi)容如下)
13 cats and 22 bikes
(在屏幕上輸出結(jié)果如下)
1322文章來源:http://www.zghlxwxcb.cn/news/detail-764214.html
#include "stdio.h"
main()
{
FILE *fp;
char ch;
if((_______________________)==NULL)
return 0;
while(_______________________)
{
_______________________
}
fclose(fp);
}
$block1$
if(ch>='0'&&ch<='9')
putchar(ch);
$end1$
$line1$
fp=fopen("case1.in","r")
$line2$
(ch=fgetc(fp))!=EOF
1107 文本文件操作_單詞的排序
Description
在當(dāng)前目錄有文件“case1.in”,文件里存放有多個(總個數(shù)不超過10000個)英文單詞(每個英文單詞不會超過10個字文字符), 每行一個,單詞未排序?,F(xiàn)要求,將文件中的所有單詞按字典順序排序,然后將排序好的單詞寫入新建的文件answer.txt中(注:文件存放于當(dāng)前目錄)。 請完成程序,實現(xiàn)該功能,(注意,填空題,請不要使用return 0結(jié)束,否則會影響評判而判錯)
(如case1.in文件中原內(nèi)容如下)
hello
bye
yes
(程序執(zhí)行后,在文件answer.txt中內(nèi)容如下)
bye
hello
yes文章來源地址http://www.zghlxwxcb.cn/news/detail-764214.html
#include "stdio.h"
#include "string.h"
main()
{
_______________________
}
$block1$
int i,j,n=0;
char w[10000][10],temp[10];
FILE *fp;
if((fp=fopen("case1.in","r"))==NULL) return 1;
while((fscanf(fp,"%s",w[n]))!=EOF) n++;
fclose(fp);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(w[j],w[j+1])>0)
{
strcpy(temp,w[j]);
strcpy(w[j],w[j+1]);
strcpy(w[j+1],temp);
}
}
}
if((fp=fopen("answer.txt","w"))==NULL) return 1;
for(i=0;i<n;i++)
{
fprintf(fp,"%s\n",w[i]);
}
fclose(fp);
$end1$
到了這里,關(guān)于SCAU高級語言程序設(shè)計OJ的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!