20級大資料1班第七次堂測題解
這次堂測的題目比較綜合,而且有點難度,所以希望同學們能好好理解
1 偶數與質數
考點:如何判斷一個數為素數
思路:運用回圈和判斷找出符合條件的兩個素數找
題目要求

輸入樣例
8
輸出樣例
3 5
完整代碼
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int n,i,j,k;
scanf("%d",&n);
for(i=2; i<n; i++)//運用回圈從2開始依次判斷是否為素數
{
int x1=sqrt(i);//判斷i是否為素數
for(j=2; j<=x1; j++)
{
if(i%j==0)
break;
}
if(j>x1)//找到素數i后,判斷n-i是否也為素數
{
int x2=sqrt(n-i);
for(k=2; k<=x2; k++)
{
if((n-i)%k==0)
break;
}
if(k>x2)//若都為素數,則列印這兩個素數
{
printf("%d %d",i,n-i);
return 0;
}
}
}
}
2 多個數的最大公約數
考點:判斷一個數是否為公約數
思路:因為有多個數,所以運用陣列將數字存盤,然后運用回圈和判斷條件找到最大公約數
題目要求

輸入樣例
3
12 18 42
輸出樣例
6
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int n,i,j,a[10],flag=0;
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%d",&a[i]);//運用陣列存盤數字
int min=a[0];
for(i=1; i<n; i++)
{
if(a[i]<=min)
min=a[i];
}//找到這些數字中的最小值
for(i=min; i>=1; i--)//從最小值開始逐漸遞減,看是否為公因數
{
for(j=0; j<n; j++)
{
if(a[j]%i!=0)//若存在一個數不能整除i,則i不是公因數
{
flag=0;
break;
}
else if(a[j]%i==0)
flag=1;
}
if(flag)//若flag=1,說明已經找到最大公因數,列印并跳出回圈即可
{
printf("%d",i);
break;
}
}
}
3 Z型數字矩陣(改錯題)
這道題需要同學們根據已有的代碼,理清回圈條件和矩陣行數與列數的關系,難度比較高,盡量理解就好,答案不作解釋,請同學們自習理解,這樣才能加深印象
題目要求

輸入樣例
3
輸出樣例
1 2 6
3 5 7
4 8 9
改錯后的代碼
$block1$
if(m<n&&m%2==0)
x=m-j;
else if(m<n&&m%2!=0)
x=j;
else if(m>=n&&m%2==0)
x=m-i-j;
else if(m>=n&&m%2!=0)
x=m+j-n+1;
y=m-x;
a[x][y]=count++;
$end1$
完整代碼
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
int main()
{ int a[20][20], n, i, j, m=0, x, y, temp, count=1;
scanf("%d", &n);
for(i=-(n-1);i<=n-1;i++)
{
for(j=0;j<n-abs(i);j++)
{
/*******************Error**********************
if(m<n)
x=m-j;
else
x=n-1-j;
y=m-x;
a[x][y]=count++;
********************Error*********************/
//不作解釋,請同學們自行理解
if(m<n&&m%2==0)
x=m-j;
else if(m<n&&m%2!=0)
x=j;
else if(m>=n&&m%2==0)
x=m-i-j;
else if(m>=n&&m%2!=0)
x=m+j-n+1;
y=m-x;
a[x][y]=count++;
}
m++;
}
for(i=0; i<n; i++)
{ for(j=0; j<n; j++)
printf("%3d", a[i][j]);
printf("\n");
}
}
4 數字的全排列
該題目難度十分的高,總結方法,理清思路最重要,
題目要求

