洛谷题单——数组篇

目           录

P1428 小鱼比可爱

P1427 小鱼的数字游戏

P5727 【深基5.例3】冰雹猜想

P1047 [NOIP2005 普及组] 校门外的树

P5728 【深基5.例5】旗鼓相当的对手

P5729 【深基5.例7】工艺品制作

P2550 [AHOI2001] 彩票摇奖

P2615 [NOIP2015 提高组] 神奇的幻方

P5730 【深基5.例10】显示屏

P1554 梦中的统计

P2141 [NOIP2014 普及组] 珠心算测验

P1614 爱与愁的心痛

P2911 [USACO08OCT] Bovine Bones G

P1161 开灯

P5731 【深基5.习6】蛇形方阵

P5732 【深基5.习7】杨辉三角

P1789 【Mc生存】插火把

P1319 压缩技术

P1320 压缩技术(续集版)

P1205 [USACO1.2] 方块转换 Transformations



P1428 小鱼比可爱

#include<stdio.h>
int main() {
	int N, nums[10000], result[10000] = {0};
	scanf("%d", &N);
	for (int i = 0; i < N; i++)
		scanf("%d", &nums[i]);

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < i; j++)
			if (nums[j] < nums[i])
				result[i]++;
	}

	for (int i = 0; i < N; i++)
		printf("%d ", result[i]);
}

P1427 小鱼的数字游戏

#include<stdio.h>
int main() {
	int nums[101] = { 1 }, i = 0;
	for (i = 0; ; i++) {
		scanf("%d", &nums[i]);
		if (nums[i] == 0)
			break;
	}

	for (int j = i - 1; j >= 0; j--) {
		printf("%d ", nums[j]);
	}
}

P5727 【深基5.例3】冰雹猜想

#include<iostream>
#include<stdlib.h>

using namespace std;
void printresult(int number) {
	if (number == 1) {
		cout << number << " ";
		return;
	}

	if (number % 2 != 0)
		printresult(number * 3 + 1);
	else
		printresult(number / 2);
	cout << number << " ";
}
int main() {
	int number;
	cin >> number;
	printresult(number);
	return 0;
}

P1047 [NOIP2005 普及组] 校门外的树

#include<iostream>
#include<stdlib.h>
#include<cstdio>
#include<cstring>
using namespace std;

int L, M;//马路长度L和M组数据
int rest_tree = 0;
int space[100000];
int main()
{
	memset(space, 0, sizeof(space));//初始化该数组
	cin >> L >> M;

	for (int i = 0; i <= L; i++)
		space[i] = 0;//从0这个树开始,将这条路上所有的树标记为未访问过
	for (int i = 1; i <= M; i++)
	{
		int head, tail;//定义区间的头和尾
		cin >> head >> tail;
		for (int j = head; j <= tail; j++)
			if (space[j] == 0)
				space[j] = 1;//从这个区间的头和尾开始,将所有区间内的树标记为访问过
	}
	for (int i = 0; i <= L; i++)
	{
		if (space[i] == 0)
			rest_tree++;//计算未被访问过的树的数目
	}
	cout << rest_tree << endl;
	return 0;
}

P5728 【深基5.例5】旗鼓相当的对手

#include<stdlib.h>
#include<iostream>
using namespace std;

int main() {
	int people_number=0,cnt=0;
	cin >> people_number;
	int score[1001][4];
	for (int i = 0; i < people_number; i++)
	{
		cin >> score[i][0] >> score[i][1] >> score[i][2];
		score[i][3] = score[i][0] + score[i][1] + score[i][2];
	}
	for (int i = 0; i < people_number; i++) {
		for (int j = i + 1; j < people_number; j++) {
			if (abs(score[i][0] - score[j][0]) <= 5 
				&& abs(score[i][1] - score[j][1]) <= 5 
				&& abs(score[i][2] - score[j][2]) <= 5 
				&& abs(score[i][3] - score[j][3]) <= 10)
				cnt++;
		}
	}

	cout << cnt;
	return 0;
}

P5729 【深基5.例7】工艺品制作

#include <iostream>
#include <stdlib.h>
using namespace std;

