计算列表中数值翻倍的元素个数

需积分: 9 2 下载量 168 浏览量 更新于2024-11-05 收藏 577B TXT 举报
"该编程问题要求创建一个程序,用于检查给定的整数列表中,有多少元素是其他元素的两倍。例如,给定列表1 4 3 2 9 7 18 22,程序应返回3,因为2是1的两倍,4是2的两倍,18是9的两倍。提供的代码示例用C++编写,采用嵌套循环结构来遍历并比较列表中的元素。" 在这个问题中,关键知识点包括: 1. **列表处理**:程序需要处理随机生成的包含2到15个唯一正整数的列表。这涉及到数组或列表数据结构的使用,以及对这些数据结构的遍历。 2. **条件判断**:程序首先通过两个嵌套do-while循环读取输入列表,其中`_if_1`和`_if_2`是控制循环的标志。当遇到0时,`_if_2`变为0,结束内部循环;当遇到-1时,`_if_1`变为0,结束外部循环。这种设计确保了正确读取用户输入的列表。 3. **遍历与比较**:为了找出每个列表中元素的两倍项,程序使用了三层嵌套循环。外层循环遍历列表,中间层循环遍历列表中的每个元素,内层循环则将当前元素与列表中所有其他元素进行比较,检查是否存在两倍关系。 4. **计数与输出**:每找到一个两倍关系,计数器`s`就增加1。在遍历完整个列表后,`s`的值就是满足条件的元素数量,然后将其输出。 5. **效率优化**:虽然这个解决方案能解决问题,但效率不高。因为对于每个元素,它都会与其他所有元素进行比较,导致时间复杂度为O(n^2)。更优的算法可以先排序列表,然后只需比较相邻的元素,从而降低时间复杂度到O(n log n)。 6. **C++语法**:提供的代码展示了C++的基本语法,包括变量声明、输入输出操作(cin和cout)、数组操作、循环和条件语句。`a[i][j]`表示二维数组的访问,`a[m][n]!=NULL`用于检查元素是否已读取。 7. **ACM编程**:标签“acm”可能指的是ACM(Association for Computing Machinery)的编程竞赛,这类问题通常要求高效且简洁的解决方案,因此在实际编程比赛中,优化代码以提高效率是非常重要的。
2013-05-30 上传
Practice 1 Date: Monday, March 18th, 2013 We highly encourage being environment friendly and trying all problems on your own. Implement exercise 2.3-7. Implement priority queue. Implement Quicksort and answer the following questions. (1) How many comparisons will Quicksort do on a list of n elements that all have the same value? (2) What are the maximum and minimum number of comparisons will Quicksort do on a list of n elements, give an instance for maximum and minimum case respectively. Give a divide and conquer algorithm for the following problem: you are given two sorted lists of size m and n, and are allowed unit time access to the ith element of each list. Give an O(lg m + lgn) time algorithm for computing the kth largest element in the union of the two lists. (For simplicity, you can assume that the elements of the two lists are distinct). Practice 2 Date: Monday, April 1st, 2013 We highly encourage being environment friendly and trying all problems on your own. Matrix-chain product. The following are some instances. Longest Common Subsequence (LCS). The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Longest Common Substring. The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Max Sum. The following is an instance. (-2,11,-4,13,-5,-2) Shortest path in multistage graphs. Find the shortest path from 0 to 15 for the following graph.   A multistage graph is a graph (1) G=(V,E) with V partitioned into K >= 2 disjoint subsets such that if (a,b) is in E, then a is in Vi , and b is in Vi+1 for some subsets in the partition; and (2) | V1 | = | VK | = 1.     Practice 3 Date: Monday, April 15th, 2013 We highly encourage being environment friendly and trying all problems on your own. Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem both as fractional knapsack and 0/1 knapsack. A simple scheduling problem. We are given jobs j1, j2… jn, all with known running times t1, t2… tn, respectively. We have a single processor. What is the best way to schedule these jobs in order to minimize the average completion time. Assume that it is a nonpreemptive scheduling: once a job is started, it must run to completion. The following is an instance. (j1, j2, j3, j4) : (15,8,3,10) Single-source shortest paths. The following is the adjacency matrix, vertex A is the source.  A B C D E A -1 3 B 3 2 2 C D 1 5 E -3 All-pairs shortest paths. The adjacency matrix is as same as that of problem 3.(Use Floyd or Johnson’s algorithm)     Practice 4 Date: Monday, May 8th, 2013 We highly encourage being environment friendly and trying all problems on your own. 0/1 Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem using back-tracking algorithm and try to draw the tree generated. Solve the 8-Queen problem using back-tracking algorithm.    
2023-05-26 上传