我正在编写一些代码,需要为我的Code :: Blocks 12.11提供C++ 11支持.我使用默认的GNU GCC编译器和MingW一起使用.有什么方法可以做到这一点吗?
我试图在循环中创建一个空向量,并希望每次将某个元素读入该循环时向该向量添加一个元素.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::vector<float> myVector();
float x;
while(cin >> x)
myVector.insert(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这给了我错误信息.
count = 0
i = 11
while count <= 1000 and i <= 10000:
if i%2 != 0:
if (i%3 == 0 or i%4 == 0 or i%5 == 0 or i%6 == 0 or i%7 == 0 or i%9 == 0):
continue
else:
print i,'is prime.'
count += 1
i+=1
Run Code Online (Sandbox Code Playgroud)
我试图通过使用循环来生成第1000个素数.我正确地生成素数,但我得到的最后一个素数不是第1000个素数.我如何修改我的代码才能这样做.提前感谢您的帮助.
编辑:我现在知道如何解决这个问题.但有人可以解释为什么以下代码不起作用?这是我在发布第二个之前写的代码.
count = 1
i = 3
while count != 1000:
if i%2 != 0:
for k in range(2,i):
if i%k == 0:
print(i)
count += 1
break …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下方法计算每月最低付款以偿还贷款:
balance = 999999
annualInterestRate = .18
monthlyInterestRate = annualInterestRate/12
balanceCOPY = balance
#Bisection search parameters
lo = balance/12
hi = (balance*(1+monthlyInterestRate**12))/12
epsilon = .01
guess = (lo + hi)/2
while True:
for month in range(1,13):
balance = balance - guess
balance = balance + (monthlyInterestRate*balance)
if balance > 0 and balance > epsilon:
lo = guess
balance = balanceCOPY
elif balance < 0 and balance < -epsilon:
hi = guess
balance = balanceCOPY
else:
print('Lowest payment: ',str(round(guess,2)))
break
guess …Run Code Online (Sandbox Code Playgroud) 我试图以函数的形式设计一个算法,该函数接受两个参数,一个数组和数组的大小.我希望它返回数组的模式,如果有多种模式,则返回它们的平均值.我的策略是采用数组并首先对其进行排序.然后计算所有出现的数字.当该数字发生时,添加一个计数器并将该计数存储在数组m中.所以m保持所有计数,另一个数组q保持我们比较的最后一个值.
例如:我的名单是{1, 1, 1, 1, 2, 2, 2}
我的m[0] = 4 q[0] = 1
and then m[1] = 3 and q[1] = 2.
所以模式是 q[0] = 1;
不幸的是,到目前为止我没有成功.希望有人可以提供帮助.
float mode(int x[],int n)
{
//Copy array and sort it
int y[n], temp, k = 0, counter = 0, m[n], q[n];
for(int i = 0; i < n; i++)
y[i] = x[i];
for(int pass = 0; pass < n - 1; pass++)
for(int pos = 0; pos < n; pos++) …Run Code Online (Sandbox Code Playgroud) 所以我有两个功能.一个函数检查给定的边是否形成一个直角三角形.问题是,当我在classify中的if-else语句中调用函数时,即使isRightTriangle(sides)的值为true,我总是得到"Not a right Triangle".
bool isRightTriangle(int sides[])
{
std::sort(sides, sides+3);
if((pow(sides[0],2) + pow(sides[1],2)) == pow(sides[2],2))
return true;
return false;
}
void classify(int sides[], ofstream &outfile)
{
int largest(int []);
void typeOfTriangle(int [], ofstream &);
bool isRightTriangle(int []);
outfile << "Largest Side: " << largest(sides) << endl;
typeOfTriangle(sides,outfile);
if(isRightTriangle(sides))
outfile << "Right Triangle\n\n\n";
else
outfile << "Not a Right Triangle\n\n\n";
}
Run Code Online (Sandbox Code Playgroud) 每次运行程序时,我都会收到消息:Segmentation fault(core dumped).我尝试做了一些研究,似乎问题与分配非法内存有关.我也试过调试程序,似乎问题出在linearsort()函数中,因为在将它注释掉后,其余的语句才能正常工作.
#include <iostream>
using namespace std;
int main()
{
void linearsort(int [], int);
int arr[10];
for( int j = 0; j < 10; j++)
arr[j] = j +1;
linearsort(arr,10);
for(int i = 0; i < 10; i++)
cout << arr[i] << " ";
cout << endl;
cin.get();
return 0;
}
void linearsort(int arr[],int n)
{
int temp;
for(int pass = 0; pass < n - 1; n++)
for(int cand = pass + 1; cand < n; cand++){
if(arr[pass] …Run Code Online (Sandbox Code Playgroud) 假设我有一个数组,int array[3] = {1,2,3};我想交换第一个和最后一个元素,以便我有array = {3,2,1};如何使用STL执行此操作?