bool judge[21][21][21] = { 0 };//定义数组判断,初始赋值为0,代表未被切除。
int w, x, h, q, x1, y1, z1, x2, y2, z2, cnt = 0;
int main()
{
    cin >> w >> x >> h >> q;
    for (int i = 1; i <= q; ++i)
    {
        cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
        for (int j = x1; j <= x2; ++j)
            for (int k = y1; k <= y2; ++k)
                for (int l = z1; l <= z2; ++l)//三重循环枚举每一个小方块。
                    judge[j][k][l] = 1;//记录被切除。
    }
    for (int i = 1; i <= w; ++i)
        for (int j = 1; j <= x; ++j)
            for (int k = 1; k <= h; ++k)//再是三重循环
                if (judge[i][j][k] == 0)//统计未被切除的。
                    cnt++;
    cout << cnt << endl;//输出
    return 0;//结束
}

P2550 [AHOI2001] 彩票摇奖

#include <iostream>
#include <stdlib.h>
using namespace std;

int number,m,f[34],p[10];
int main() {
	cin >> number;

    for (int i = 0; i < 7; i++)
    {
        cin >> m;
        f[m] = 1;
    }//输入中奖号码,将中奖号码所在数组赋值为1

    for (int i = 1; i <= number; i++)
    {
        int cnt = 0;//中奖号码数量
        for (int j = 1; j <= 7; j++)
        {
            cin >> m;
            if (f[m] == 1)
                cnt++;
        }
        p[cnt]++;
    }
    for (int i = 7; i >= 1; i--)
        cout << p[i] << ' ';
    return 0;

}

P2615 [NOIP2015 提高组] 神奇的幻方

#include <iostream> 
#include <stdlib.h>

using namespace std;
int a[40][40] = { 0 };

int main() {
	int number;
	cin >> number;
	int step = 1;     // 代表填到第几个数字了
	int last_posx, last_posy;   // 上一个点坐标


	while (step <= number * number) {
		if (step == 1) {
			a[1][number / 2 + 1] = step++;
			last_posx = 1;
			last_posy = number / 2 + 1;
		}
			
		else if (last_posx == 1 && last_posy != number) {
			a[number][last_posy + 1] = step++;
			last_posx = number;
			last_posy = last_posy + 1;
		}
		
		else if (last_posx != 1 && last_posy == number) {
			a[last_posx - 1][1] = step++;
			last_posx = last_posx - 1;
			last_posy = 1;
		}
			
		else if (last_posx == 1 && last_posy == number) {
			a[last_posx+1][last_posy] = step++;
			last_posx = last_posx + 1;
		}

		else if (last_posx != 1 && last_posy != number) {
			if (a[last_posx - 1][last_posy + 1] == 0) {
				a[last_posx - 1][last_posy + 1] = step++;
				last_posx = last_posx - 1;
				last_posy = last_posy + 1;
			}
			else {
				a[last_posx + 1][last_posy] = step++;
				last_posx = last_posx +1;
			}

		}
			
	}
	for (int i = 1; i <= number; i++) {
		for (int j = 1; j <= number; j++)
			cout << a[i][j] << " ";
		cout << std::endl;
	}
}

P5730 【深基5.例10】显示屏

#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;

int number;
string str, ans[5];
int main() {
	cin >> number;
	cin >> str;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == '0')
		{
			ans[0] += "XXX"; ans[4] += "XXX";
			ans[1] += "X.X"; ans[2] += "X.X"; ans[3] += "X.X";
		}
		if (str[i] == '1')
		{
			ans[0] += "..X"; ans[4] += "..X";
			ans[1] += "..X"; ans[2] += "..X"; ans[3] += "..X";
		}
		if (str[i] == '2')
		{
			ans[0] += "XXX"; ans[4] += "XXX";
			ans[1] += "..X"; ans[2] += "XXX"; ans[3] += "X..";
		}
		if (str[i] == '3')
		{
			ans[0] += "XXX"; ans[4] += "XXX";
			ans[1] += "..X"; ans[2] += "XXX"; ans[3] += "..X";
		}
		if (str[i] == '4')
		{
			ans[0] += "X.X"; ans[4] += "..X";
			ans[1] += "X.X"; ans[2] += "XXX"; ans[3] += "..X";
		}
		if (str[i] == '5')
		{
			ans[0] += "XXX"; ans[4] += "XXX";
			ans[1] += "X.."; ans[2] += "XXX"; ans[3] += "..X";
		}
		if (str[i] == '6')
		{
			ans[0] += "XXX"; ans[4] += "XXX";
			ans[1] += "X.."; ans[2] += "XXX"; ans[3] += "X.X";
		}
		if (str[i] == '7')
		{
			ans[0] += "XXX"; ans[4] += "..X";
			ans[1] += "..X"; ans[2] += "..X"; ans[3] += "..X";
		}
		if (str[i] == '8')
		{
			ans[0] += "XXX"; ans[4] += "XXX";
			ans[1] += "X.X"; ans[2] += "XXX"; ans[3] += "X.X";
		}
		if (str[i] == '9')
		{
			ans[0] += "XXX"; ans[4] += "XXX";
			ans[1] += "X.X"; ans[2] += "XXX"; ans[3] += "..X";
		}
		if (i != str.size() - 1) 
		{
			ans[0] += "."; ans[4] += ".";
			ans[1] += "."; ans[2] += "."; ans[3] += ".";
		}
		
	}
	for (int i = 0; i < 5; i++) 
		cout << ans[i] << endl;
	
	return 0;


}

