目录
题目一:
代码:
题目二:
代码:
题目三:
代码:
题目四:
代码:
题目五:
代码:
题目六:
代码:
题目七:
代码:
题目八:
代码:
题目九:
代码:
题目十:
代码:
题目一:
代码:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
float a[1010];
float sum=0;
for(int i=1;i<=n;i++)//求倒数
{
cin>>a[i];
sum+=1.0/a[i];
}
float aver=sum/n;//求倒数的平均
printf("%.2f",1.0/aver);//求倒数的平均的倒数
}
题目二:
代码:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int a[5],cha,minn;
cin>>a[1]>>a[2]>>a[3]>>a[4]>>minn>>cha;
int flagmax=0;
for(int i=1;i<=4;i++)
{
cin>>a[i];
flagmax=max(flagmax,a[i]);//求最大值
}
vector<int> error;
for(int i=1;i<=4;i++)
{
if(flagmax-a[i]<=cha&&a[i]>=minn)
continue;
else//不满足条件则进入
error.push_back(i);
}
if(!error.size())//不满足条件的没有
cout<<"Normal"<<endl;
else if(error.size()==1)//不满足条件的只有一个
cout<<"Warning: please check #"<<error[0]<<"!"<<endl;
else
cout<<"Warning: please check all the tires!"<<endl;
}
题目三:
代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
getline(cin, s);
int cnt = 0;
int d = 0;
int k = 0;
while (s != ".")
{
k++;
int flag = s.find("chi1 huo3 guo1");//查找位置
if (flag != -1 && cnt == 0)//找到了,且是第一个
d = k;//存第一次出现
if (flag != -1)//每次找到都加一
cnt++;
getline(cin, s);
}
cout << k << endl;
if (cnt == 0)
cout << "-_-#" << endl;
else
cout << d << " " << cnt << endl;
}
题目四:
代码:
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
string str;
int n,m;
int main()
{
cin>>n>>m;
int k=pow(2,n);//最大编号+1
int pos,n;
while(m--)
{
pos=1,n=k;
cin>>str;
for(int j=0;j<str.length();j++)
{
if(str[j]=='n')//出现一次n,则加一半(默认为最右边的最后一个)
pos+=(n/2);
n/=2;//减半
}
cout<<pos<<endl;
}
}
题目五:
代码:
#include<iostream>
using namespace std;
int main()
{
int v[30]={0,0,0,0,0,0,10000,36,720,360,80,252,108,72,54,180,72,180,119,36,306,1080,144,1800,3600};
int a[10][10];
int b[15]={0};
int flagx,flagy;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
{
cin>>a[i][j];
if(a[i][j]==0)//记录0的位置
flagx=i,flagy=j;
b[a[i][j]]=1;//1~9出现标记为1
}
for(int i=1;i<=9;i++)
{
if(b[i]==0)//1~9没出现过的赋给一开始为0的位置
a[flagx][flagy]=i;
}
int n=3;
while(n--)
{
int x,y;
cin>>x>>y;
cout<<a[x][y]<<endl;
}
int sum=0;
cin>>n;
if(n<=3)//横
{
for(int i=1;i<=3;i++)
sum+=a[n][i];
}
else if(n>3&&n<=6)//竖
{
for(int i=1;i<=3;i++)
sum+=a[i][n-3];
}
else if(n==7)//主对角
{
sum=a[1][1]+a[2][2]+a[3][3];
}
else//副队角
sum=a[1][3]+a[2][2]+a[3][1];
//cout<<sum<<endl;
cout<<v[sum]<<endl;
}
题目六:
代码:
#include<iostream>
#include<stack>
using namespace std;
int summ(int f, int s, char y)/计算
{
int sum = 0;
if (y == '+')
sum = f + s;
else if (y == '-')
sum = f - s;
else if (y == '*')
sum = f * s;
else
{
if (s == 0)
return -1;
else
sum = f / s;
}
return sum;
}
int main()
{
stack<char>s2;
stack<int>s1;
int n;
int x1, x2;
char y;
cin >> n;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
s1.push(x);
}
for (int i = 1; i <= n - 1; i++)
{
char x;
cin >> x;
s2.push(x);
}
while (!s1.empty() && !s2.empty())
{
x1 = s1.top(), s1.pop();//取数
if (!s1.empty())
x2 = s1.top(), s1.pop();//取数
else
break;
y = s2.top(), s2.pop();//取运算符
if (summ(x2, x1, y) == -1)//不合法输出
{
cout << "ERROR: " << x2 << "/" << x1;
return 0;
}
x1 = summ(x2, x1, y);//下面的为第一个运算数,上面的为第二个运算数,计算结果重新入队
s1.push(x1);
}
cout << s1.top();
}
题目七:
代码:
#include<iostream>//有一个点没过
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct people
{
string name, id;//名字,身份证
int s;//身体状况
string time;
int order;//列表出现的顺序
};
int d, p;
map<string, int> day;
vector<people> v;
queue<people> v2;
bool cmp(people a, people b)//排序
{
if(a.time==b.time)//时间相等根据出现的顺序排
return a.order>b.order;
return a.time > b.time;
}
int judgeid(string s)//判断身份证是否合法
{
if(s.size()<18)
return 0;
for(int i=0;i<s.size();i++)
{
if(s[i]<'0'||s[i]>'9')
return 0;
}
return 1;
}
int main()
{
cin >> d >> p;
int dday = 0;//记录天数
while (d--)
{
dday++;
int t, ss;
cin >> t >> ss;
v.clear();
int cnt=0;
while (t--)
{
cnt++;//记录列表顺序
string name, time, id;
int s;
cin >> name >> id >> s >> time;
if (judgeid(id) && day.count(name) != 1)//之前这人没出现过且身份证合法
{
v.push_back({ name,id,s,time,cnt});
}
else if (judgeid(id) && day[name] + p + 1 <= dday)//之前出现过,天数合法,身份证合法
{
v.push_back({ name,id,s,time,cnt });
}
if (judgeid(id) && s == 1)//身份证合法,身体有情况
v2.push({ name,id,s,time,cnt });
}
sort(v.begin(), v.end(), cmp);//按时间顺序小到大排序
for (int i = 1; i <= ss; i++)//从后往前,按时间小的先出
{
if (!v.empty())//合法的人不为空
{
day[v.back().name] = dday;//更新该人的天数
cout << v.back().name << " " << v.back().id << endl;
}
else
break;
v.pop_back();
}
}
map<string, int> mp;//去重防止重复出现
while (!v2.empty())
{
if (mp.count(v2.front().name) < 1)
{
cout << v2.front().name << " " << v2.front().id << endl;
mp[v2.front().name]++;
}
v2.pop();
}
}
题目八:
代码:
#include<iostream>
using namespace std;
int tree[110],n;
void create(int k)//建第k编号的节点
{
if(k>n)
return;
create(2*k);//建左树
create(2*k+1);//建右树
cin>>tree[k];
}
int main()
{
cin>>n;
create(1);
for(int j=1;j<=n;j++)
{
cout<<tree[j];
if(j!=n)
cout<<" ";
}
}
题目九:
代码:
#include<iostream>
#include<cstring>
using namespace std;
int g[205][205];
int main()
{
int n, m;
cin >> n >> m;
memset(g, 0, sizeof(g));
while (m--)
{
int v1, v2, w;
cin >> v1 >> v2 >> w;
g[v1][v2] = g[v2][v1] = w;//邻接矩阵建边
}
long long k, fflag, ans = 0, maxx = 1e9;
cin >> k;
for (int i = 1; i <= k; i++)
{
long long x, sum = 0, flag = 1, tmp = 0;
int t;
int book[205];
memset(book, 0, sizeof(book));
cin >> t;
for (int j = 1; j <= t; j++)
{
cin >> x;
if (g[tmp][x] != 0 && !book[x])//能走,且没走过
{
book[x] = 1, sum += g[tmp][x];
tmp = x;
}
else
flag = 0;//标记为0,不符合攻略
}
if (t == n && flag && g[x][0])//能否回到家
{
ans++;
sum += g[x][0];
// cout << i << " " << sum << endl;
if (maxx > sum)//花费更少则更新
{
fflag = i;
maxx = sum;
}
}
}
cout << ans << endl;
cout << fflag << " " << maxx << endl;
}
题目十:
代码:
#include<iostream>
using namespace std;
int n, m, v1, v2;
int book[510] = { 0 }, g[510][510], path[510] = { 0 };
int dfs(int v)//深度搜索
{
book[v] = 1;
if (path[v])//是否已经有路径(走到终点或者走到已经走到过的点)
return path[v];
for (int i = 1; i <= n; i++)
if (g[v][i])//有路可走
path[v] += dfs(i);//加上i点往下走的path个数
return path[v];
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> v1 >> v2;
g[v1][v2] = 1;
}
cin >> v1 >> v2;
path[v2] = 1;//终点path标记为1
int cnt = dfs(v1);
int flag = 1;
for (int i = 1; i <= n; i++)
{
if (book[i] == 1 && !path[i])//访问过的但没有路径,则说明不是逻辑自洽
{
flag = 0;
break;
}
}
cout <<cnt<< " ";
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}