Problem A.牙膏问题
题目描述
4只牙膏比较价格,返回最便宜的牙膏
输入:
第一行输入T,表示数据的组数
第二行输入a b c d 表示4只牙膏的价格
输出
返回最低价格
样例输入
2
2 3 5 2
3 8 7 4
1
2
3
123456
样例输出:
2
3
1
2
1234
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
//求数组最小值
int t;
scanf("%d",&t);
while(t--){
int a[4];
for(int i=0;i<4;i++){
cin>>a[i];
}
int min=a[0];
for(int i=0;i<4;i++){
if(a[i]<min)min=a[i];
}
cout<<min<<endl;
}
}
Problem B.闰年问题
题目描述
统计连个年份间能被4整除且不能被100整除的普通闰年和能被400整除的世纪闰年的年数
输入:
第一行输入T,表示数据的组数
之后输入T组数据,每组输入x y代表始末年份(y<x)
输出
返回x到y年 ∈[x,y] 内闰年数目count
样例输入
2
2999 3019
1999 2019
1
2
3
123456
样例输出:
4
5
1
2
1234
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
bool isrun(int y){
if(y%400==0||(y%4==0&&y%100!=0)){
return true;
}else return false;
}
int main()
{
int t;
cin>>t;
while(t--){
int a,b;
cin>>a>>b;
int sum=0;
for(int i=a;i<=b;i++){
if(isrun(i))sum++;
}
cout<<sum<<endl;
}
}
Problem C.链表操作
题目描述
一个初值为 1,2,3,4,…,n的n(0<n<=50)个节点的顺序链表,有以下3种操作方式:
1 移除节点值为x的节点
2 翻转链表,对调整个链表的顺序
3 查询链表值为x的节点所指的下一个节点的值
输入:
第一行输入T,表示数据的组数
每组第一行输入 n (表示节点数目) m (表示操作数目)
接下来m行分别输入 op (操作方式) x (操作的节点值,操作方式为2时不会输入该值)
输出:
输出操作为3查询时,所查节点所指的下一个节点值,若没有查到或查到的节点为表尾 (没有下一节点),输出"NONE"
样例输入
1
6 6
3 6
1 2
3 1
2
1 5
3 6
1
2
3
4
5
6
7
8
12345678910111213141516
样例输出
NONE
3
4
123
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *next;
node(int x):data(x),next(NULL){}
};
node *reverse(node *l){
node *n=new node(0);
while(l){
node *s=new node(l->data);
//cout<<s->data<<" ";
s->next=n->next;
n->next=s;
l=l->next;
}
node *r=n->next;
return r;
}
int main()
{
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
node *l = new node(1);
node *q;
q=l;
for(int i=2;i<=n;i++){
node *p=new node(i);
q->next=p;
q=p;
}
while(m--){
int op,num;
cin>>op;
if(op==1){
cin>>num;
if(l->data==num)l=l->next;
while(l->next){
if(l->next->data<num){
l=l->next;
}else break;
}
l->next=l->next->next;
}else if(op==2){
l=reverse(l);
}else if(op==3){
cin>>num;
int flag=0;
node *p;
p=l;
while(p){
if(p->data==num){
flag=1;
break;
}
p=p->next;
}
if(flag==1&&p->next){
cout<<p->next->data<<endl;
}else cout<<"NONE"<<endl;
}
}
}
return 0;
}
Problem D.网络连接
题目描述
有一个保证能联通的计算机网络(连通图),
Aij表示i计算机联通到j计算机的开销,
保证 :Aii = 0 , Aij = Aji ,Aij <= 100;
输入:
第一行输入T (T=1),表示数据的组数
每组第一行输入N (0 < N <= 50),表示网络内计算机的数目
然后n行输入Ai0 - Ai n-1
输出
返回连通这个计算机网络需要的最小开销
输入样例
1
6
0 1 4 3 7 3
1 0 2 5 1 8
4 2 0 3 9 2
3 5 3 0 1 4
7 1 9 1 0 3
3 8 2 4 3 0
输出样例
7
解析
利用图的prime算法计算最短路径
#include<bits/stdc++.h>
using namespace std;
int main(){//prim算法的简单实现,得益于题目提供的方便限制条件
int T;
int i,j,k,n,result,min,pos;
int path[55][55];
bool check[55];
vector<int> point;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
result = 0;
memset(path,0,sizeof(path));
point.clear();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d",&path[i][j]);
}
check[i] = false;
}
if( n < 2 ){
printf("0\n");
continue;
}
check[0] = true;
point.push_back(0);
for(i=1;i<n;i++){//只是计数循环N-1次,因为题目保证了全连通图
min = 124;//取了一个大于题目最大值的数
for(j=0;j<point.size();j++){
for(k=0; k<n; k++){
if ( !check[k] && path[point[j]][k]<min ){
min = path[point[j]][k];
pos = k;
}
}
}
result += min;
point.push_back(pos);
check[pos] = true;
}
printf("%d\n",result);
}
return 0;
}