1.数字模式的识别:
数字的模式是指在一堆给定数字中出现次数最多的数值,如5,5,5,3,3,2,6,4,它的模式就是5。现在你的任务,就是从数字中找到它的模式.
#include <stdio.h>
#define MAX 4000001
int
arr[MAX];
int
main()
{
arr[MAX]=0;
int
n;
int
max;
int
a;
int
index;
scanf
(
"%d"
,&n);
for
(
int
i=1;i<=n;i++)
{
scanf
(
"%d"
,&a);
arr[2000000+a]++;
}
max=0;
for
(
int
j=2000000;j<MAX;j++)
{
if
(arr[j]>max)
{
max = arr[j];
index = j;
}
}
printf
(
"%d\n"
,index-2000000);
return
0;
}
|
这道题目最大的破题思路就是把数字变成写数组的下标,数字的模变成新数组的值!
2.变位词
如果两个单词的组成字母完全相同,只是字母的排列顺序不一样,则它们就是变位词,两个单词相同也被认为是变位词。如tea 与eat , nic 与cin, ddc与dcd, abc与abc 等。你的任务就是判断它们是否是变位词。
|
#include "stdio.h"
#include <string.h>
int
main()
{
void
BubbleSort(
char
arr[],
int
n);
int
n;
char
str1[100];
char
str2[100];
scanf
(
"%d"
,&n);
for
(
int
i=1;i<=n;i++)
{
scanf
(
"%s"
,&str1);
scanf
(
"%s"
,&str2);
BubbleSort(str1,
strlen
(str1));
BubbleSort(str2,
strlen
(str2));
if
(
strcmp
(str1,str2)==0)
{
printf
(
"Yes\n"
);
}
else
{
printf
(
"No\n"
);
}
}
return
0;
}
void
BubbleSort(
char
arr[],
int
n)
{
int
i,j;
char
temp;
for
(i=0;i<n-1;i++)
{
for
(j=0;j<n-1-i;j++)
{
if
(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
|
该题就是把字符串排序,然后对比就OK了,当然使用快排或者归并排序可以大大提高程序效率!
3.翻煎饼
麦兜最喜欢的食物是煎饼,每次在街上看到煎饼摊的时候都会在那里停留几分钟。最吸引麦兜还是煎饼师傅那一手熟练的翻煎饼的技术,一堆煎饼在那里,师傅只需要用铲子翻几下,就让煎饼整齐的叠在了一起。 这天,为了庆祝麦兜被保送上研究生,他从煎饼师傅那里买回来一些煎饼请客。但是麦兜买回的煎饼大小不一,麦兜太想吃煎饼了,他想吃这些煎饼中最大的那个。麦兜还知道同学们也很喜欢煎饼,为了表示他的诚意,他想让同学们先吃,麦兜最后吃,因此,麦兜想把煎饼按照从小到大的顺序叠放在一起,大的在最下面。这样麦兜就可以在最后拿到最大的那一块煎饼了。 现在请你帮助麦兜用煎饼师傅翻煎饼的方法把麦兜买的煎饼从小到大的叠在一起。煎饼师傅的方法是用铲子插入两块煎饼之间,然后将铲子上的煎饼翻一转,这样铲子上第一个煎饼就被翻到了顶上,而原来顶上的煎饼则被翻到了刚才插入铲子的地方。麦兜希望这样翻煎饼的次数最少。
|
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
int
arr[1000];
using
namespace
std;
void
SwapIndex(
int
n)
{
for
(
int
i=0;i<n/2;i++)
{
int
t=arr[i];
arr[i]=arr[n-1-i];
arr[n-1-i]=t;
}
}
int
main()
{
int
n,i,j;
int
count=0;
//scanf("%d",&n);
cin>>n;
for
(i=0;i<n;i++)
{
//scanf("%d",&arr[i]);
cin>>arr[i];
}
while
(n>1)
{
//每次得到最大值,然后想办法把最大值放到数组首位,然后通过交换把最大值放到最下面!
int
max=0,index=0;
for
(j=0;j<n;j++)
{
if
(arr[j]>max)
{
max=arr[j];
index=j;
}
}
if
(index==0)
{
count++;
SwapIndex(n);
}
else
if
(index<n-1)
{
count+=2;
SwapIndex(index+1);
SwapIndex(n);
}
n--;
}
printf
(
"%d\n"
,count);
return
0;
}
|
解题思路:想办法把最大值交换到数组的第一位置,然后再次交换数组最后位置!数组数减一!
4.约瑟夫问题的实现
n个人围成一个圈,每个人分别标注为1、2、...、n,要求从1号从1开始报数,报到k的人出圈,接着下一个人又从1开始报数,如此循环,直到只剩最后一个人时,该人即为胜利者。例如当n=10,k=4时,依次出列的人分别为4、8、2、7、3、10,9、1、6、5,则5号位置的人为胜利者。给定n个人,请你编程计算出最后胜利者标号数。
第一种:
|
#include<iostream>
using
namespace
std;
int
main()
{
int
n,k;
cin>>n>>k;
int
i,s=0;
for
(i=2;i<=n;i++)
{
s=(s+k)%i;
}
cout<<s+1;
return
0;
}
|
通过数学公式!
第二种:
|
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
using
namespace
std;
typedef
struct
Link
{
int
data;
struct
Link * next;
}link;
/*采用尾插法建立链表*/
void
createLink(link *&l,
int
a[],
int
n)
{
link *r,*s;
int
i;
l = (link*)
malloc
(
sizeof
(link));
r=l;
//r始终代表链表的首部
for
(i=0;i<n;i++)
{
s = (link * )
malloc
(
sizeof
(link));
s->data=a[i];
r->next=s;
r=s;
}
r->next=l->next;
//循环单链表
}
void
getWin(link *&l,
int
n,
int
k)
{
link *p, *cur;
p=l->next;
//第一条数据
cur = p;
//当前指针
int
count=0,j=0;
while
(1)
{
j++;
if
(j==k)
{
if
(count==n-1)
{
printf
(
"%d\n"
,cur->data);
break
;
}
j=0;
p->next=cur->next;
//链表的删除
free
(cur);
cur=p->next;
count++;
}
else
{
p=cur;
cur=cur->next;
}
}
}
int
main()
{
link *head;
int
a[100],n,i,k;
scanf
(
"%d %d"
,&n,&k);
for
(i=0;i<n;i++)
{
a[i]=i+1;
}
createLink(head,a,n);
getWin(head,n,k);
/*link *p = head->next;
while(p->next!=NULL)
{
printf("%d",p->data);
p = p->next;
}
printf("%d",p->data);*/
return
0;
}
|
通过单循环链表来实现!
【还有6道算法题目没做呢,加油!】