注意:这一年所有题目都要文件操作
1.普通排序 【normalsort】
描述
牛牛是一位编程爱好者,今天第一次参加初中组比赛,看到第一题,不要紧张,来一个简单的排序题做一做,牛牛学过了很多排序,一直想练个手,这回机会来了,给牛牛N个数(n<=100),每个数都在(0~1000)之间,其中由很多重复的数字,请将重复的数字只保留一个,并将剩下的数由小到大排序并输出。
输入
输入文件名为normalsort.in。
输入有2行,
第1行为1个正整数,表示数的个数:N
第2行有N个用空格隔开的整数。
输出
输出文件名为normalsort.out。
第1行为1个正整数M,表示不相同数的个数。
接下来的一行,从小到大输出排好序的不相同的数,每个数用逗号隔开。
输入样例 1复制
10 20 40 32 67 40 20 89 99 100 15
输出样例1复制
8 15,20,32,40,67,89,99,100
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("normalsort.in","r",stdin);
freopen("normalsort.out","w",stdout);
int n,a[101],tong[1145]={0},cnt=0,e=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
tong[a[i]]++;
}
for(int i=0;i<=1000;i++){
if(tong[i]!=0){
cnt++;
}
}
cout<<cnt<<endl;
for(int i=0;i<=1000;i++){
if(tong[i]!=0){
cout<<i;
e++;
if(e!=cnt){
cout<<",";
}
}
}
return 0;
}
以上是桶排序,如果用其他排序应该也行
2.进制转换 【system】
描述
牛牛在学习计算机基础的时候听过计算机中数据都是以二进制的形式保存的,随后对进制产生了兴趣,比如生活中最普通的十进制,由0,1,2,3,4,5,6,7,8,9十个数字构成,逢十进一,用10代表十,而二进制由0,1构成,逢二进一,用10代表二。那么如何将十进制转换成对应的进制呢?请你编写程序帮助牛牛解决这个问题,将一个十进制数X转换成任意进制数M(2<=M<=32)
输入
输入文件名为system.in。
一行,整数X和M,X<=109,M<=32。
输出
输出文件名为system.out。
十进制数X的M进制数。
输入样例 1复制
11 2
输出样例1复制
1011
提示
【数据范围以及提示】
对于十六进制来说,10在十六进制中是A,11在十六进制中是B,12在十六进制中是C,13在十六进制中是D,14在十六进制中是E,15在十六进制中是F
比如十六进制中的1F换算程十进制就是31.
对于三十二进制来说,比如16在三十二进制中是G,依次类推
十进制转任意进制,十分地好做
下面用栈来写一遍,用数组也行,用栈代码更短
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("system.in","r",stdin);
freopen("system.out","w",stdout);
int n,b;
stack<int>q;
cin>>n>>b;
while(n){
q.push(n%b);
n/=b;
}
while(!q.empty()){
int a=q.top();
if(a<10){
cout<<a;
}else{
cout<<(char)(a-10+'A');
}
q.pop();
}
return 0;
}
3.重复出现的数 【number】
描述
牛牛同学拿到了2组数字,请你编程帮他找出,第2组数中的哪些数,在第1组数中出现了,从小到大输出所有满足条件的数。
比如:
第1组数有:8 7 9 8 2 6 3
第2组数有:9 6 8 3 3 2 10
那么应该输出:2 3 3 6 8 9
输入
输入文件名为number.in。
第一行两个整数n和m,分别代表2组数的数量
第二行n个正整数
第三行m个正整数
对于60%的数据 1≤n,m≤1000,每个数<=2×10^9<=2×10^9
对于100%的数据1≤n,m≤100000,每个数<=2×10^9<=2×10^9
输出
输出文件名为number.out。
按照要求输出满足条件的数,数与数之间用空格隔开
输入样例 1复制
7 7 8 7 9 8 2 6 3 9 6 8 3 3 2 10
输出样例1复制
2 3 3 6 8 9
#include<bits/stdc++.h>
using namespace std;
int a[100005],b[100005],c[100005],n,m,k;
int main(){
freopen("number.in","r",stdin);
freopen("number.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
}
for(int i=1;i<=m;i++){
scanf("%d",b+i);
}
sort(a+1,a+1+n);
sort(b+1,b+1+m);
int x=1,y=1;
while(x<=n&&y<=m){
while(x<=n&&a[x]<b[y]){
x++;
}
if(b[y]==a[x]){
printf("%d ",b[y]);
}
y++;
}
return 0;
}
4.最短的路 【short】
描述
牛牛被邀请参加安徽省田径爱好者聚会,是在城市n,而牛牛当前所处的城市为1,附近还有很多城市2~n-1,有些城市之间没有直接相连的路,有些城市之间有直接相连的路,这些路都是双向的,当然也可能有多条。
现在给出直接相邻城市的路长度,牛牛想知道从城市1到城市n,最短多少距离。
输入
输入文件名为short.in。
输入n, m,表示n个城市和m条路;
接下来m行,每行a b c, 表示城市a与城市b有长度为c的路。
1≤n≤2000。
1≤m≤10000。
0≤c≤10000。
输出
输出文件名为short.out。
输出1到n的最短路。如果1到达不了n,就输出-1。
输入样例 1复制
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
输出样例1复制
90
典型bfs最短路径问题
#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[2005][2005],c[2005],b[2005];
int main(){
freopen("short.in","r",stdin);
freopen("short.out","w",stdout);
memset(a,127,sizeof(a));
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y,z;
cin>>x>>y>>z;
a[x][y]=a[y][x]=min(a[x][y],z);
}
for(int i=1;i<=n;i++) c[i]=a[1][i];
c[1]=0;
b[1]=1;
for(int i=1;i<n;i++){
int mi=0x7fffffff;
int k=0;
for(int j=1;j<=n;j++){
if(!b[j]&&c[j]<mi){
mi=c[j];
k=j;
}
}
if(!k) break;
b[k]=1;
for(int j=1;j<=n;j++){
c[j]=min(c[k]+a[j][k],c[j]);
}
}
if(c[n]<0){
cout<<"-1";
return 0;
}
cout<<c[n];
return 0;
}