Algorithm Design and Analysis Experiment 1

Generate nonvoid proper subsets

描述

Given a set, generate all of its nonvoid proper subsets in ascending order.

输入

Each test case starts with 2 <= N <= 9, which is the number of elements in a set. The next line will have N integers 1 <= ai <= 9, representing the value of each element in the set in ascending order. The input is terminated at the end of the file (EOF).

输出

For each test case, you must print all the nonvoid proper subsets of the input set in ascending order. Afer each test case you must print a blank line.

输入样例 1

2
1 2
4
1 2 3 4

输出样例 1

1
2

1
2
3
4
12
13
14
23
24
34
123
124
134
234


#include <stdio.h>
#include <string.h>
#include<iostream>
using namespace std;

int num = 0;            //存储集合中的元素个数
int *arr2 = NULL;   //存储元素排列的数组
int arr1[100];

void opt(int length)    //输出指定长度的元素 
{
    for(int i = 0;i < length; i++){
    	
        cout<<arr1[arr2[i]];
    }
    cout<<endl;
}
//递归组合集合中指定长度的真子集 
void travel(int len, int currLen, int position)
{
    if(currLen == len){
        opt(len);                     
        return;                                
    }
    while(position != num){
        arr2[currLen] = position;               //记录当前位置
        travel(len, currLen + 1, ++position);   //继续递归产生下一个位置
    }
}
int main(){
	int N,i;
	while(cin>>N){
	  for(i=0;i<N;i++)
	  	cin>>arr1[i];
	num = i;   
	arr2 = (int *)malloc(sizeof(int) * num);          
    int j;                                      
    for(j = 1;j < num; j++)
        travel(j, 0, 0);      
		cout<<endl;                             //递归遍历子集 
    } 
    return 0;
}

Simple Count

描述

Count how many numbers do not contain 4 or 7 in the N numbers from 1 to N.

输入

Each test case starts with 1 <= N <= 1,000,000. The input is terminated by the end of file (EOF).

输出

For each test case, print how many numbers from 1 to N do not contain 4 or 7.

输入样例 1 

5
10
20

输出样例 1

4
8
16

#include<iostream> 
using namespace std;

bool check(int x){
	while(x){
		if(x%10 == 4|| x%10==7){
		   return true;	  
	    }
	    x = x/10;
	}
	return false;
}


int countNum(int m){
	int result = 0;
	for(int i =1;i<=m;i++){
		result += check(i)?1:0;
	}
    int total = m-result;
	cout<<total<<endl;
}

int main(){
	int n;
	while(scanf("%d",&n)!=EOF){ 
	    if((1<=n)&&(n<=1000000))
	   	   countNum(n);	   
	}
	return 0;
}

Line up in the canteen

描述

One day, there is a kind of new delicious food from one of the windows in the canteen. All students want to have a taste. Therefore, they all go to this window to line up and form a sequence. We need to simplify this sequence. For example, if a sequence is 12221133345678899, we can simplify it as 1213456789.

输入

Each test case contains a sequence. There are only numbers and letters in the sequence. The max length of the sequence is 100. The input is terminated by the end of file (EOF).

输出

For each test case, you must print the simplyfied sequence in one line.

输入样例 1 

12221133345678899
aabbccddeeffgg

输出样例 1

1213456789
abcdefg

#include<iostream> 
#include<bits/stdc++.h>

using namespace std;

int main(){
	string str1,str2;
	while(cin>>str1){
		int len1 = str1.length();
		int i=j=0;	
		for( i =0 ; i < len1; i ++){
			if(str1[i+1] != str1[i]) {
				str2[j] = str1[i];
				j++;}
		}
		
		for(int m= 0; m< j ; m++)
			cout<<str2[m];
		cout<<endl;
	}
	return 0;
}

Removing the Wall

描述

There is a wall in Tom's garden, which is builded by N different bricks. Bricks may have different height, but their width are the same. For example, when N is 7, and H=[1,2,6,1,1,7,1], this wall will be look like:

One day, Tom bought a new car and he wants to park it in his garden. So he needs to remove some parts of the wall. In order to reduce the cost, the sum of height of the removing part should be as low as possible.

输入

Each test case starts with two integers N, K (1<=N<=1*10^3,1<=K<=N). n means this wall is builded by N different bricks, and K means the width of the car. The next line will be n integers H1,H2,..,Hn(1<=Hi<=100), representing the height of the N bricks. The input will be terminated at the end of file (EOF).

输出

For each test case, you must print an integer M which means removing from the Mth brick. If there is more than one position, print the most left one.

输入样例 1 

7 3
1 2 6 1 1 7 1

输出样例 1

3

提示

In the sample, the sum of the numbers of the 3rd, 4th, 5th bricks is 8, which is the lowest, so the wall should be removed from the 3rd position.

#include<bits/stdc++.h>
#include<iostream>
using namespace std;

