折点计数
#include <stdio.h>
//折点计数
int main() {
int n, count = 0;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int j = 0; j < n - 2; j++) {
if (a[j] - a[j + 1] > 0 && a[j + 1] - a[j + 2] < 0) count++;
if (a[j] - a[j + 1] < 0 && a[j + 1] - a[j + 2] > 0) count++;
}
printf("%d\n", count);
return 0;
}
众数问题
#include <stdio.h>
int main() {
int n;
//输入组数
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int m;
//输入数组的个数
scanf("%d", &m);
int s[100] = { 0 };
int max = 0;
int node = 0;
for (int j = 0; j < m; j++) {
int num;
//输入m个有序的值
scanf("%d", &num);
//在对应的数组下加1
s[num]++;
//通过比较数组的值(该下标num出现的次数),找出最大的数值,再将num赋给node
if (s[num] > max) {
max = s[num];
node = num;
}
}
printf("%d %d\n", node, max);
}
}
将给定的整数去掉任意个数字后重新
#include <bits/stdc++.h>
using namespace std;
int main() {
/********* Begin ********/
int k;
string s;
cin >> s >> k;
if (k > s.size()) {
cout << "Invalid Input.";
}
while (k) {
int i;
for (i = 0; i < s.size() - 1 && s[i] <= s[i + 1]; i++);
s.erase(i, 1);
k--;
}
if (s.empty()) {
cout << 0 << endl;
}
int i = 0;
for (i = 0; i < s.size()-1;) {
if (s[i] == '0') i++; else break;
}
cout << s.substr(i);
return 0;
/********* End ********/
}
素数圈
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 100
int n,x[N];
bool vis[N],flag=false;
bool checkPrime(int t)//约束条件 相邻的和为素数
{
for(int j=2;j<=sqrt(t);j++)
{
if(t%j==0)
return false;
}
return true;
}
void dfs(int i)
{
if(i>n)
{
if(flag){
printf("围成的圈是:");
for(int j=1;j<i;j++)
printf("%d ",x[j]);
exit(0);
}
flag=true;
}
else
{
for(int j=1;j<=n;j++)
{
if(!vis[j]&&checkPrime(x[i-1]+j))
{
vis[j]=true;
x[i]=j;
dfs(i+1);
vis[j]=false;
}
}
}
}
int main()
{
scanf("%d",&n);
dfs(1);
return 0;
}
排列的逆序数
#include<cstdio>
#include <iostream>
using namespace std;
long sum = 0;//逆序数的个数
void output(int *a,int n) ;
void swap(int &a, int &b) ;
void Merge(int a[],int s,int m, int e) ;
void MergeSort(int a[],int s,int e) ; //归并排序
void MergeAndCountNum(int * arr,int s,int mid,int e); //归并有序序列并计算逆序数的个数
int main()
{
int i,n,x;
int *a=0,*b=0;
scanf("%d",&n);
a=new int[n];
b=new int[n];
for(i=0;i<n;i++)
{
scanf("%d",&x);
a[i]=b[i]=x;
}
output(a,n);
MergeSort(a,0,n-1);
printf("逆序数的个数为:");
cout << sum ;
delete []a;
delete []b;
system("pause");
return 0;
}
void output(int *a,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d,",a[i]);
}
printf("\n");
}
void swap(int &a, int &b)
{
int t;
t=a;a=b;b=t;
}
void Merge(int a[],int s,int m, int e)
{ //将数组a的局部a[s,m]和a[m+1,e]合并到tmp,并保证tmp有序,然后再拷贝回a[s,e] //归并操作时间复杂度:O(e-m+1),即O(n)
int pb = 0;
int p1 = s,p2 = m+1;
int *tmp = new int[e-s+1];
/********** Begin **********/
while( p1 <= m && p2 <= e)
{ if( a[p1] < a[p2])
tmp[pb++] = a[p1++];
else
tmp[pb++] = a[p2++];
}
while( p1 <= m)
tmp[pb++] = a[p1++];
while( p2 <= e)
tmp[pb++] = a[p2++];
for(int i = 0;i < e-s+1; ++i)
a[s+i] = tmp[i];
/********** End **********/
delete []tmp;
}
void MergeSort(int a[],int s,int e)
{
int *tmp = new int[e-s+1];
if( s < e)
{ int m = s + (e-s)/2;
/********** Begin **********/
MergeSort(a,s,m);
MergeSort(a,m+1,e);
MergeAndCountNum(a,s,m,e);
/********** End **********/
}
delete []tmp;
}
void MergeAndCountNum(int * arr,int s,int mid,int e)
{
/********** Begin **********/
int *tmp = new int[e-s+1];
int pb = 0;
int p1 = s, p2 = mid + 1;
while (p1 <= mid && p2 <= e) {
if (arr[p1] > arr[p2]) {
tmp[pb++] = arr[p2++];
sum += mid - p1 + 1; // 计算逆序数的个数
} else {
tmp[pb++] = arr[p1++];
}
}
while (p1 <= mid) {
tmp[pb++] = arr[p1++];
}
while (p2 <= e) {
tmp[pb++] = arr[p2++];
}
for (int i = 0; i < e - s + 1; ++i) {
arr[s + i] = tmp[i];
}
delete[] tmp;
/********** End **********/
}