第28次CCF计算机软件能力认证(测试)

测试300分要是考试的时候也能这么发挥就好

第一题:现值计算

解题思路:直接模拟
n , m = input().split()
n = int(n);m = float(m)
l = list(map(int , input().split()))
res = 0
for i in range(0 , n + 1):
    res += pow(1 + m , -i) * l[i]
print(res)

第二题:训练计划

解题思路:读题+模拟
#include
#include

using namespace std;

const int N = 400;
int p[N] , t[N];
int n , m;

int main()
{
	cin >> n >> m;
	for(int i = 1;i <= m;i ++) cin >> p[i];
	for(int i = 1;i <= m;i ++) cin >> t[i];
	
	int first_time[N];
	bool f = true;
	for(int i = 1;i <= m;i ++)
	{
		if(!p[i]) first_time[i] = 1;
		else first_time[i] = t[p[i]] + first_time[p[i]];
		int check = first_time[i] + t[i] - 1;
		if(check > n) f = false;
	}
	
	for(int i = 1;i <= m;i ++)
		cout << first_time[i] << " ";
	
	if(f)
	{
	    cout << endl;
	    memset(first_time , 0x3f , sizeof first_time);
		for(int i = m;i >= 1;i --)
		{
		    if(first_time[i] > n) 
		        first_time[i] = n + 1 - t[i];
		    if(p[i]) 
		        first_time[p[i]] = min(first_time[p[i]] , first_time[i] - t[p[i]]);
		}
		
		for(int i = 1;i <= m;i ++)
		    cout << first_time[i] << " ";
	    cout << endl;
	}
	return 0;
}

第三题:JPEG编码

 解题思路:依旧是模拟,但是更大
# z型遍历
import math
def cal(i , j , m):
    res = 0
    for u in range(8):
        for v in range(8):
            alphau = 1 if u else (0.5) ** 0.5
            alphav = 1 if v else (0.5) ** 0.5
            ucos = math.cos(math.pi * (1 / 8) * (i + (1 / 2)) * u)
            vcos = math.cos(math.pi * (1 / 8) * (j + (1 / 2)) * v)
            res += 0.25 * (alphau * alphav * m[u][v] * ucos * vcos)
    return res

def input_m(data):
    M = [[0 for i in range(8)] for j in range(8)]
    x = y = 0
    f = True
    while x != 8 or y != 8:
        if x < 8 and y < 8:
            if not len(data):
                break
            M[x][y] = data[0]
            data.pop(0)
        if f: x -= 1;y += 1
        else: x += 1;y -= 1

        if x < 0: x = 0;f = not f
        if y < 0: y = 0;f = not f
    return M

def cal_muti(Q , M):
    res = [[0 for i in range(8)] for j in range(8)]
    for i in range(8):
        for j in range(8):
            res[i][j] = Q[i][j] * M[i][j]

    return res

Q = [] # 量化矩阵
for i in range(8):
    l = list(map(int , input().split()))
    Q.append(l)
n = int(input())
t = int(input()) # t = 0 输出填充矩阵
data = list(map(int , input().split()))
input_M = input_m(data)
liang_M = cal_muti(Q , input_M)
if t == 0 or t == 1:
    for i in range(8):
        for j in range(8):
            print(input_M[i][j] if t == 0 else liang_M[i][j] , end = ' ')
        print()
else:
    for i in range(8):
        for j in range(8):
            res = round(cal(i , j , liang_M) + 128)
            if res < 0: res = 0
            if res > 255: res = 255
            print(res, end = ' ')
        print()

你可能感兴趣的:(ccf,csp,算法,c++,开发语言)