慧通编程第九关

慧通编程第九关,第1张

慧通编程第九关 991 有序插入
#include 
using namespace std;

const int N = 109;
int n, a[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	cin >> a[++n];
	
	int k = n;
	while (a[k] > a[k-1] && k >= 2) {
		swap(a[k], a[k-1]);
		k --;
	}
	
	for (int i = 1; i <= n; i ++) cout << a[i] << ' ';

	return 0;
}
992 插队
#include 
using namespace std;

const int N = 109;
int n, x, a[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
		
		if (a[i] + a[i-1] > 350) {
			cout << 50 << ' ';
		}
		cout << a[i] << ' ';
	}

	return 0;
}
993 删除数组中重复的数
#include 
using namespace std;

int n, x;
set  s;
set  :: iterator it;

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> x;
		s.insert(x);
	}
	
	for (it = s.begin(); it != s.end(); it ++) cout << *it << ' ';
	
	return 0;
}
994 删除倍数
#include 
using namespace std;

const int N = 109;
int n, x, a[N];

int main() {
	cin >> n >> x;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
		if (a[i] % x != 0) {
			cout << a[i] << ' ';
		}
	}
	
	return 0;
}
995 第K个点
#include 
using namespace std;

const int N = 1009;
int n, k, x;
set  s;
set  :: iterator it;

int main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i ++) {
		cin >> x;
		s.insert(x);
	}
	
	it = s.begin();
	for (int i = 1; i < k; i ++) it ++;
	cout << *it << endl;
	
	return 0;
}
996 前缀
#include 
using namespace std;

const int N = 109;
int n, a[N], b[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
		b[i] = b[i-1] + a[i];
		cout << b[i] << ' ';
	}
	
	return 0;
}
997 前缀和的逆
#include 
using namespace std;

const int N = 109;
int n, a[N], b[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> b[i];
		a[i] = b[i] - b[i-1];
		cout << a[i] << ' ';
	}
	
	return 0;
}
998 新数列
#include 
using namespace std;

const int N = 109;
int n, a[N], b[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	for (int i = 1; i <= n; i ++) {
		if (i == 1 || i == n) cout << a[i] << ' ';
		else cout << a[i-1] + a[i] + a[i+1] << ' ';
	}
		
	return 0;
}
999 多次求一段和
#include 
using namespace std;

const int N = 1e5 + 10;
int n, m, a[N], s[N];

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i ++) {
		scanf("%d", &a[i]);
		s[i] = s[i-1] + a[i];
	}
	
	while (m --) {
		int x, y;
		scanf("%d%d", &x, &y);
		if (x > y) swap(x, y);
		printf("%dn", s[y] - s[x - 1]);
	}
		
	return 0;
}
1000 11的倍数
#include 
using namespace std;

const int N = 1e5 + 10;
int n, m, a[N], s[N];

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i ++) {
		scanf("%d", &a[i]);
		s[i] = s[i-1] + a[i];
	}
	
	while (m --) {
		int x, y;
		scanf("%d%d", &x, &y);
		if (x > y) swap(x, y);
		
		int t = s[y] - s[x - 1];
		if (t % 11 == 0) puts("1");
		else puts("0");
	}
		
	return 0;
}
1001鸡蛋
#include 
using namespace std;

const int N = 1e5 + 10;
int n, a[N], s[N], h[7];

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) {
		scanf("%d", &a[i]);
		s[i] = s[i-1] + a[i];
		h[s[i] % 7] ++;
	}
	
	long long ans = h[0];
	for (int i = 0; i < 7; i ++) {
		ans += h[i] * (h[i] - 1) / 2;
	}
	cout << ans;
			
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4751926.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-08
下一篇 2022-11-08

发表评论

登录后才能评论

评论列表(0条)

保存