輸入樣例
3
輸出樣例
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1
完整代碼
#include <stdio.h>
#define max 10
void sort(int a[],int n,int s,int r[],int m);//先定義函式
int main()
{
int n,a[max],i;
int r[max];
scanf("%d",&n);
for(i=0; i<n; i++)
a[i]=i+1;
sort(a,n,0,r,n);
return 0;
}
void sort(int a[],int n,int s,int r[],int m) //其中m表示排列數字的個數,整個遞回程序不發生改變;
{
//其實條件中的s是r陣列的下標,r[s]存盤遞回每次取出的那個元素;
int i,j; //開始在該行定義了l=0;錯誤;導致下邊的第一個for回圈在第二次回圈時還保留第一次了的值,必須每次保證清零;
int flag=0;
int b[max];
for(i=0; i<n; i++) //該回圈將陣列中的第i個元素存入陣列r中;
{
flag=1; //確定函式的范圍是否到達該輸出的時候;
r[s]=a[i];
int l=0;
for(j=0; j<n; j++) //該回圈的目的是將剩下的元素放入一個新陣列中,方便下次遞回運用;
{
if(i!=j)
{
b[l]=a[j];
l++;
}
}
sort(b,n-1,s+1,r,m);
}
if(flag==0)
{
int k;
for(k=0; k<m; k++)
{
if(k!=m-1)
printf("%d,",r[k]);
else
printf("%d",r[k]);
}
printf("\n");
}
}
5 三角形旋轉(改錯題)
對一個二維陣列順時針旋轉的方法
1.沿主對角線對稱位置上的元素進行交換
2.對一維陣列而言,將每一行第i個元素與第N-i-1個元素進行交換
對一個二維陣列逆時針旋轉的方法
1.沿主對角線對稱位置上的元素進行交換
2.將每一列第i個元素與第N-i-1個元素進行交換
題目要求

輸入樣例
3
1
23
456
90
輸出樣例
421
53
6
修改后的代碼
$block1$
for(i = 0; i < n; i++)//先將a陣列復制給b
for(j = 0; j < n; j++)
b[i][j]=a[i][j];
for(i = 0; i < n; i++)//將b陣列沿主對角線對稱位置上的元素進行交換
for(j = 0; j < i; j++)
{
t = b[i][j];
b[i][j] = b[j][i];
b[j][i] = t;
}
for(i=0; i<n; i++)//將b陣列每一行第i個元素與第N-i-1個元素進行交換
for(j = n - 1; j >= n/ 2; j--)
{
t=b[i][j];
b[i][j]=b[i][n-1-j];
b[i][n-1-j]=t;
}
$end1$
$block2$
for(j=0; j<n; j++)
{
if(a[i][j]==0)//判斷是否需要列印空格
printf(" ");
else
printf("%c",a[i][j]);
}
$end2$
完整代碼
#include <stdio.h>
#include <stdlib.h>
char a[20][20]= {0};
char b[20][20]= {0};
int main()
{
int i,j,n,w,t;
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%s", a[i]);
scanf("%d", &w);
w=(w%360)/90;
for(t=0; t<w; t++)
{
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
b[i][j]=a[i][j];
for(i = 0; i < n; i++)
for(j = 0; j < i; j++)
{
t = b[i][j];
b[i][j] = b[j][i];
b[j][i] = t;
}
for(i=0; i<n; i++)
for(j = n - 1; j >= n/ 2; j--)
{
t=b[i][j];
b[i][j]=b[i][n-1-j];
b[i][n-1-j]=t;
}
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
a[i][j] = b[i][j];
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(a[i][j]==0)
printf(" ");
else
printf("%c",a[i][j]);
}
printf("\n");
}
return 0;
}
6 綜合實驗: 學生成績管理
難點:
1、資料比較多,資料復雜
2、要根據學生成績將學生整體資訊排序
解題思路:
1、看到這型別的題目,就應該想到用結構體做,因為要將一個學生看成一個整體,進行排序,雖然二維陣列也可以實作這個程序,但是不如結構體自然,
2.確定用結構體后,再梳理整個程式的思路,首先要讀入資料,然后分別算出各學生的總分和各科目的平均分,接著根據總分將學生排序,最后輸出資料
題目要求

