? 1. Recognition of digital patterns:
? The pattern of a number is the number of occurrences in a given number of numbers, such as 5,5,5,3,3,2,6,4, whose pattern is 5. Now your task is to find its pattern from the numbers.
|
#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; } |
The most essay idea of this problem is to turn the number into a subscript for the array, and the number's modulus becomes the value of the new array!
2. Inflection words
If the constituent letters of the two words are identical, but the letters are arranged in different order, they are the conjugation words, and the two words are also regarded as the conjugation words. such as tea and eat, Nic with Cin, DDC with DCD, ABC with ABC, etc. Your task is to determine whether they are conjugation words.
|
#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; } } } } |
The problem is to sort the strings, and then the comparison is OK, of course, the use of fast or merge sorting can greatly improve the efficiency of the program!
3. The most favorite food for pancake mcdull is pancakes, which will stay there for a few minutes every time you see a pancake stall in the street. The most attractive to mcdull or pancake master that hand skilled pancake technology, a pile of pancakes in there, the master only need to use a shovel to turn a few, let pancake neatly stacked together. On this day, in order to celebrate McDull was a walk to graduate school, he bought some pancakes from the baker to treat. But McDull bought the pancake size is different, mcdull too want to eat pancakes, he wants to eat the biggest of these pancakes. McDull also know that classmates also like pancakes, in order to show his sincerity, he wants to let students eat first, mcdull last eat, therefore, McDull want to put pancakes in order from small to large stacked together, big in the bottom. So McDull can get the biggest pancake at the end. Now please help mcdull with the pancake Chef to turn the pancake to the pancake from small to large stacked together. The pancake Master's method is to use a shovel to insert between two pancakes, then turn the pancake on the shovel, so that the first pancake on the shovel was turned to the top, and the original top of the pancake was turned to the place where the shovel was just inserted. McDull hopes to have the least number of pancakes.
|
#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; } |
Solution: Try to swap the maximum value to the first position of the array, and then swap the last position of the array again! Number of arrays minus one!
4. The realization of the Joseph question
n individuals in a circle, each person labeled as 1, 2 、...、 N, the request from 1th number from 1, the person to check k out of the circle, and then the next person from 1 began to count, so cycle, until only the last person, the man is the winner. For example, when n=10,k=4, the number of people in the order of 4, 8, 2, 7, 3, 10,9, 1, 6, 5, the position of 5th is the winner. For a given n person, you are programmed to calculate the last winner number.
The first type:
|
#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; } |
Pass the mathematical formula!
The second type:
|
#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; } |
Through a single cycle linked list to achieve!
"There are 6 algorithm questions not done, come on!" 】
Implementation of a simple algorithm