int main(){
	int len,num,count;
	while(cin>>num>>len){
		vector<int> a(num);
		for(int i =0;i<num;i++)
			cin>>a[i];
	count = num-len+1;
	long sum;
	int loc;
    long MIN = 50000;
	for(int i =0;i<count;i++){
		   sum = 0;
		for(int j = i;j<i+len;j++)
		  sum +=a[j];
		if(sum <MIN){
		  MIN = sum;
		  loc = i+1;}
      }
	cout<<loc<<endl;
    }
	return 0;
}

A dice game

描述

One day, boy A and girl B are playing a dice game. First, they write a number between 1~6 in a piece of paper, and then roll the dice. The one whose number is closer to the number of the dice will win this game. For example, A wins if |a-x|<|b-x|. And |a-x|=|b-x| means a draw.

输入

Each test case cantains two integers a and b, representing the numbers guessed by boy A and girl B respectively. The input is terminated in the end of file (EOF).

输出

For each test case, you must print the numbers of the cases of "A wins", "draw" and "B wins".

输入样例 1 

2 5

输出样例 1

3 0 3

输入样例 2 

2 4

输出样例 2

2 1 3
#include<iostream>
using namespace std;

int main(){
	int boy,girl;
	int Bscore,Gscore,Equal;
	while(cin>>boy>>girl){
	Bscore = 0,Gscore = 0,Equal = 0;
		for(int i =1;i <=6;i++){
			if(abs(boy-i) == abs(girl-i)) Equal++;
			else if(abs(boy-i) < abs(girl-i)) Bscore++;
			else Gscore++;
			//cout<<i<<" "<<Bscore<<" "<<Equal<<" "<<Gscore<<endl;
		}
		cout<<Bscore<<" "<<Equal<<" "<<Gscore<<endl;
	}
}

Don't touch my cake!

描述

A boy bought N different cakes someday. They were arranged randomly in a line given by permutation P=[p1, p1, ..., pn], where pi denotes the unique label of a cake. To play a game, his mother sorted some subsegment of permutation P from position L to position R, that is, [L, R].

After every such sorting, the boy would be wondering if the X-th cake is still in its original position. In other words, was px changed?

After every such sorting, cakes were returned to their initial positions, so you can assume that all the sortings are independent of each other.

输入

The first line contains two space-separated integers N, M (1 ≤ N, M ≤ 1000) — the length of permutation and the number of sortings.

The second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that all elements in the permutation are different.

Each of the next M lines contains three space-separated integers Li, Ri, Xi (1 ≤ Li ≤ Xi ≤ Ri ≤ n) — the left and right borders of the subsegment and the position which is interesting to the boy in the i-th sorting.

The input is terminated at the end of the file (EOF).

输出

For each sorting, print "Yes" if the cake at position X was not changed, or "No" otherwise.

输入样例 1 

5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3

输出样例 1

Yes
No
Yes
Yes
No

输入样例 2 

6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3

输出样例 2

Yes
No
Yes
No
Yes

提示