輸入樣例
5 3
90 70 75
89 67 78
87 65 98
97 86 77
88 96 82
輸出樣例
88 96 82
97 86 77
87 65 98
90 70 75
89 67 78
90.2 76.8 82
完整代碼
#include <stdio.h>
#include <stdlib.h>
struct student
{
int screo[11];
};
typedef struct student DEG;
int main()
{
DEG stu[1005],t;
int i,j,n,m,sum[20],t1;
double ave[20];
scanf("%d%d",&n,&m);
for (i=0;i<n;i++)
{
for(j=0;j<m;j++) //讀入資料
{
scanf("%d",&stu[i].screo[j]); //注意結構體的使用
sum[i]+=stu[i].screo[j];
}
}
for (i=0;i<m;i++) //注意m,n的使用
{
for (j=0;j<n;j++)
{
ave[i]+=stu[j].screo[i];
}
ave[i]/=n; //算出平均分
}
for (i=0;i<n-1;i++) //冒泡排序
{
for (j=0;j<n-1-i;j++)
{
if (sum[j]<sum[j+1]) //交換學生資訊的同時還要交換總分
{
t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;
t1=sum[j];
sum[j]=sum[j+1];
sum[j+1]=t1;
}
}
}
for (i=0;i<n;i++) //輸出資料
{
for (j=0;j<m;j++)
{
printf("%d ",stu[i].screo[j]);
}
printf("\n");
}
for (i=0;i<m;i++)
printf("%.1lf ",ave[i]);
return 0;
}
7 階乘中的零
該題目是個演算法類,需要知道規律才能進行
這里有一篇博客專門講了該如何計算階乘后零的個數
https://blog.csdn.net/Z_sea/article/details/80160098
同學們可以去學習一下
題目要求

輸入樣例
6
3
60
100
1024
23456
8735373
輸出樣例
0
14
24
253
5861
2183837
完整代碼
#include <stdio.h>
#include <stdlib.h>
int F(int x)
{
int j,y,count=0;
for(y=1;y<=x;y++)
{
for(j=5;j<=y;j=j*5)
{
if(y%j==0)
count++;
}
}
return count;
}
int main()
{
int a,i;
scanf("%d",&a);
int b[a];
for(i=0;i<a;i++)
scanf("%d",&b[i]);
for(i=0;i<a;i++)
printf("%d\n",F(b[i]));
return 0;
}
8 幸運的數
思路:從最低位開始判斷每一位是否是4或者8,如果是8,則繼續判斷下一位,如果是4,就可以不需要繼續判斷了,直接跳出回圈
題目要求

