计算机
第一题:求导数
描述:求函数f(x) = ax^3 + bx^2 + c*x + d在x = x0处的一阶导数。
输入:a b c d x0。
输出:f’(x0)。
样例输入:
1 1 1 1 1
样例输出:
6
#include <bits/stdc++.h>
using namespace std;
int main(){
int t,n,m;
// for(scanf("%d",&t);t--;){
int a,b,c,d,x0;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&x0);
printf("%d\n",3*a*x0*x0+b*2*x0+c);
// }
return 0;
}
第二题:LIST
描述:在该LIST上实现3种操作
1、append x在该LIST末尾添加x,x是32位整数
2、pop删除该LIST末尾的数
3、find i寻找第i个数,若i为负数表示寻找倒数第i个数,例如i = -1表示寻找倒数第一个
输入:第一行输入一个m,表示有m条操作,接下来每行输入一条操作
输出:输出find i找到的数
样例输入
2
5
append 1
append 2
find 1
find -1
pop
6
append 1
append 2
append 3
append 4
find -2
find 2
样例输出
1
2
3
2
#include <bits/stdc++.h>
using namespace std;
int main(){
int t,n,x;
vector<int> v;
for(scanf("%d",&t);t--;){
v.clear();
for(scanf("%d",&n);n--;){
char op[7];
scanf("%s",op);
if(op[0]=='a'){
scanf("%d",&x);
v.push_back(x);
}
else if(op[0]=='f'){
scanf("%d",&x);
if(x<0) printf("%d\n",v[v.size()+x]);
else printf("%d\n",v[x-1]);
}else
v.pop_back();
}
}
return 0;
}
第三题:图像压缩存储
描述:以二维数组表示图像,其值只有0、1两种,寻找两幅图像中最大的相同部分
输入:第一行输入一个n,接下来的2n行输入两个n * n数组,寻找一个最大的m * m子区域,使得两个数组在该子区域完全相同
输出:输出上诉m
样例输入:
1
4
1 1 1 1
1 1 1 0
1 1 1 0
1 1 1 1
0 1 1 1
0 1 1 1
0 1 1 1
0 1 1 0
样例输出:
2
解释:上诉两个4阶数组中的一个2阶子区域(第1、2行,第2、3列完全相同)
#include <bits/stdc++.h>
using namespace std;
int main(){
int t,n,sub[100][100],dp[100][100];
for(scanf("%d",&t);t--;){
int res=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&sub[i][j]);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
int x;
scanf("%d",&x);
sub[i][j]=1-abs(x-sub[i][j]);
}
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++){
if(sub[i][0]==1){
dp[i][0]=1;
res=1;
}
if(sub[0][i]==1){
res=1;
dp[0][i]=1;
}
}
for(int i=1;i<n;i++){
for(int j=1;j<n;j++){
if(sub[i][j]==1)
dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1;
res=max(res,dp[i][j]);
}
}
printf("%d\n",res);
}
return 0;
}
第四题:解析表达式
描述:输入一个字符串形式的表达式,该表达式中包括整数,四则运算符(+、-、*、/),括号,三角函数(sin(x)、cos(x)、tan(x)),底数函数(lg(x)、ln(x)),计算该表达式的值
输入:输入一个字符串形式的表达式,保证中间及最终结果不超出double的范围
输出:表达式的值,保留6位小数
样例输入:
3
3+5
((2-1)*5-1)*6
1+cos(0)
sin(sin(1-1))
样例输出:
3.000000
8.000000
24.000000
2.000000
0.000000
草稿,没写完
#include <bits/stdc++.h>
using namespace std;
int pushprior(char c){
if(c=='#') return 0;
else if(c=='$') return 1;
else if(c=='(') return 2;
if(c=='-'||c=='+') return 3;
else if(c=='*'||c=='/') return 4;
}
double getnum(string str,int &index){
double res=0;
while(isdigit(str[index])){
res=res*10+str[index++]-'0';
}
return res;
}
double cal(double a,double b,char op){
if(op=='-') return a-b;
else if(op=='+') return a+b;
else if(op=='*') return a*b;
else return a/b;
}
int main(){
int t;
for(scanf("%d",&t);t--;){
stack<double> data;
stack<char> op;
op.push("#");
char str[100];
scanf("%s",str);
string s(str);
s+="$";
for(int i=0;i<s.size();i++){
if(isdigit(s[i]))
}
}
return 0;
}
网研
Problem A.质数统计
题目描述
给你一个数字n( n <=30),求比和小的质成的个数:
输入描述
第一行是数据组数T(T<=10)。
接下来T行每行有一个整数n。
输出描述
对于每组输入,输出一个数字表示比它小的质数的个数。
样例输入
3
3
5
8
样例输出
1
2
4
#include <bits/stdc++.h>
using namespace std;
int prime[30];
bool mark[30];
int primesize=0;
void getprime(){
fill(mark,mark+30,true);
mark[0]=mark[1]=false;
for(int i=0;i<30;i++){
if(!mark[i]) continue;
mark[i]=true;
prime[primesize++]=i;
for(int j=i*i;j<30;j+=i)
mark[j]=false;
}
}
int main(){
int t,n;
getprime();
for(scanf("%d",&t);t--;){
scanf("%d",&n);
int i=0;
for(;i<primesize&&prime[i]<n;i++);
printf("%d\n",i);
}
return 0;
}
Problem B.矩阵连乘
题目描述
给你3个5*5的矩阵A、B、C,让你求他们依次相乘的结果矩阵D,D=ABC,
输入描述
有多组数据,第一行是数据总数。
每组数据输入三个5*5的矩阵,
输出描述
输出一个矩阵D即答案
样例输入
1
1 2 3 4 5
6 5 8 9 7
6 5 7 8 4
5 6 3 2 1
2 3 1 4 5
1 2 3 4 5
3 2 1 4 5
6 5 4 1 2
6 2 1 4 5
3 2 1 3 4
7 8 9 6 3
1 2 4 5 6
3 2 1 4 5
3 2 1 8 6
3 0 0 1 0
样例输出
886 734 804 1112 832
2074 1700 1852 2634 2005
1791 1462 1589 2280 1740
942 722 770 1234 954
848 662 710 1072 788
传递二维数组参数时必须指定列(存储还是按一维存的),传的本身就是地址了,也即是引用型参数,不需要再做取地址操作了
#include <bits/stdc++.h>
using namespace std;
void mul(int x[5][5],int y[5][5],int z[5][5]){
for(int i=0;i<5;i++)
for(int j=0;j<5;j++){
z[i][j]=0;
for(int k=0;k<5;k++)
z[i][j]+=x[i][k]*y[k][j];
}
}
void input(int x[5][5]){
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
scanf("%d",&x[i][j]);
}
int main(){
int k,n,x[5][5],y[5][5],z[5][5],t[5][5];
for(scanf("%d",&k);k--;){
input(x);
input(y);
input(z);
mul(x,y,t);
mul(t,z,x);
for(int i=0;i<5;i++){
for(int j=0;j<4;j++)
printf("%d ",x[i][j]);
printf("%d\n",x[i][4]);
}
}
return 0;
}
}
Problem C.哈希映射
题目描述
哈希是计算机科学中一种常用的压维映射方式。对于一个只包含大写字母的字符串我们可以定义这样一种简单的哈希映射方法:
字母A-Z分别代表数字0-25, 整个字符串的哈希值即为空符串中所有字母的权重和。现给出N(N<=105)个长度不超过103仅包含大写字母的字符串
。然后给出所有哈希值为X的字符串,按输入顺序输出, 数据输入保证总长不超过4*107
输入描述
首先输入一个整数N(N<=105),代表字符串的个数。接下来的N行,每行包
含一个长度不超过1000的字符串。最后输入一个整数X。
输出描述
按输入顺序输出所有哈希值为X的字符串。
样例输入
5
AAAA
ABC
ZZ
A
AAA
0
样例输出
AAAA
A
AAA
用map输出时自动按字典顺序 所以选择用两个数组,用strlen得到字符数组的长度
#include <bits/stdc++.h>
using namespace std;
char hashs[110][1000];
int hash[110];
int main(){
int k,hashx;
scanf("%d",&k);
for(int i=0;i<k;i++){
scanf("%s",hashs[i]);
hash[i]=0;
for(int j=0;j<strlen(hashs[i]);j++)
hash[i]+=hashs[i][j]-'A';
}
scanf("%d",&hashx);
for(int i=0;i<k;i++){
if(hash[i]==hashx) printf("%s\n",hashs[i]);
}
return 0;
}
Problem D.HTML转换
题目描述
写HTML和CSS的代码是非常无聊的事情,有很多没必要的代码。例如,写一个HTML标签<div>
用id div1
和class col3
你必须这样写:
<div id="div1" class ="col3">
...
</div>
为了方便,一些网络程序猿开发了一个vim的插件-Emmet.利用这个工具,程序猿只需要写出div#divl.col3,Emmet 会将它转变成
。使用起来非常方便,现在你的任务是编写一个程序来完成这种任务如下:1.处理多重tag。div>p>span意味着有三个tag,tag
在tagdiv中,tag在tagp中。所以,正确的答案是
2.处理id和class。每个tag没有或至多一个id,任意数量的class,在“#”后的字符串(只包含数字和字母)是id的名字,在”.”后的字符串(只包含数字和字母)是class的名字,当tag同时有id和class的时候,要先输出id,当tag有多个class的时候,你要按照输入的顺序输出这些class。
例如div.aa#bb.cc.ee>p#g>span.d为
<div id="bb" class="aa cc ee">
<p id="g">
<span class="d"></span>
</p>
</div>
3.处理圆括号。使用圆括号来处理兄弟关系的tag。
<div id="bb" class="aa cc ee">
<p id="g1"><span class="d1"></span></p>
<p id="g2"><span class="d2"></span></p>
<p id="g3"><span class="d3"></span></p>
</div>
可以缩写成div.aa#bb.cc.ee>(p#g1>span.d1)(p#g2>span.d2)(p#g3>span.d3)
如果输入的字符串包含圆括号,保证最右端的)为字符串的最后一个字符。
4.处理符号””。在tag的尾部,你可能看到一个后缀”%d”.这说明这个tag要重复”%d”次。
例如:
ul#id1>li.classA3>p2为:
<ul id="id1">
<li class="classA">
<p></p>
<p></p>
</li>
<li class="classA">
<p></p>
<p></p>
</li>
<li class="classA">
<p></p>
<p></p>
</li>
</ul>
输入描述
第一行为一个数字N,为需要转换的字符串的个数。下面是N行,每行包含一个输入的字符串。每个字符串最多120 个字符,最终的结果至多1000个字符。tag, class, id 的字符串仅仅包含英文字符和数字。保证输入的字符串合法。
输出描述
输出N行,每一行为转换后的结果。从输出样例可以得到更多的细节。严格按照输出格式输出,不能出现额外的空格或换行。
样例输入
4
div>p>span
div.aa#bb.cc.ee>p#g>span.d
div.aa#bb.cc.ee>(p#g1>spah.d1)(p#g2>span.d2)(p#g3>span.d3)
ul#id1>li.classA*3>p*2
样例输出
<div><p><span></span></p></div>
<div id="bb" class="aa cc ee"><p id="g"> <span class="d"></span></p> </div><div id="bb" class= "aa cc ee"><p id="g1"><span class="d1"></span></p><p id="g2"><span class= "d2"></span></p><p id="g3"><span class="d3"></span></p></div>
<ul id="id1"><li class="classA"><p></p><p></p></li><li class="classA"><p></p><p></p></li><li class="classA"><p></p><p></p></li></ul>