P1554 梦中的统计

#include<stdlib.h>
#include<iostream>

using namespace std;
int M, N, ans[10] = { 0 };

int main() {
	cin >> M >> N;
	for (int i = M; i <= N; i++) {
		for (int j = i; j > 0;) {
			ans[j % 10] ++;
			if (j <= 9)
				break;
			j = j / 10;
		}
	}
	for (int i = 0; i <= 9; i++)
		cout << ans[i] << " ";

	return 0;
}

P2141 [NOIP2014 普及组] 珠心算测验

#include<stdlib.h>
#include<iostream>
using namespace std;

int n,nums[101]={0}, ans = 0;
bool judge[101] = { false };

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) 
		cin >> nums[i];

	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			for(int k=0;k<n;k++)
			if (nums[i] + nums[j] == nums[k]&& judge[k] != true)
			{
				ans++;
				judge[k] = true;
			}
		}
	}

	cout << ans;
    return 0;
}

P1614 爱与愁的心痛

#include<stdlib.h>
#include<iostream>
using namespace std;

int n, m, minsum = 0, sum = 0;
int nums[3001];

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> nums[i];
	for (int i = 0; i < n; i++) {
		if (i + m > n)
			break;
		for (int j = i; j < i + m && i + m <= n; j++) {
			sum = sum + nums[j];
		}
		if (i == 0)
			minsum = sum;

		if (sum < minsum)
			minsum = sum;
		sum = 0;
		
	}
	cout << minsum << endl;

}

P2911 [USACO08OCT] Bovine Bones G

#include<stdlib.h>
#include<iostream>
using namespace std;

int s1, s2, s3, max_Probability_value = 0;
int ans[81];

int main() {
	cin >> s1 >> s2 >> s3;
	for (int i = 1; i <= s1; i++) {
		for (int j = 1; j <= s2; j++) {
			for (int k = 1; k <= s3; k++) {
				ans[i + j + k]++;
			}
		}
	}
	for (int i = 0; i <= 81; i++) {
		if (ans[i + 1] > ans[i]) {
			max_Probability_value = i + 1;
		}
	}
	cout << max_Probability_value;
}

P1161 开灯

#include<stdlib.h>
#include<iostream>
using namespace std;

int n, t;
bool ans[2000001] = { false };
double a;

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a >> t;
		for (int j = int(a * t); t >= 1; t--) {
			if (ans[int(a * t)] == false)
				ans[int(a * t)] = true;
			else
				ans[int(a * t)] = false;
		}
	}

	for (int i = 1; i <= 2000001; i++) {
		if(ans[i]==true)
		{
			cout << i;
			break;
		}
	}
	return 0;
}

P5731 【深基5.习6】蛇形方阵

#include <iostream>
#include<stdlib.h>
using namespace std;

int num[10][10] = { 0 }, n;

