数学 |.Problem A.质数统计
题目描述
给你一个数字n( n <=30),求比和小的质成的个数:
输入描述
第一行是数据组数T(T<=10)。
接下来T行每行有一个整数n。
输出描述
对于每组输入,输出一个数字表示比它小的质数的个数。
样例输入
3
3
5
8
样例输出
1
2
4
解析:
利用素数筛数法统计素数,然后进行比较
#include <iostream>
#include<bits/stdc++.h>
#define N 10000
using namespace std;
vector<int>prime;
bool num[N];
void initial(){
for(int i=0;i<N;i++){
num[i]=true;
}
num[0]=false;
num[1]=false;
for(int i=2;i<N;i++){
if(num[i]==false)continue;
prime.push_back(i);
for(int j=i*i;j<N;j+=i){
num[j]=false;
}
}
return;
}
int main()
{
int t;
cin>>t;
initial();
while(t--){
int n;
cin>>n;
int i=0;
while(prime[i]<n){
i++;
}
cout<<i<<endl;
}
return 0;
}
矩阵|Problem B.矩阵连乘
题目描述
给你3个55的矩阵A、B、C,让你求他们依次相乘的站果矩阵D,D=ABC,
输入描述
有多组数据,第一行是组数T。
每组数据输入三个55的矩阵,
输出描述
输出一个矩阵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 134 954
848 662 710 1072 788
解析
矩阵相乘的算法,三个for循环
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int a[5][5],b[5][5],c[5][5],d[5][5];
void cheng(){
int temp[5][5];
memset(temp,0,sizeof(temp));
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
for(int k=0;k<5;k++){
temp[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
for(int k=0;k<5;k++){
d[i][j]+=temp[i][k]*c[k][j];
}
}
}
}
int main()
{
int t;
cin>>t;
while(t--){
memset(d,0,sizeof(d));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cin>>a[i][j];
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cin>>b[i][j];
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cin>>c[i][j];
}
}
memset(d,0,sizeof(d));
cheng();
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout<<d[i][j]<<" ";
}
cout<<endl;
}
}
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
1234567
样例输出
AAAA
A
AAA
#include <iostream>
#include <string>
using namespace std;
const int maxn = 1000;
int HashTable[maxn] = {0};
string Hash[maxn];
int main()
{
int N;
cin >> N;
string str;
for( int i = 0; i < N; i++ )
{
cin >> str;
int len = str.size();
int sum = 0;
for( int k = 0; k < len; k++ )
sum += str[k] - 'A';
Hash[i] = str ;
HashTable[i] = sum;
}
int x;
cin >> x;
for( int i = 0; i < N; i++ )
{
if( HashTable[i] == x )
cout << Hash[i] << endl;
}
return 0;
}
模拟 |Problem D.HTML转换
题目描述
写HTML和CSS的代码是非常无聊的事情,有很多没必要的代码。例如,写一个HTML标签
为了方便,一些网络程序猿开发了一个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><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>
``