2016.计算机院.Problem A.三元组
问题描述
给你一个长度为m的数组(数组元素从0到m-1),如果数组里有a[i]+a[j]==a[k](i,j,k大于等于0并且小于m),便称之为三元组。
现在给你一个数组,让你求三元组的个数。
例如m为2,里面的元素为(0,0)
那么三元组为
(a[0],a[0],a[0])
(a[0],a[0],a[1])
(a[0],a[1],a[0])
(a[0],a[1],a[1])
(a[1],a[0],a[0])
(a[1],a[0],a[1])
(a[1],a[1],a[0])
(a[1],a[1],a[1])
输出答案为8.
Input
输入正整数N,表示N例测试。接着输入N组数据,每组输入m(1<=m<=50),表示数组长度,然后输入这个数组。
Output
对每组输入数据,输出三元组的个数。
Sample Input
2
2
0 0
5
1 1 1 2 1
Sample Output
8
16
代码
#include <iostream>
using namespace std;
const int maxn = 50;
int A[maxn];
int main()
{
int T, N;
cin >> T;
int i, j, k, triple = 0;
while( T-- )
{
cin >> N;
triple = 0;
for( i = 0; i < N; i++ )
cin >> A[i];
for( i = 0; i < N; i++ )
for( j = 0; j < N; j++ )
for( k = 0; k < N; k++ )
if( A[i] + A[j] == A[k] )
triple++;
cout << triple << endl;
}
return 0;
}
2016.计算机院.Problem B.寻找变化前01序列
问题描述
给你一个01序列,HDLC协议处理的话,如果出现连续的5个1会补1个0。例如1111110,会变成11111010。
现在给你一个经过HDLC处理后的01序列,你需要找到HDLC处理之前的01序列。
例如给你11111010
你需要输出1111110
Input
输入正整数N,表示N例测试。接着输入N组数据,每组输入经过HDLC处理过的01序列(长度小于100)。
Output
对每组输入数据,输出HDLC处理前的01序列。
Sample Input
2
11111010
1111100
Sample Output
1111110
111110
解题思路:
直接遍历一次字符串数组,对1出现次数计数,如果连续出现5次,那么下一个数不输出。
代码
#include <iostream>
#include <string>
using namespace std;
const int maxn = 101;
int dp[maxn];
int main()
{
int T;
cin >> T;
string str;
while( T-- )
{
cin >> str;
int i = 1;
int cnt = 0;
for( int i = 0; i < str.size(); i++ )
{
if( str[i] == '1')
cnt++;
else
cnt = 0;
cout << str[i];
if( cnt == 5 )
{
i++;
cnt = 0;
}
}
cout << endl;
}
return 0;
}
2016.计算机院.Problem C.寻找i*j=m的个数
问题描述
3*3的矩阵内容。
1 2 3
2 4 6
3 6 9
即a[i][j](1<=i<=n,1<=j<=n)=i*j。
问一个这样n*n的矩阵里面,里面m出现的次数。
例如n为3,m为6.
那么出现的次数就是2
Input
输入正整数N,表示N例测试(N<=20)。接着输入n(n<=105),(m<=109)。
Output
对每组输入数据,输出m出现的次数。
Sample Input
2
3 6
3 3
Sample Output
2
2
代码
#include <iostream>
using namespace std;
int main()
{
int T;
int n, m;
cin >> T;
while( T-- )
{
cin >> n >> m;
int cnt = 0;
for( int i = 1; i <= n; i++ )
if( m%i == 0 && m/i <= n )
cnt++;
cout << cnt << endl;
}
return 0;
}
模拟 | 2016.计算机院.Problem D.字符串处理
问题描述
有以下三种操作。
(1) COPY l r
(0<=l<=r<n),n代表s串的长度。这个表示将s串从l到r的序列复制到剪贴板t里面,覆盖t字符串。
例如s为abcde
t为pqr
执行COPY 1 2变为
s为abcde
t为bc
(2) CUT l r
(0<=l<=r<n),n代表s串的长度。这个表示将s串从l到r的序列剪切到剪贴板t里面(删除s串中的l到r的序列),覆盖t字符串。
例如s为abcde
t为pqr
执行CUT 1 2变为
s为ade
t为bc
(3) PASTE p
(0<=p<n),n代表s串的长度。这个表示将t串插入到s串p位置的后面。t保持不变。
例如s为abcde
t为pqr
执行PASTE 1 变为
s为abpqrcde
t为pqr
Input
输入正整数N,表示N例测试。首先给你s串,再给你一个m,然后给你m个操作。
Output
对每个操作,输出操作后的s串。
Sample Input
abcde
5
CUT 1 2
COPY 0 1
PASTE 1
PASTE 1
CUT 1 3
Sample Output
ade
ade
adade
adadade
aade
代码
#include <iostream>
using namespace std;
int main()
{
int T, N;
cin >> T;
string s, t;
string cmd;
int l, r, p;
while( T-- )
{
cin >> s;
t.clear();
cin >> N;
while( N-- )
{
cin >> cmd;
if( cmd == "COPY")
{
cin >> l >> r;
t = s.substr(l, r-l+1);
cout << s << endl;
}
else if( cmd == "CUT")
{
cin >> l >> r;
t = s.substr( l, r-l+1 );
s.erase( l, r-l+1 );
cout << s << endl;
}
else
{
cin >> p;
s.insert( p+1, t );
cout << s << endl;
}
}
}
return 0;
}