- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用插入排序对 7 个不同大小的数组进行排序。
在 main() 中,我知道重复调用同一个函数看起来很愚蠢,但我真的想不出一种方法来简化我的代码。
此外,我还利用 switch cases 生成了不同大小的数组。这是明智的做法吗?
希望有人能帮助我。
我在这里发布了我的代码。:
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
vector<int> n = { 100, 500, 1000, 2000, 5000, 8000, 10000 }; //Different input size
const int N = 20000; //random scale
int n0[100], n1[500], n2[1000], n3[2000], n4[5000], n5[8000], n6[10000]; //initialize input arrays
int *myArray[] = { n0, n1, n2 };
bool isAlreadyAdded(int value, int index, int *pointer)
{
for (int i = 0; i < index; i++)
{
if (*pointer == value)
return true;
pointer++;
}
return false;
}
void generator(int size){
int input_size = n[size];
int *p, *p2; //create a pointer point to the arrays which we want to manipulate.
switch (input_size)
{
case 100:
p = n0;
p2 = n0;
//cout << "p = n0 :" << *p << endl;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 500:
p = n1;
p2 = n1;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 1000:
p = n2;
p2 = n2;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 2000:
p = n3;
p2 = n3;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 5000:
p = n4;
p2 = n4;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 8000:
p = n5;
p2 = n5;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 10000:
p = n6;
p2 = n6;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
default:
cout << "Invalid input_size" << endl;
} //end swith cases.
}
void insertion_sort(int arr[], int length){
int j, temp;
for (int i = 0; i < length; i++){
j = i;
while (j > 0 && arr[j] < arr[j - 1]){
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
}
void writeToFile(int array[], string fileName, int length)
{
ofstream myfile;
myfile.open(fileName);
for (int i = 0; i < length; ++i)
{
myfile << array[i] << endl;
}
myfile.close();
}
int main() {
for (int i = 0; i < n.size(); ++i){
generator(i); // Parameter is the input size
}
insertion_sort(n0, sizeof(n0) / sizeof(n0[0]));
insertion_sort(n1, sizeof(n1) / sizeof(n1[0]));
insertion_sort(n2, sizeof(n2) / sizeof(n2[0]));
insertion_sort(n3, sizeof(n3) / sizeof(n3[0]));
insertion_sort(n4, sizeof(n4) / sizeof(n4[0]));
insertion_sort(n5, sizeof(n5) / sizeof(n5[0]));
insertion_sort(n6, sizeof(n6) / sizeof(n6[0]));
//writeToFile(n0, "n0", sizeof(n0) / sizeof(n0[0]));
cin.ignore();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
最佳答案
关于:
添加索引:
#include <map>
....
vector<int> n = { 100, 500, 1000, 2000, 5000, 8000, 10000 }; //Different input size
const int N = 20000; //random scale
int n0[100], n1[500], n2[1000], n3[2000], n4[5000], n5[8000], n6[10000]; //initialize input arrays
int *myArray[] = { n0, n1, n2 };
// index by size
map<int,int*> index= { {100,n0}, {500,n1}, {1000,n2}, {2000,n3}, {5000,n4}, {8000,n5}, {10000,n6} }; //Different input size
抽象代码:
void all_the_things_I_did_in_the_case(int *nx,int size) {
int input_size = n[size];
int *p, *p2; //create a pointer point to the arrays which we want to manipulate.
p = nx;
p2 = nx;
//cout << "p = n0 :" << *p << endl;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
}
并替换丑陋的生成器
void generator(int size){
map<int,int*>::iterator it=index.find(size);
if(it!=index.end()) {
all_the_things_I_did_in_the_case(it->second,size);
} else {
cout << "Invalid input_size" << endl;
}
}
关于使用不同的输入(数组)调用 C++ 相同的函数,如何缩小我的语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22952698/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我正在尝试创建一个可以像在 Excel 中一样放大和缩小的 QTableView。 此处提出了类似的问题:Zooming function on a QWidget 但是,我在 PyQt 而不是 C
如图所示。 我在 QScrollArea 中有 QWidget。QWidget 充当细胞图像和一些基于矢量的轮廓数据的渲染小部件。用户可以执行放大/缩小操作,简单地发生的是,它改变了 QPaint
双击 MKMapView 时:放大。 但是如何缩小呢? 最佳答案 总是使用两根手指来放大和缩小。在模拟器上,您需要按住选项键才能在模拟屏幕上显示“两根手指”(我认为这是 Alt 键,但我在 Mac 上
我有一些 javascript for {} 循环,我在整个项目中重复使用它们,它们都类似于: for (var i = 0; i < things.length; i++) { consol
我知道我可以使用C-x C- +进行放大/缩小,但这仅适用于当前文件。一旦我打开另一个文本,文本将恢复为默认值,一遍又一遍地做起来真的很累。如何保持当前emacs session 的全局缩放级别? 如
我对使用编译器工具自动化/简化 Angular 项目感兴趣,这可能适用于其他所有事情,但 Angular 注入(inject)和命名空间很尴尬,足以逃避编译器知识。执行此操作的最佳/专业方法是什么?
有没有办法在emacs上放大和缩小(动态改变字体大小,相当流畅)? 最佳答案 尝试 C-x C-+ 和 C-x C--;即 Control-x Control-减号/Control-再加上。 在一个组
我有一个Windows表单对象,其中包含3个对象,树 View ,richtextbox和tabcontrol。它们没有停靠在Windows窗体中,而是被 anchor 定(顶部+左侧)。 我已经编写
我想向 javascript-mode 添加功能,以便每当我在当前缓冲区上保存 Javascript 文件时,它都会在使用相对路径定义的目录中创建该文件的缩小文件,例如 ../foo 具有相同的文件名
这里有一些关于缩小.war文件的教程,甚至一些帖子。但是,最常见的技术(在Config.groovy中包含grails.war.resources = {})似乎对我不起作用。无论如何,grails会
如何使用 ScaleTransition缩小图像?我现在有这个,它只能放大。如果我误解了该方法,我不会,但我将其从 1 缩放到 0.8。由于某种原因,这种情况仍在扩大。 ScaleTransition
基本上,我想问ReplicaSets是否与CronJobs的suspend: "true"选项类似,但我愿意接受其他建议。 最佳答案 From the official Kubernetes doc
我想使用 boost::polygon 扩展/收缩带孔的多边形。所以澄清一点,我有一个单一的数据结构 boost::polygon::polygon_with_holes_data inPoly 其中
我有一个 map 列表: [%{~D[2019-02-11] => 7}, %{~D[2019-02-12] => 1}, %{~D[2019-02-15] => 1}] 我正在尝试将其变成一张大
我正在制作一个横幅,您可以使用jquery幻灯片功能缩放图像并且可以拖动图像。 除了一件事之外,它工作完美。当您使用图像下方的幻灯片放大图像时,效果非常好。您可以将图像拖动到您想要的位置。但当你想用幻
我们有一个 extjs 应用程序,其中我们布置的结构与 Sencha 推荐的结构不完全匹配。在我们的结构中,我们没有 app.js,但我们有一个 js,其中我们提到了自动加载和启动功能,示例如下以及文
我想在 Chrome/Firefox 中运行的应用程序是: 用 typescript 写 使用 React 使用 es 下一个功能(模块、导入等)编写 有一些导入是纯 js 文件 网页包 3 我可以在
我正在尝试像此处一样应用 Google map 的放大/缩小 - https://www.google.com/maps/@36.241201,-98.1261798,5.13z?hl=en我无法让它
我正在使用 Protractor ,需要缩小到 50%,我尝试了 StackOverflow 上发布的其他几个问题的解决方案,但没有任何效果。其中一些包括: browser.actions().key
我是一名优秀的程序员,十分优秀!