完整代碼
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int i,flag1=0,flag2=1,a,t;//flag1判斷是否有8,flag2判斷是否存在4
for(i=8;i<=998;i++)
{
flag1=0;
flag2=1;
t=i;
while(t!=0)
{
a=t%10;
if(a==8)
flag1=1;
if(a==4)
{
flag2=0;//若存在4,flag2=0,并跳出回圈
break;
}
t=t/10;
}
if(flag1==1&&flag2==1)
printf("%d\n",i);
}
}
本次考試鏈表的題目比較簡單,都是書本上的基本知識,只要理解了鏈表的結構,就可以做出來,但是可能同學們大多還沒有理解鏈表是啥,先理解,再重復運用,熟能生巧,
理解鏈表可以參考理解陣列,其不同點是
1、存盤方式:陣列在記憶體中是一段連續的空間,而鏈表以結點的形式分散存盤,
2、讀取方式:陣列以下標靈活讀取陣列中的變數,但是鏈表只能單方向的讀取結點中的內容,3、創建方式:創建陣列僅僅需要“陣列名【】”的形式就可以,但是創建一個鏈表需要定義一個結構體,創建一個函式,一個一個的將開辟記憶體空間再將資料讀入,但鏈表的優勢在于可以存盤若干數量和種類的資料,并且可以節省記憶體空間,
鏈表其實就是這么一回事,如果不能很好理解的話,就多打代碼,對照著課本的例題,先模仿,多重復打幾次,僅有的幾個操作無非就是增刪查改,“無他,唯手熟爾”,
9 [填空題]鏈表結點指定位置插入
1、看見題目后,發現是個填空題,這個題目非常友好,鏈表的創建、輸出已經幫我們編好了,觀察主函式,流程是:創建鏈表——列印已創建鏈表——輸入待插入結點資訊、插入位置——插入結點——列印新鏈表,
2、我們所要做的步驟就是插入結點,思路是:遍歷鏈表直到插入位置n,待插入結點指向原來n位置結點所指向的結點,n位置前的結點指向待插入結點
題目要求
下面程式,先創建一個鏈表,然后創建一個新結點stu,并呼叫putinto函式,將新結點插入到第m個結點前(注意,如果m<=1表示插在表頭;m>鏈表結點數表示插在表尾),但putinto未完成,請補充完整,
輸入樣例
3
1
98
7
99
5
87
6
78
2
輸出樣例
1 98
7 99
5 87
1 98
6 78
7 99
5 87
完整代碼
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *creat(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 *putinto (struct student *head, struct student *stu , int n)
{
struct student *p;
int i;
p=head;
for (i=1;i<(n-1)&&p!=NULL;i++) //直到n的前一個結點
{
p=p->next;
}
stu->next=p->next;
p->next=stu;
return head;
}
//
main()
{
struct student *head,*stu;
int n, m;
scanf("%d",&n);
head=creat(n);
print(head);
stu=(struct student *)malloc(LEN);
scanf("%ld",&stu->num);
scanf("%d",&stu->score);
stu->next = NULL;
scanf("%d", &m);
head=putinto (head, stu, m);
print(head);
}
這個程式無法插入到第一個結點處,不過OJ系統過了,應該是題目不考慮插入第一個結點的情況,
10 [填空]鏈表結點的刪除
題目要求:找到結點并洗掉
解題思路:
1、題目比較友好,跟上一題一樣,主體部分都已經給出,只需要自己撰寫洗掉結點的函式就行,
2、觀察主函式,大家試著自己梳理下,
洗掉結點思路:讓待洗掉的結點的上一個結點指向待洗掉結點的下一個結點,但要另外考慮洗掉第一個結點的情況,
輸入樣例
3
1
98
4
99
5
87
4
輸出樣例
1 98
4 99
5 87
1 98
5 87
完整代碼
#include "stdio.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *creat(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 *del(struct student *head,long num)
{
struct student *p,*pre;
p=head;
while (p!=NULL&&p->num!=num) //注意這里的判斷條件是p->!=num,也就是當p->num=num時停下
{
pre=p; //記錄前一個結點
p=p->next;
}
if (p==NULL) //排除鏈表為空的情況
{
return head;
}
else if (p==head) //當需要洗掉的結點為第一個的情況
{
head=head->next;
}
else
{
pre->next=p->next; //待洗掉結點的前一個結點指向待洗掉結點的后一個結點
}
free(p); //釋放空間
return head;
}
//
main()
{
struct student *creat(int n);
void print(struct student *head);
struct student *delete(struct student *head,long num);
struct student *head;
int n;
long del_num;
scanf("%d",&n);
head=creat(n);
print(head);
scanf("%ld",&del_num);
head=del(head,del_num);
print(head);
}
本次考試的鏈表考的不怎么難,都是很基礎的題目,把課本的代碼自己打幾次運行幾次就能融匯貫通了,大家得多動手操作!!!!!!這很重要!!!!!!
臨近考試,同學們要對做過的題目要多理解,做到融會貫通,考試遇到相似的題目就能迎刃而解!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/239546.html
標籤:其他
上一篇:簡麗Framework-開篇
