程序设计题型
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1、输出图形(本题100分) (1)
2、旋转数字(本题100分) (2)
3、求和(本题100分) (3)
4、多项式相乘 (5)
5、二叉树 (9)
6、二叉树1 (13)
7、哈夫曼树及编码(100分) (15)
8、哈夫曼树、编码及译码(100分) (17)
9、判定连通图(100分) (20)
10、Least Time (最短时间,100分) (23)
11、取序号判定素数 (25)
12、名次与分数 (26)
13、快速排序(100分) (30)
14、堆排序(100分) (32)
15、归并排序(100分) (34)
16、基数排序(100分) (36)
17、表达式求值 (38)
18、取数相加 (41)
19、大整数排序 (43)
(graph.cpp)
【题目描述】
编写程序打印n行如下图形,其中1≤n≤26。
n=4输出图形如下:
D
DCD
DCBCD
DCBABCD
【输入】
输入文件graph.in包含1个整数。
【输出】
输出文件graph.out。
【输入输出样例1】
graph.in graph.out
4 D
DCD
DCBCD
DCBABCD
#include<iostream>
using namespace std;
int main()
{
freopen("graph.in","r",stdin);
freopen("graph.out","w",stdout);
int i,j,n;
cin>>n;
for(j=1;j<=n;j++)
{
for(i=1;i<=n-j;i++)
cout<<" ";
for(i=1;i<=j;i++)
cout<<(char)('A'+n-i);
for(i=1;i<=j-1;i++)
cout<<(char)('A'+n+i-j);
cout<<endl;
}
return 0;
}
【题目描述】
编写程序打印n行如下菱形图形(1≤n≤99),图形有n行n列个整数,图形从第1行的中间开始,数字分别是1,2,……n*n-1,n*n,并且顺时针向中间转入,如果1个数据项宽度不足n*n位,用0补足到n*n位。
提醒:图案中没有数字的地方用空格填满,每行最后一个数字后面没有多余的空格。
【输入】
输入文件rotation.in包含1个整数n。
【输出】
输出文件rotation.out是一个菱形图形,图形从第1行的中间开始,数字分别是n*n,n*n-1,n*n-2,……1,并且顺时针向中间转入。
【输入输出样例1】
例如n=7,输出:
01
24 02
23 25 03
22 40 26 04
21 39 41 27 05
20 38 48 42 28 06
19 37 47 49 43 29 07
18 36 46 44 30 08
17 35 45 31 09
16 34 32 10
15 33 11
14 12
13
【限制】
1≤n≤99
【提示】
m=4
i=15;.
printf("%0*d", m, i);//这里用*表示系数待定,m即为指定的宽度
//上面语句输出“0015”,数据宽度为4,不足4位,前补0 printf("%*c", m, ‘ ‘);//左边语句输出“”,即输出4个空格
#include "stdio.h"
const int N0=2*100-1+10;
int a[N0][N0]={ 0 };
struct node
{ int row,col;
}dir[4]={ {1,1},{1,-1},{-1,-1},{-1,1} };
int main()
{
int i,j,n;
int row,col,row1, col1,d,m=0;
freopen("rotation.in", "r", stdin);
freopen("rotation.out","w", stdout);
scanf("%d", &n);
row=1;
col=n;
d=0;
int t=n*n;
while( t!=0 )
{ m++;
t/=10;
}
for( i=1; i<=n*n; i++)
{ a[row][col]=i;
row1=row+dir[d].row;
col1=col+dir[d].col;
if( row1<1 || col1<1 || row1>2*n-1 || col1>2*n-1
|| a[row1][col1]!=0 )
{ d=(d+1)%4;
row1=row+dir[d].row;
col1=col+dir[d].col;
}
row=row1;
col=col1;
}
for( i=1; i<=2*n-1; i++)
{ for( j=1; j<=2*n-1; j++ )
if( a[i][j]!=0 )
printf("%0*d", m, a[i][j]);
else
if( i<=n && j<n+i || i>n && j<3*n-i) printf("%*c", m, ' ');
printf("\n");
}
return 0;
}
3、求和(本题100分)
(total.cpp)
【问题描述】
有1个n×n的矩阵,从左上到右下称为主斜线(倾角135º),从右上到左下称为次斜线(倾角45º),在主斜线上最大,次斜线上最小的元素称为该矩阵的斜线鞍点,求出该矩
阵所有斜线鞍点的和。
如5×5的矩阵:
3 2 5 98 10
44 12 1 12 8
67 61 45 7 19
12 21 33 56 3
35 36 78 90 19
次斜线主斜线
元素7为上面矩阵的一个斜线鞍点, 元素10和19也是上面矩阵的斜线鞍点。
【输入】
输入文件total.in第一行是一个整数n((1≤n≤100)),接下去是n行×n列的矩阵,矩阵的每个元素都是整数。
【输出】
输出文件total.out也只有1个整数,即该矩阵所有斜线鞍点的和(0≤和≤1010)。
【输入输出样例1】
total.in total.out
36
5
3 2 5 98 10
44 12 1 12 8
67 61 45 7 19
12 21 33 56 3
35 36 78 90 19
#include<stdio.h>
#define NO 100
int main()
{
freopen("total.in","r",stdin);
freopen("total.out","w",stdout);
int i,j,x,y,max,min,n=0,sum=0;
scanf("%d",&n);
int**a=new int*[n];
i=-1;
while(++i<n)
{
a[i]=new int[n];
j=-1;
while(++j<n)scanf("%d",a[i]+j);
}
i=-1;
while(++i<n)
{
j=-1;
while(++j<n)
x=i,y=j;
max=1,min=1;
while(x>0&&y>0){x--;y--;}
while(x<n&&y<n)
{
if(a[x][y]>a[i][j])
{
max=0;
break;
}
x++;y++;
}
x=i,y=j;
while(x>0&&y<n-1){x--;y++;}
while(x<n&&y>=0)
{
if(a[x][y]<a[i][j])
{
min=0;
break;
}
x++,y--;
}
if(!max||!min)continue;
else sum+=a[i][j];
}
}
printf("%d\n",sum);
i=-1;
while(++i<n)delete[]a[i];
delete[]a;
return 0;
}
4、多项式相乘
(conv.cpp/c)
【题目描述】
编程实现若干个多项式相乘。
多项式的输入输出格式为:系数在前,指数在后,各项按指数递增排列,每个多项式输入时以两个0结束。
系数为0的项不输出。
例如:1+4X3-9X5输入格式为:1 0 0 1 0 2 4 3 0 4 -9 5 0 0或者 1 0 4 3 -9 5 0 0,其输出只能是:1 0 4 3 -9 5
输入文件conv.in每行为一个多项式,多项式输入时以两个0结束。
数据为若干行的多项式,例如:
1 0 1 1 0 0
1 0 -1 1 0 0
1 0 1
2 0 0
表示(1+x)(1-x)(1+x2)
【输出】
输出文件conv.out包含1行,为上述多项式相乘结果。
上例的输出为:
1 0 -1 4
表示1-x4
【输入输出样例1】
conv.in conv.out
1 0 -1 4
1 0 1 1 0 0
1 0 -1 1 0 0
1 0 1
2 0 0
【数据限制】
所有系数、指数均为整数(int类型)
#include "stdio.h"
typedef struct node
{
int c,e;
struct node *next;
}ND;
ND *createLink()
{ ND *head, *p;
head=p=new ND;
int c,e;
while( true )
{
if( scanf("%d%d", &c, &e)!=2 ) break;
if( c==0 && e==0 ) break;
p->next=new ND;
p=p->next;
p->c=c;
p->e=e;
}
p->next=NULL;
return head;
}
void printLink( ND *head )
{ ND *p=head->next;
while( p )
{
printf("%d %d ", p->c, p->e);
p=p->next;
}
printf("\n");
}
void freeLink( ND *head )
{
ND *p;
while( head )
{ p=head;
head=head->next;
delete p;
}
}
ND *addPoly( ND *ha, ND *hb )
{ ND *hc, *pc, *pa=ha->next, *pb=hb->next;
hc=pc=new ND;
int c, e;
while( pa || pb )
{
if( pa && (pb==NULL || pa->e<pb->e) )
{ c=pa->c;
e=pa->e;
pa=pa->next;
}
else if( pb && (pa==NULL || pb->e<pa->e) )
{ c=pb->c;
e=pb->e;
pb=pb->next;
}
else
{ c=pa->c+pb->c;
e=pa->e;
pa=pa->next;
pb=pb->next;
}
if( c )
{ pc->next=new ND;
pc=pc->next;
pc->c=c;
pc->e=e;
}
}
pc->next=NULL;
return hc;
}
ND *oneXmulty( ND *pa, ND *hb ) { ND *hc, *pc, *pb=hb->next;
hc=pc=new ND;
while( pb )
{
pc->next=new ND;
pc=pc->next;
pc->c=pa->c*pb->c;
pc->e=pa->e+pb->e;
pb=pb->next;
}
pc->next=NULL;
return hc;
}
ND *multyXmulty( ND *ha, ND *hb ) { ND *hc, *ht, *pa=ha->next;
hc=new ND;
hc->next=NULL;
while( pa )
{
ht=oneXmulty( pa, hb );
hc=addPoly( hc, ht );
freeLink( ht );
pa=pa->next;
}
return hc;
}
int main()
{ ND *ha, *hc;
freopen("conv.in", "r", stdin);
//freopen("conv.out", "w", stdout);
hc=createLink();
while( true )
{
ha=createLink();
if( ha->next==NULL ) break;
hc=multyXmulty( hc, ha);
freeLink( ha );
}
printLink( hc );
freeLink( hc );
return 0;
}
5、二叉树
(bTree.cpp/c)
【题目描述】
已知二叉树的先序遍历序列和中序遍历序列,输出其后序遍历序列和层次序遍历序列。
【输入】
输入文件bTree.in有二行,分别是二叉树的先序遍历序列和中序遍历序列。
【输出】
输出文件bTree.out包含二行,分别是上述二叉树的后序遍历序列和层次序遍历序列。
【输入输出样例1】
bTree.in bTree.out
abdeijfcgh dijefbghca jifedhgcba abdcegifhj
【数据限制】
所有序列字串长度<=100
#include "stdio.h"
#include "string.h"
const int N0=100;
struct node
{ char data;
int lch, rch;
}tree[N0+1];
int root=1;
void createTree( int root, char *pri, char *mid)
{ char *p;
int k;
if( root==0 || *pri=='\0' || *mid=='\0') return;
tree[root].data=*pri;
p=strchr( mid, *pri );
*p='\0'; // 中序:mid(1) p+1(2)
k=strlen(mid); // 先序:pri+1(1) , pri+k+1(2) if( k>0 )
{ tree[root].lch = root+1;
createTree( root+1,pri+1, mid);
}
if( strlen( p+1)>0 )
{ tree[root].rch = root+k+1;
createTree( root+k+1, pri+k+1, p+1);
}
}
void postOrder( int root )
{ if( root )
{
postOrder( tree[root].lch );
postOrder( tree[root].rch );
printf("%c", tree[root].data);
}
}
void layerOrder( int root)
{ int qu[N0+5],f=0,r=0;
int t;
if( root==0 ) return;
qu[ r++ ]=root;
while( r!=f )
{ t=qu[f++];
printf("%c", tree[t].data);
if( tree[t].lch )
qu[ r++ ] = tree[t].lch;
if( tree[t].rch )
qu[ r++ ] = tree[t].rch;
}
}
void showTree( int root, int tab, char ch )
{ if( root==0 ) return;
int i;
for( i=1; i<tab; i++)
printf(" ");
printf("%c(%c)\n", tree[root].data, ch);
if( tree[root].lch )
showTree( tree[root].lch, tab+6, 'L' );
if( tree[root].rch )
showTree( tree[root].rch, tab+6, 'R' ); }
int high( int root )
{ if( root==0 ) return 0;
int lh, rh;
lh=high( tree[root].lch );
rh=high( tree[root].rch );
return 1+( lh>rh ? lh : rh);
}
int leafNumber( int root )
{
if( root==0 ) return 0;
if( tree[root].lch==0 && tree[root].rch==0 )
return 1;
return leafNumber( tree[root].lch )+ leafNumber( tree[root].rch) ; }
int main()
{ char pri[N0+1], mid[N0+1];
freopen("Btree.in", "r", stdin);
// freopen("Btree.out", "w", stdout);
gets( pri );
gets( mid );
createTree( root , pri, mid);
postOrder( root );
printf("\n");
layerOrder( root );
printf("\n");
showTree( root, 1, 'T' );
printf("%d\n", high( root ));
printf("%d\n", leafNumber( root));
return 0;
}
方法二:
#include <stdio.h>
#include <string.h>
struct Node
{
char ch;
Node *lch;
Node *rch;
}*root;
char pri[105], mid[105];
Node* buildTree(char *pri, char *mid)
{
char *pos = strchr(mid, *pri);
if(pos)
{
Node *temp = new Node;
temp->ch = *pos;
temp->lch = temp->rch = NULL;
*pos = '\0';
int k = strlen(mid);
if(*(pri + 1))
temp->lch = buildTree(pri+1, mid);
if(*(pos + 1))
temp->rch = buildTree(pri+k+1, 1+pos);
return temp;
}
return NULL;
}
void aftShowTree(Node *root)
{
if(root != NULL)
{
aftShowTree(root->lch);
aftShowTree(root->rch);
putchar(root->ch);
}
}
void levShowTree(Node *root)
{
Node *arr[105] = {0}, *p;
int top = 0, tail = 0;
arr[++tail] = root;
while(tail != top)
{
p = arr[++top];
if(p->lch)
arr[++tail] = p->lch;
if(p->rch)
arr[++tail] = p->rch;
putchar(p->ch);
}
}
void free(Node *root)
{
if(root)
{
if(root->rch)
free(root->rch);
if(root->lch)
free(root->lch);
delete root;
}
}
int main(void)
{
gets(pri);
gets(mid);
root = buildTree(pri, mid);
aftShowTree(root);puts("");
levShowTree(root);puts("");
free(root);
return 0;
}
6、二叉树1
(bTree1.cpp/c)
【题目描述】
已知一颗二叉树的先序遍历序列和中序遍历序列,输出该树的高度和该树的叶子数。
【输入】
输入文件bTree1.in有二行,分别是一颗二叉树的先序遍历序列和中序遍历序列。
【输出】
输出文件bTree1.out只有一行,包含两个整数,分别是该树的高度和叶子数,两个整数之间用一个空格隔开。
【输入输出样例1】
bTree1.in bTree1.out
6 3
abdeijfcgh
dijefbghca
【数据限制】
1<=所有序列字串长度<=100
#include "stdio.h"
#include "string.h"
struct Tr
{
char da;
Tr *lchild,*rchild;
};
Tr *Renew(char *bch,char *mch,int len)
{
Tr *root;
int l;
char *temp;
if(len<=0)
{
root=NULL;
return NULL;
}
root=new Tr;
root->da=*bch;
for(temp=mch;temp<mch+len;temp++)
{
if(*temp==*bch) break;
}
l=strlen(mch)-strlen(temp);
root->lchild=Renew(bch+1,mch,l);
root->rchild=Renew(bch+1+l,temp+1,len-1-l); return root;
}
void freet(Tr *root)
{
if(root->lchild!=NULL)
freet(root->lchild);
if(root->rchild!=NULL)
freet(root->rchild);
delete root;
return;
}
int depth(Tr *root)
{
int h,lh,rh;
if(root==NULL)
h=0;
else
{
lh=depth(root->lchild);
rh=depth(root->rchild);
if(lh>rh)
h=lh+1;
else
h=rh+1;
}
return h;
}
int leaf(Tr *root)
{
int num=0;
if(root!=NULL)
if(root->lchild==NULL&&root->rchild==NULL) num++;
else
num=leaf(root->lchild)+leaf(root->rchild); return num;
}
{
int len;
char bch[100],mch[100];
Tr *root;
freopen("Btree1.in","r",stdin);
freopen("Btree1.out","w",stdout);
gets(bch);
gets(mch);
len=strlen(bch);
root=Renew(bch,mch,len);
printf("%d %d",depth(root),leaf(root));
freet(root);
return 0;
}
7、哈夫曼树及编码(100分)
(huffman.cpp)
【题目描述】
给定一篇用于通信的英文电文,统计该电文中每个字符出现的频率,按频率左小右大的
方法为这些字符建立哈夫曼(Huffamn)树,并编出每个字符的哈夫曼树码,输出该电文的哈
夫曼码译文。
【输入】
输入文件huffman.in是一篇用于通信的英文电文。
【输出】
输出文件huffman.out输出该电文的哈夫曼码译文。
【输入输出样例1】
huffman.in huffman.out
aaccdddbacbcddddddd 011011000011101001100010001111111 【数据限制】
2<=英文电文字符数<=10000000
#include "stdio.h"
#include "string.h"
const int N0=256;
const int INF=100000;
struct node1
{ int w, lch, rch, parent;
}ht[N0+1];
struct node2
{ char ch;
char code [N0+1];
int start;
int n, root;
char str[INF];
void readData()
{ int a[N0]={ 0 }, p=0;
char ch;
while( scanf("%c", &ch)==1 )
{
a[ch]++; // ch='a', a[97]=1; ch='b', a[98]=1;
// a[97]=3, a[98]=2, a[99]=4, a[100]=10
str[p++]=ch;
}
str[p]='\0';
n=0;
int i;
for( i=0; i<256; i++)
if( a[i] )
{ n++;
ht[n].w=a[i];
hc[n].ch=i;
}
}
void selectMin( int t, int *s1, int *s2 )
{ int min1=INF, min2=INF, i;
for( i=1; i<=t; i++)
if( ht[i].parent==0 )
if( ht[i].w<min1 )
{ min2=min1;
*s2=*s1;
min1=ht[i].w;
*s1=i;
}
else if( ht[i].w<min2 )
{ min2=ht[i].w;
*s2=i;
}
}
void createHtHc()
{ int s1, s2, w, child, parent, i;
root=2*n-1;
for( i=n+1; i<=root; i++)
{ selectMin( i-1, &s1, &s2 );
ht[i].w=ht[s1].w+ht[s2].w;
ht[i].lch=s1;
ht[i].rch=s2;
ht[s1].parent=ht[s2].parent=i;
}
for( i=1; i<=n; i++ )
{ child=i;
parent=ht[child].parent;
while( child!=root )
{ if( child==ht[parent].lch )
hc[i].code[hc[i].start++]='0';
else
hc[i].code[hc[i].start++]='1';
child=parent;
parent=ht[child].parent;
}
}
}
void str2code( char str[], char code[] )
{ int i,j,k, p=0;
for( i=0; str[i]; i++ )
{ for( j=0; j<=n; j++ )
if( str[i]==hc[j].ch )
break;
for( k=hc[j].start-1; k>=0; k-- )
code[p++]=hc[j].code[k];
}
code[p]=0;
}
int main()
{ char code[INF];
freopen("huffman.in", "r", stdin);
freopen("huffman.out", "w", stdout);
readData();
createHtHc();
str2code( str, code );
puts( code );
return 0;
}
8、哈夫曼树、编码及译码(100分)
(codeToTxt.cpp)
【题目描述】
给定2个输入文件,第1个输入文件是用于通信的英文电文,统计该电文中每个字符出现的频率,按频率左小右大的方法为这些字符建立哈夫曼(Huffamn)树,并编出每个字符的
哈夫曼树码;第2个输入文件是已经按第1个输入文件的英文电文编好的哈夫曼码,输出该哈夫曼码的对应的英文电文。
【输入】
第1个输入文件为huffman.in是用于通信的英文电文, 第2个输入文件codeToTxt.in是已经按第1个输入文件编好的哈夫曼码。
【输出】
输出文件codeToTxt.out输出codeToTxt.in文件内容的英文电文。
【输入输出样例1】
huffman.in codeToTxt.in codeToTxt.out
aaccdddbacbcddddddd 011111011000011101001100010001111 adddaccdddbacbcdddd 【数据限制】
2<=英文电文字符数<=10000000
#include "stdio.h"
#include "string.h"
const int N0=256;
const int INF=100000;
struct node1
{ int w, lch, rch, parent;
}ht[N0+1];
struct node2
{ char ch;
char code [N0+1];
int start;
}hc[N0+1];
int n, root;
char str[INF];
void readData()
{ int a[N0]={ 0 }, p=0;
char ch;
while( scanf("%c", &ch)==1 )
{
a[ch]++; // ch='a', a[97]=1; ch='b', a[98]=1;
// a[97]=3, a[98]=2, a[99]=4, a[100]=10
str[p++]=ch;
}
str[p]='\0';
n=0;
int i;
for( i=0; i<256; i++)
if( a[i] )
{ n++;
ht[n].w=a[i];
hc[n].ch=i;
}
}
void selectMin( int t, int *s1, int *s2 )
{ int min1=INF, min2=INF, i;
for( i=1; i<=t; i++)
if( ht[i].parent==0 )
if( ht[i].w<min1 )
{ min2=min1;
*s2=*s1;
min1=ht[i].w;
*s1=i;
}
else if( ht[i].w<min2 )
{ min2=ht[i].w;
*s2=i;
}
}
void createHtHc()
{ int s1, s2, w, child, parent, i;
root=2*n-1;
for( i=n+1; i<=root; i++)
{ selectMin( i-1, &s1, &s2 );
ht[i].w=ht[s1].w+ht[s2].w;
ht[i].lch=s1;
ht[i].rch=s2;
ht[s1].parent=ht[s2].parent=i;
}
for( i=1; i<=n; i++ )
{ child=i;
parent=ht[child].parent;
while( child!=root )
{ if( child==ht[parent].lch )
hc[i].code[hc[i].start++]='0';
else
hc[i].code[hc[i].start++]='1';
child=parent;
parent=ht[child].parent;
}
}
}
void code2str( char *code, char *str )
{ char *p=code, *q=str;
int r=root;
while( *p )
{ if( *p=='0' )
r=ht[r].lch;
else
r=ht[r].rch;
if( ht[r].lch==0 )
{ *q++=hc[r].ch;
r=root;
}
p++;
}
*q='\0';
}
int main()
{ char code[INF];
freopen("huffman.in", "r", stdin);
readData();
freopen("codeToTxt.in", "r", stdin);
gets(code);
freopen("codeToTxt.out", "w", stdout);
createHtHc();
code2str( code, str );
puts( str );
return 0;
}
9、判定连通图(100分)
(dist.cpp)
【题目描述】
给你一张描述n个城镇的公路表(n*n的对称矩阵A),请判定这n个城镇是否是连通的。
公路表中表示了任意两个城镇的连通情况,矩阵元素a(i,j)=0表示城镇i,j不连通,a(i,j)!=0表示城镇i到城镇j的距离。
【输入】
输入文件dist.in的第一行为一个自然数n(1<n<=30);
接着n行,每行n个整数,是这n个城镇的公路表(元素的值小于1000)。
【输出】
输出文件dist.out包括一行,如果n个城镇是连通的输出Yes,否则输出No。
【输入输出样例1】
dist.in dist.out
Yes
6
0 1 3 4 9 0
1 0 9 9 0 0
3 9 0 0 6 8
4 9 0 0
5 7
9 0 6 5 0 4
0 0 8 7 4 0
【输入输出样例2】
dist.in dist.out
No
6
0 1 0 4 9 0
1 0 0 9 0 0
0 0 0 0 0 8
4 9 0 0 0 0
9 0 0 0 0 0
0 0 8 0 0 0
【数据限制】
1<n<=30
#include "stdio.h"
const int N0=100;
const int INF=100000;
int map[N0][N0];
int n;
void readData()
{ int i,j,t;
scanf("%d", &n);
for( i=1; i<=n; i++)
for( j=1; j<=n; j++)
{ scanf("%d", &t);
if( i==j )
map[i][j]=0;
else if( t==0 )
map[i][j]=INF;
else map[i][j]=t;
}
}
/*void DFS( int x )
{ int s[N0], top=0, mark[N0]={ 0 }, p, i;
printf("%d", x);
mark[x]=1;
s[++top]=x; //push
while( top )
{ p=s[top];//peak
for( i=1; i<=n; i++)
if( i!=p && mark[i]==0 && map[p][i]!=INF )
{ printf("%d", i);
mark[i]=1;
s[++top]=i;
break;
}
if( i>n )
top--;//pop
}
printf("\n");
}
void BFS( int x )
{ int q[N0], f=0, r=0, mark[N0]={ 0 }, p, i;
printf("%d", x);
mark[x]=1;
q[r++]=x;
while( r!=f )
{ p=q[f++];
for( i=1; i<=n; i++)
if( i!=p && mark[i]==0 && map[p][i]!=INF )
{ printf("%d", i);
mark[i]=1;
q[r++]=i;
}
}
printf("\n");
}*/
void check( int x )
{ int s[N0], top=0, mark[N0]={ 0 }, p, i;
int num=0;
num++; // printf("%d", x);
mark[x]=1;
s[++top]=x; //push
while( top )
{ p=s[top];//peak
for( i=1; i<=n; i++)
if( i!=p && mark[i]==0 && map[p][i]!=INF )
{ num++;// printf("%d", i);
mark[i]=1;
s[++top]=i;
break;
}
if( i>n )
top--;//pop
}
if( num==n ) printf("Yes\n");
else printf("No\n");
}
int main()
{
freopen("dist.in", "r", stdin);
freopen("dist.out", "w", stdout);
readData();
check( 1 );
return 0;
}
10、Least Time (最短时间,100分)
(Least.cpp/c)
【Description】
You are a spy, and you have stolen some top secret of the enemy, now you are to find a way which takes you least time to escape.
There are some many cross-points and some many roads, even two cross-points there can be multiple roads. You can assume that the cross-points are numbered from 1 to n. Cross-point 1 is your starting point and you need to go to cross-point n. All roads are bidirectional(双向的).
【Input】
The first line contains two integers n and m(1≤n≤200, 0≤m≤10000), number of cross-points and number of roads respectively. Next m lines has three integers i, j, k ( i≠j, 1≤k≤10000), indicating there is a road of length k connecting cross-point i to cross-point j.
【Output】
Output one number, the shortest distance from cross-point 1 to corss-point n. If there is no path exist, output -1. No extra spaces are allowed.
【Sample Input/Sample Output 1】
Least.in Least.out
4 6
13
1 2 5
1 3 4
2 4 8
3 4 9
1 2 10
2 4 11
【Sample Input/Sample Output 2】
Least.in Least.out
6 7
-1
1 2 5
1 3 4
2 4 8
3 4 9
5 6 10
2 4 11
6 5 15
#include "stdio.h"
const int N0=205;
const int INF=1000000;
int map[N0][N0];
int n,m;
void readData()
{ int i,j, k;
scanf("%d%d", &n,&m);
for( i=1; i<=n; i++)
for( j=1; j<=n; j++)
if( i!=j )
map[i][j]=INF;
while( scanf("%d%d%d", &i, &j, &k)==3 )
{ if(k<map[i][j])
{map[i][j]=k;
map[j][i]=k;}
}
}
void dijkstra( int x )
{ int dist[N0],mark[N0]={ 0 };
int i,j,k,min;
mark[x]=1;
for( i=1; i<=n; i++)
{
dist[i]=map[x][i];
}
for( i=1; i<n; i++)
{ min=INF;
for( j=1; j<=n; j++)
if( mark[j]==0 && dist[j]<min )
{ min=dist[j];
k=j;
}
if( min==INF ) break;
mark[k]=1;
for( j=1; j<=n; j++)
if( mark[j]==0 && dist[j]>dist[k]+map[k][j] )
{ dist[j]=dist[k]+map[k][j];
}
}
if(dist[n]!=INF) printf("%d",dist[n]);
else printf("-1");
}
int main()
{ freopen("Least.in", "r", stdin);
freopen("Least.out", "w", stdout);
readData();
dijkstra( 1 );
return 0;
}
11、取序号判定素数
(prime.cpp/c)
【题目描述】
一个数组a[0]到a[n-1]存放有n整数,其中2≤n≤100。
求出n整数中最大数a[m1]和次最大数a[m2],并判定m1+m2是否为素数。
【输入】
输入文件prime.in包含n个整数。
【输出】
输出文件prime.out包含两行第1行是m1和m2,第2行若m1+m2为素数则输出Yes否则输出No。
【输入输出样例1】
prime.in prime.out
2 3 5 1 5 8 -2 -1 5 2
Yes
【输入输出样例2】
prime.in prime.out
123456789 33333 777 54321 99999 0 4
No
【限制】
10-10≤整数a[i] ≤10+10,2≤n≤100
#include "stdio.h"
const int MAX=100;
void prime(int a[],int l)
{
int i,m1,m2,flag=0;
m1=m2=0;
for(i=1;i<l;i++)
{
if(a[i]>a[m1])
{
m1=i;
}
}
if(m1==0) m2=1;
for(i=1;i<l;i++)
{
if(i!=m1)
{
if(a[i]>a[m2])
m2=i;
}
}
printf("%d %d\n",m1,m2);
for(i=1;i<=m1+m2;i++)
{
if((m1+m2)%i==0) {flag++;}
}
if(flag<=2) printf("Yes");
else printf("No");
}
int main()
{
freopen( "prime.in", "r", stdin);
freopen( "prime.out", "w", stdout);
int a[MAX],len=0,i=0;
while(true)
{
if(scanf("%d",&a[i])!=1) break;
i++;
len++;
}
prime(a,len);
return 0;
}
12、名次与分数
(score.cpp/c)
【问题描述】
输入数据是我校ACM集训队参加某次ACM网络联赛的成绩和账号,数据分上下两部分,
数据上半部分第1列为名次,第2列为账号,第3列为AC(完成的)题目数。
数据下半部分为我校ACM集训队队员的姓名和账号。
任务1:将我校ACM集训队队员的成绩排序,即做一个队内排名。
任务2:计算每个队员的成绩。
成绩计算方法:
①没有参加网赛,成绩=0
②有参加网赛,网赛AC 0 题,成绩=10
③网赛AC题数≥1,成绩=101-队内名次*sqrt(队内名次) 【注:sqrt为平方根】例如李饶立同学队内名次=1,则其成绩=101-1*sqrt(1)=100.000
再如林哲斌同学队内名次=2, 则其成绩=101-2*sqrt(2)=98.172。
输出结果为两列:姓名和成绩,成绩保留3位小数,第4位四舍五入,按成绩由高到低排列。
【输入】
输入文件score.in为ACM网络联赛的成绩和帐号。
【输出】
输出文件score.out结果为按成绩由高到低排序的两列:姓名和成绩,成绩保留3位小数,第4位四舍五入。
【输入输出样例1】
score.in score.out
1 SZU2011_FM1_00
2 4
2 ZHBIT2011_FM1_001 3
3 SZU2011_FM1_01
4 3
4 FJUT2011_FM1_001 3
5 SZU2011_FM1_004 3
6 SZU2011_FM1_013 3
7 FJAU2011_FM1_002 3
8 SZU2011_FM1_010 3
9 FJUT2011_FM1_004 3
10 FJUT2011_FM1_009 3
11 FJUT2011_FM1_002 3
12 ZHBIT2011_FM1_002 3
13 FJAU2011_FM1_007 2
14 SZU2011_FM1_008 2
15 FJAU2011_FM1_018 2
16 FJAU2011_FM1_005 2
17 SZU2011_FM1_001 2
18 SZU2011_FM1_009 2
19 SZU2011_FM1_007 2
20 FJAU2011_FM1_022 2
21 FJAU2011_FM1_011 2
22 ZHBIT2011_FM1_003 2
23 FJUT2011_FM1_011 2 李饶立 100.000 林哲斌 98.172 李绍江 95.804 林剑辉 93.000 李福彬 89.820 谢荣祥 86.303 罗雄飞 82.480 李光泽 78.373 程千兴 74.000 沈炬 69.377
李华均 64.517 黄艳丽 59.431 陈贻劲 54.128 朱智佳 48.617 苏桂明 42.905 魏述文 37.000 杨少群 10.000 卢丽娜 10.000 彭秦中 0.000
陈竞郴 0.000
吴胜杰 0.000
郑志强 0.000
邱一潮 0.000
24 FJAU2011_FM1_015 2
25 FJAU2011_FM1_006 2
26 FJAU2011_FM1_008 2
27 FJUT2011_FM1_010 2
28 SZU2011_FM1_018 2
29 FJNU2011_FM1_010 2
30 SZU2011_FM1_015 2
31 FJAU2011_FM1_014 2
32 FJAU2011_FM1_020 2
33 FJAU2011_FM1_009 2
34 FJUT2011_FM1_006 1
35 SZU2011_FM1_011 1
36 SZU2011_FM1_005 1
37 FJUT2011_FM1_012 1
38 FJAU2011_FM1_025 1
39 FJUT2011_FM1_007 1
40 FJAU2011_FM1_001 1
41 FJUT2011_FM1_008 1
42 FJAU2011_FM1_023 1
43 FJUT2011_FM1_013 1
44 FJNU2011_FM1_009 1
45 FJUT2011_FM1_005 1
46 FJAU2011_FM1_024 1
47 FJNU2011_FM1_020 0
48 FJAU2011_FM1_012 0
49 FJAU2011_FM1_017 0
50 SZU2011_FM1_016 0
姓名账号
朱智佳 FJAU2011_FM1_001 李饶立 FJAU2011_FM1_002 彭秦中 FJAU2011_FM1_003 陈竞郴 FJAU2011_FM1_004 林剑辉 FJAU2011_FM1_005 李光泽 FJAU2011_FM1_006 林哲斌 FJAU2011_FM1_007 林雪勇 0.000 吴灿南 0.000
程千兴 FJAU2011_FM1_008
黄艳丽 FJAU2011_FM1_009
吴胜杰 FJAU2011_FM1_010
谢荣祥 FJAU2011_FM1_011
杨少群 FJAU2011_FM1_012
郑志强 FJAU2011_FM1_013
沈炬FJAU2011_FM1_014
罗雄飞 FJAU2011_FM1_015
邱一潮 FJAU2011_FM1_016
卢丽娜 FJAU2011_FM1_017
李绍江 FJAU2011_FM1_018
林雪勇 FJAU2011_FM1_019
李华均 FJAU2011_FM1_020
吴灿南 FJAU2011_FM1_021
李福彬FJAU2011_FM1_022
苏桂明FJAU2011_FM1_023
魏述文FJAU2011_FM1_024
陈贻劲FJAU2011_FM1_025
#include "stdio.h"
#include "math.h"
#include "string.h"
const int N0=1000;
struct node
{ int mc;
char name[15],num[30];
int ac;
double score;
}student[N0+1];
int n=0,totalNumber=0;
void readData()
{ char s[1000];
bool data1=true;
while( gets(s)!=NULL )
{ if( strcmp( s, "" )==0 ) continue;
if( strstr( s, "姓名")!=NULL && strstr(s, "账号")!=NULL )
{ data1=false;
continue;
}
if( data1 && strstr( s, "FJAU")!=NULL )
{ int t;
n++;
totalNumber++;
student[n].mc=n;
sscanf( s,"%d%s%d", &t, student[n].num, &student[n].ac );
if( student[n].ac==0 )
student[n].score=10;
else
student[n].score=101-n*sqrt(n);
}
if( !data1 )
{ int i;
char name1[15], num[30];
sscanf( s, "%s%s", name1, num );
for( i=1; i<=n; i++)
if( strcmp( num, student[i].num )==0 )
{ strcpy(student[i].name, name1);
break;
}
if( i>n )
{ totalNumber++;
strcpy(student[totalNumber].name,name1);
student[totalNumber].score=0;
}
}
}
}
void printData()
{ int i;
for( i=1; i<=totalNumber; i++)
printf("%s %.3f\n", student[i].name, student[i].score);
}
int main()
{
freopen("score.in", "r", stdin);
// freopen("score.out", "w", stdout);
readData();
printData();
return 0;
}
13、快速排序(100分)
(quickSort.cpp)
【题目描述】
给定n个整数作从小到大快速排序,每次分组以本组的左边第1个数作标准元素,用两个指针向中间移动的分组策略,完成快速排序,输出分组次数。
【输入】
输入文件quickSort.in的第一行为一个自然数n(1<n≤30000), 接着若干行共有n个整数,每个整数之间用空格隔开。
【输出】
输出文件quickSort.out包括一个整数,完成快速排序整个过程的分组次数。
【输入输出样例1】
quickSort.in quickSort.out
4
2
5 6 1 2
【输入输出样例2】
quickSort.in quickSort.out
3
4
1 6
2 5
【数据限制】
1<n≤30000
#include "stdio.h"
const int N0=30005;
int n,a[N0];
int num;
int divider(int a[],int s,int t)
{
int i,j;
int c,temp;
i=s; j=n;
temp=a[s];
do
{while((a[j]>=temp)&&(i<j))
j--;
if(i<j)
{
a[i]=a[j];
i++;
}
while((a[i]<=temp)&&(i<j))
i++;
if(i<j)
{
a[j]=a[i];
j--;。