附近的最小值
Description
fly学姐有一个序列a[1],a[2],...,a[n]。
给定一个正整数k,请问对于每一个1到n之间的序号i,a[i−k],a[i−k+1],...,a[i+k]这2k+1个数中的最小值是多少?
当某个下标超过1到n的范围时,数不存在,求最小值时只取存在的那些值。
Input
输入的第一行包含一整数n。
第二行包含n个整数,分别表示a[1],a[2],...,a[n]。
第三行包含一个整数k。
Output
输出一行,包含n个整数,分别表示对于每个序号求得的最小值
Sample Input 1
5 5 2 7 4 3 1
Sample Output 1
2 2 2 3 3
Hint
对于 30% 的评测用例,1<=n<=1000,1<=a[i]<=1000。
对于 50% 的评测用例,1<=n<=10000,1<=a[i]<=10000。
对于所有评测用例,1<=n<=1000000,1<=a[i]<=100000
在纸上用笔写写十分简单,没什么难的
#include <stdio.h>
int main()
{
int n,k;
int i,j;
int a[1000001];
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&k);
for(j=1;j<=n;j++)
{
int min=9999999;
//printf("66666");
for(i=j-k;i<=j+k;i++)
{
if(i>=1&&i<=n)
{
if(min>a[i])
{
min=a[i];
}
}
}
printf("%d ",min);
}
}
环形杀人事件
Description
一天,杀人狂魔李华突然想要杀人,就把他抓来的N个英雄依次分配一个编号,第一个人的编号为1号,第二个人的编号为2号,第N个人的编号就为N号,他们按顺序围成一个环形,现在给出一个数字M,第一个人开始从1报数,第二个人报的数就是2,依次类推,报到M这个数字的人被杀了(1a秒了),紧接着从被杀了的这个人的下一个人重新开始从1报数,和上面过程类似,报到M的人被杀,直到N个人全部被杀掉,请问,这个被杀的顺序是什么?
Input
给出两个正整数N、M
Output
输出被杀的顺序
Sample Input 1
10 3
Sample Output 1
3 6 9 2 7 1 8 5 10 4
Hint
1<n<=10000
1<m<=100
早知道就直接递归了,还想着学了链表就用用,结果1个小时没想出来wok
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* headnode()//创建头节点
{
struct Node* headnode = (struct Node*)malloc(sizeof(struct Node));
headnode->next = NULL;
return headnode;
}
struct Node* chuang(int x)//创建新节点
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void weicha(struct Node* headnode, int data)//尾插
{
struct Node* newnode = chuang(data);
struct Node* teap = headnode;
//printf("*%d*", teap->data);
while (teap->next!=NULL)
{
teap = teap->next;
}
teap->next = newnode;
//printf("&%d& ", teap->data);
}
void shan(struct Node* headnode, int M)//删除某一个元素
{
struct Node* teap = headnode;
struct Node* teap1 = teap->next;
int sum=0;
while (teap->next != NULL)
{
teap = teap->next;
teap1 = teap1->next;
sum++;
if (sum==M-1)
{
printf("%d ",teap1->data);
teap->next = teap1->next;
teap1 = teap1->next;
sum=0;
}
if(teap->next==NULL)
{
teap=headnode;
teap1=teap->next;
//printf("头节点");
}
if(teap->next==NULL)
{
break;
}
}
}
void shuchu(struct Node* headnode)//输出
{
struct Node* teap = headnode->next;
while (teap!=NULL)
{
printf("%d ", teap->data);
teap = teap->next;
}
}
int main()
{
int N,M;
struct Node* list = headnode();
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
{
weicha(list,i);
}
shuchu(list);
shan(list,M);
shuchu(list);
}
太拉了(;´༎ຶД༎ຶ`) !!!