Explanation of first test case:

  1. [1, 2, 3, 4, 5] — permutation after sorting, the 3-rd element is not changed, so answer is "Yes".
  2. [3, 4, 5, 2, 1] — permutation after sorting, the 1-st element is changed, so answer is "No".
  3. [5, 2, 3, 4, 1] — permutation after sorting, the 3-rd element is not changed, so answer is "Yes".
  4. [5, 4, 3, 2, 1] — permutation after sorting, the 4-th element is not changed, so answer is "Yes".
  5. [5, 1, 2, 3, 4] — permutation after sorting, the 3-rd element is changed, so answer is "No".
    	int right = r-1;
    	for(i=left;i<right;i++){
    		int k = i;
    		for(int j =i+1; j<=right;j++){
    			if(a[j]<a[k]) k = j;
    		}
    		if(k!=i){
    			int temp = a[i];
    			a[i]  = a[k];
    			a[k] = temp;
    		}
    	}	
    }
    
    bool check(int a[],int l, int r, int x){
    	    for(int i = 0;i<N;i++)
    	       arr2[i] = a[i];
            sort(a,l,r);
            int loc = x-1;
            if(arr2[loc] == a[loc]){
               	for(int i = 0;i<N;i++)
    	         arr1[i] = arr2[i];
    	      return true;
            	
    	    }
    	    else {
    	    	for(int i = 0;i<N;i++)
    	         arr1[i] = arr2[i];
    	         return false;	    	
    		}     
    }
    
    
    int main(){
    	int m;
    	while(cin>>N>>m){
    		for(int i=0;i<N;i++)
    			cin>>arr1[i];
    	int l,r,x;
        for(int i =0; i<m ;i++){
        	cin>>l>>r>>x;
    		bool result = check(arr1,l,r,x);
    		if(result)
    		  cout<<"Yes"<<endl;
    		else
    		  cout<<"No"<<endl;
    	}
      }
        return 0;
    }

    Tom palindrome number

    描述

    Tom is studing math these days. If there is a number X, whose binary form and decimal form are all palindrome numbers, Tom names it "Tom palindrome number". And Tom wants to know how many "Tom palindrome number" there are between N and M ? (1001,66,64446 are all palindrome number)

    输入

    Each test case starts with two integer N and M (1<=N<=M<=1000000).

    输出

    For each test case, you must print all Tom palindrome numbers between N and M, one line for each. After each test case you must print a blank line. And print nothing if there isn't any "Tom palindrome number" between N and M.

    输入样例 1 

    1 5
    2 2
    5 10
    10 40
    

    输出样例 1

    1
    3
    5
    
    5
    7
    9
    
    33
    #include<iostream>
    using namespace std;
    
    int judge(int i, int x) {
        int res = i, t = 0;
        while (i) {
            t = t * x + i % x;
            i /= x;
        }
        return t == res;
    }
    
    int main() {
        int m,n;
        while(cin>>m>>n){
        for (int i=m; i <=n; i++) {
            if (judge(i,10) && judge(i,2)) {
                cout << i <<endl;
            }
       }
       cout<<endl;
        }
        return 0;
    }
    
    

    Unova

    描述

    In mythology, Unova was created by uniting the warring peoples of the land leaded by twin heroes. They used a single dragon more than 2500 years ago. The brothers started to argue over their beliefs; the elder brother sought truth and the younger brother sought ideals. Different people have different ideas on many things. If there are N people and N different views on one thing. How many situations could be possible if everyone holds a different view?

    输入

    Each test case contains an integer N (N < 10), which is the number of people and views. Inputs will be terminated at the end of file (EOF).

    输出

    For each test case, you must print all the situations in lexicographical order. There should be a blank line after each answer.

    输入样例 1 

    1
    2
    3
    

    输出样例 1

    1
    
    12
    21
    
    123
    132
    213
    231
    312
    321
    
    
    #include<cstdio>
    #include<iostream>
    using namespace std;
    
    int A[11];  // n<10
    
    void print_permutation(int n, int* A, int cur) {
      if(cur == n) { // terminate recursion
        for(int i = 0; i < n; i++) printf("%d", A[i]);
        printf("\n");
      } else for(int i = 1; i <= n; i++) { // try to assign A[cur] with i
        int ok = 1;
        for(int j = 0; j < cur; j++)
          if(A[j] == i) ok = 0; // i is already used in A[0]~A[cur-1]
        if(ok) {
          A[cur] = i;
          print_permutation(n, A, cur+1); // recursion
        }
      }
    }
    
    int main() {
      int n;
      while(cin>>n){
      print_permutation(n, A, 0); 
       printf("\n");
       }
      return 0;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: data structure and algorithm analysis in c是一本C语言数据结构和算法分析的经典教材,由Mark Allen Weiss编写。该书的主要特点是对数据结构和算法的讲解非常详细且透彻,采用了很多实例进行讲解,使得读者可以很快地掌握这些内容。此外,该书还特别强调了算法分析和设计的重要性,帮助读者理解复杂算法的实现方式,提高算法的优化能力。 而data structure and algorithm analysis in c的下载方式也非常简单,可以在网上找到相关的资源下载,并且也可以通过购买实体书的方式获得。对于想要深入学习C语言数据结构和算法分析的人来说,这本书是非常值得推荐的一本入门教材。 ### 回答2: data structure and algorithm analysis in c是一本关于C语言数据结构和算法分析的经典教科书。该书涵盖了广泛的数据结构和算法,包括数组、链表、树、图、排序、查找等。本书不仅涵盖了基本概念和技术,而且提供了深入的分析和高级应用。书中有丰富的例子和习题,方便读者深入理解和应用。该书是学习数据结构和算法的好材料,对于提高程序员的编程能力和解决问题的能力有很大的帮助。 ### 回答3: Data Structure and Algorithm Analysis in C是一本面向C++程序设计开发人员的算法和数据结构分析书籍。它涵盖了许多重要的算法和数据结构,如排序和搜索算法,二叉树,平衡树和图论等方面。本书旨在帮助读者深入了解算法和数据结构的基本知识,并提供了许多实用的示例和演练题来帮助读者巩固自己的知识。 本书包含了许多实际的示例和演练题,这些题目涵盖了从简单到复杂的各种情况,有助于读者更好地理解和应用所学知识。与此同时,这本书也提供了大量的编码和调试技巧,帮助读者编写出高效和可维护的代码。 总之,Data Structure and Algorithm Analysis in C是一本优秀的参考书,无论你是刚接触算法和数据结构或是已经具备一定的基础,它都可以提供丰富的知识和实践经验。如果你正在寻找一本深入了解算法和数据结构的书籍,那么它一定是首选之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yvonnae

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值