int main() {
    cin >> n;

    int total = 1;
    int i = 1, j = 1;
    int direction = 0; // 0: right, 1: down, 2: left, 3: up

    while (total <= n * n) {
        num[i][j] = total++;
        if (direction == 0) {
            if (j + 1 > n || num[i][j + 1] != 0) {
                direction = 1; // change direction to down
                i++;
            }
            else {
                j++;
            }
        }
        else if (direction == 1) {
            if (i + 1 > n || num[i + 1][j] != 0) {
                direction = 2; // change direction to left
                j--;
            }
            else {
                i++;
            }
        }
        else if (direction == 2) {
            if (j - 1 < 1 || num[i][j - 1] != 0) {
                direction = 3; // change direction to up
                i--;
            }
            else {
                j--;
            }
        }
        else if (direction == 3) {
            if (i - 1 < 1 || num[i - 1][j] != 0) {
                direction = 0; // change direction to right
                j++;
            }
            else {
                i--;
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            printf("%3d", num[i][j]);
        }
        cout << endl;
    }

    return 0;
}

P5732 【深基5.习7】杨辉三角

#include <iostream>
using namespace std;

int num[21][21] = { 0 };

int main() {
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        num[i][1] = 1;
        num[i][i] = 1;
        for (int j = 2; j <= n - 1; j++) {
            num[i][j] = num[i - 1][j] + num[i - 1][j - 1];
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            cout << num[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

P1789 【Mc生存】插火把

#include<stdlib.h>
#include<iostream>
using namespace std;

int n, m, k, x, y, o, p, cnt = 0;
int num[105][105] = { 0 };

int main() {
	cin >> n >> m >> k;
	for (int q = 1; q <= m; q++)
	{
		cin >> o >> p;
		x = o + 2;
		y = p + 2;
		for (int i = x - 1; i <= x + 1; i++)
			for (int j = y - 1; j <= y + 1; j++)
				num[i][j] = 1;
		num[x - 2][y] = 1, num[x + 2][y] = 1, num[x][y - 2] = 1, num[x][y + 2] = 1;
	}

	for (int q = 1; q <= k; q++) {
		cin >> o >> p;
		x = o + 2;
		y = p + 2;
		for (int i = x - 2; i <= x + 2; i++) {
			for (int j = y - 2; j <= y + 2; j++) {
				num[i][j] = 1;
			}
		}
	}

	for (int i = 3; i <= n + 2; i++) {
		for (int j = 3; j <= n + 2; j++) {
			if (num[i][j] == 0)
				cnt++;
		}
	}
	cout << cnt;

}

P1319 压缩技术

#include<stdlib.h>
#include<iostream>
using namespace std;

int judge = -1;//judge为1时输出1,为-1时输出0
int N, num, cnt = 0, list = 0;

int main() {
	cin >> N;
	while (cnt < N * N) {
		cin >> num;//循环输入a
		for (int i = 1; i <= num; i++) {
			if (list == N) 
			{
				cout << endl;
				list = 0;//判断是否需要回车
			}
			if (judge == -1)
				cout << 0;
			else
				cout << 1;
			list++;
			cnt++;//计数器+1
		}
		judge = -1 * judge;
	}
	cout << endl;
	return 0;
}

P1320 压缩技术(续集版)

#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
int lenth = 0;
char final_line[40401], add_line[201];

int main()
{
	cin >> final_line;
	lenth = strlen(final_line);
	for (int i = 2; i <= lenth; i++)
	{
		cin >> add_line;
		strcat(final_line, add_line);//输入并连接
	}
	cout << lenth << " ";


	for (int i = 0, sum = 0, num = '0'; i <= strlen(final_line); i++)
	    if (num == final_line[i])
	        sum++;//如果相等,sum加一
	    else
	    {
	        num = final_line[i];
	        printf("%d ", sum);
	        sum = 1;
	    }
	return 0;
}

P1205 [USACO1.2] 方块转换 Transformations

#include<stdlib.h>
#include<iostream>
using namespace std;

int n;
char start[10][10], final[10][10], temp[10][10];


bool work1()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			temp[j][n - 1 - i] = start[i][j];
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (temp[i][j] != final[i][j])
				return 0;
	return 1;
}
bool work2()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			temp[n - i - 1][n - j - 1] = start[i][j];
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (temp[i][j] != final[i][j])
				return 0;
	return 1;
}
bool work3()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			temp[n - j - 1][i] = start[i][j];
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (temp[i][j] != final[i][j])
				return 0;
	return 1;
}
bool work4()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			temp[i][n - j - 1] = start[i][j];
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (temp[i][j] != final[i][j])
				return 0;
	return 1;
}
bool work5()
{
	work4();
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			start[i][j] = temp[i][j];
	if (work1())
		return 1;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			start[i][j] = temp[i][j];
	if (work2())
		return 1;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			start[i][j] = temp[i][j];
	if (work3())
		return 1;
	return 0;
}
bool work6()
{
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (temp[i][j] != final[i][j])
				return 0;
	return 1;
}
void detect() {
	if (work1())
	{
		cout << 1;
		return;
	}
	if (work2())
	{
		cout << 2;
		return;
	}
	if (work3())
	{
		cout << 3;
		return;
	}
	if (work4())
	{
		cout << 4;
		return;
	}
	if (work5())
	{
		cout << 5;
		return;
	}
	if (work6())
	{
		cout << 6;
		return;
	}
	cout << 7;
}


int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> start[i][j];
			temp[i][j] = start[i][j];
		}
		getchar();//消除回车对字符数组的影响
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> final[i][j];
		}
		getchar();//消除回车对字符数组的影响
	}

	detect();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值