我编写了一个程序,它从用户那里获取10个整数,然后计算其中的素数并显示总和.问题是它将所有奇数作为素数.我已经尝试了很长时间,但无法弄明白.这是程序.
#include<iostream>
#include<conio>
#include<math>
int isPrime(int);
int main()
{
int sum_of_prime;
int count=0;
int a[10];
for(int i=1; i<=10; i++){
cout<<"Enter a number: ";
cin>>a[i];
if( isPrime(a[i]) ){
sum_of_prime+=a[i];
countp++;
}
}
cout<<"Total Prime Numbers in given Numbers: "<<count<<endl;
cout<<"Sum of All the prime numbers in given numbers"<<sum_of_prime<<endl;
getch();
}
int isPrime(int n){
for(int i=2; i<=sqrt(n); i++){
if(n%i==0)
return 0;
else
return 1;
}
}
Run Code Online (Sandbox Code Playgroud) 我需要获取一个列表并使用字典来编目列表中特定项目的位置,例如:
L = ['a','b','c','b','c','a','e']
字典需要包含以下内容:
D = {'a':0,5,'b':1,3,'c':2,4,'e':6}
但是,如果我使用我写的:
for i in range(len(word_list)):
if D.has_key('word_list[i]') == False:
D['word_list[i]'] = i
else:
D[word_list[i]] += i
Run Code Online (Sandbox Code Playgroud)
然后我得到一个单词的KeyError,我不明白为什么我应该得到一个错误.
我想做一个简单的函数涉及sqrt(),floor()和pow().所以,我包括在内<math.h>.当我尝试使用我的功能时,我的程序会说sqrt()并且floor()不存在.我已经对我的文件进行了三次检查并重写了它们,但它仍然会出现同样的错误.只是为了检查目录是否有任何问题<math.h>,我创建了另一个单独的文件来计算相同的东西并且它有效.我现在很无能为力.我究竟做错了什么?
非运作程序的代码:
#include <math.h>
#include "sumofsquares.h"
int sumofsquares(int x){
int counter = 0;
int temp = x;
while(temp != 0){
temp = temp - (int)pow(floor(sqrt(temp)), 2);
counter ++;
}
return counter;
}
Run Code Online (Sandbox Code Playgroud)
工作测试文件:
#include <stdio.h>
#include <math.h>
int main(void){
printf("%d", (int)pow(floor(sqrt(3)), 2));
}
Run Code Online (Sandbox Code Playgroud)
错误就是这个
/tmp/ccm0CMTL.o:在函数sumofsquares'中:/home/cs136/cs136Assignments/a04/sumofsquares.c:9:对sqrt'/home/cs136/cs136Assignments/a04/sumofsquares.c:9的未定义引用:undefined reference to floor'collect2:ld返回1退出状态`
我在虚拟Ubuntu OS上使用runC进行编译
我正在创建一个登录注册页面,其中包含两个编辑文本框(一个用于用户名,另一个用于密码)和一个登录按钮.如果没有在文本框中填写所需信息,用户就无法进入.我怎样才能做到这一点?
该代码在"算法简介"一书中给出.为此,我使用了1个索引数组
#include <cstdlib>
#include <iostream>
using namespace std;
int n=6;
int x[1000];
void max_heapify(int a[],int i)
{
int left=2*i;
int right=2*i+1;
int largest=i;
if( left<=n && a[left]>a[largest]){
largest=left;
}
if(right<=n && a[right]>a[largest])
{
largest=right;
}
if(largest!=i)
{
int t=a[i];
a[i]=a[largest];
a[largest]=t;
}
max_heapify(a,largest);
}
void max_heap(int a[])
{
int heap_length=n;
for(int i=n/2;i>=1;i--)
max_heapify(a,i);
}
int main(int argc, char *argv[])
{
for(int i=1;i<=n;i++)
cin>>x[i];
max_heap(x);
cout<<endl;
for(int i=1;i<=n;i++)
cout<<x[i]<<" ";
// system("PAUSE");
//return EXIT_SUCCESS;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于这个输入
1 5 7 …Run Code Online (Sandbox Code Playgroud) 我正在一个面向对象程序的类中编写一个get函数,但是i++我的代码中没有执行某些原因.
这是我的.cpp文件中使用的内容:
char MyString::Get(int i)
{
if( i = '\0')
{
exit(1);
}
else
{
return String[i];
}
}
Run Code Online (Sandbox Code Playgroud)
这是main.cpp文件中调用的内容:
for(int i=0; i < String1.Length()+1; i++)
{
cout<< String1.Get(i)<<" ";
}
cout << endl;
Run Code Online (Sandbox Code Playgroud)
这是.cpp文件中的length方法,供参考:
int MyString::Length()
{
int counter(0);
while(String[counter] != '\0')
{
counter ++;
}
return (counter);
}
Run Code Online (Sandbox Code Playgroud)
另外:String1 = Jello World
输出:
JJJJJJJJJJJJJ
我有一段代码需要理解.但是我迷失了一点.这是代码:
typedef unordered_map <string, TimeStampSet *> HIEMap;
typedef set <TimeStamp> TimeStampSet;
struct HostInfo {
HostActivity *hostActivity;
HIEMap *hieMapArr;
};
typedef unordered_map <uint32_t, HostInfo *> HostInfoMap;
HIEMap::iterator hieMapIt;
void method(...){
for (hieMapIt = hostInfoIt -> second -> hieMapArr -> begin();
hieMapIt != hostInfoIt -> second -> hieMapArr -> end();
hieMapIt = nextMapIt)
{
if (hieMapIt -> second == NULL) {
//what does *hieMapIt -> second* returns?
}
}
}
Run Code Online (Sandbox Code Playgroud)
什么hieMapIt -> second回报?我有点迷茫.
这不是所有代码,都有初始化等等.但是我没有把所有的代码放在这里.
谢谢,
什么是代码明智的开发和网站的faging之间的区别?
我的意思是开发< - >直播,比直播所有代码都干净/最小化,对吧?
任何信息,现实生活中的"代码"部分示例?
任何人都可以告诉我Nunit测试的结果是什么?它是否会产生通过或失败(带错误信息),或者它会在哪一点产生错误和更多细节?我们可以将错误结果存储在XML文件中吗?
您好我在C++编程中非常糟糕,我有这个项目要做.因此程序必须在整个硬盘中搜索MP3文件,并将其名称,路径和ID标签写入文本文件并为Windows平台制作.整个不使用数组.任何人都可以帮助我一点吗?
我有一个字符串
01 01 01 02 01 01 20 00 40 0b 00 01 ef cc 45 4e 47 00 1a 02
我怎么读这个作为输入?我知道宣布
String s = "01 01 01 02 01 01 20 00 40 0b 00 01 ef cc 45 4e 47 00 1a 02"
Run Code Online (Sandbox Code Playgroud)
显然会给我带来错误,那么有什么不同的方法可以使用Java读取这个输入(作为参数传递)?
我有以下数据:
htno sub marks credits
1 a 15 0
1 b 10 0
1 c 25 4
1 d 24 4
1 e 22 2
1 f 12 0
2 a 22 4
2 b 15 0
2 c 23 4
2 d 18 2
2 e 20 4
2 f 6 0
3 a 22 4 n so on
Run Code Online (Sandbox Code Playgroud)
我将向用户提供表格.如果用户输入他的halticket数字"1",那么它应该以这种方式在PHP和MySQL中显示输出:
Number of Backlogs Passed Subjects
3 3
Run Code Online (Sandbox Code Playgroud)
这意味着它应该计算零的数量并显示积压的数量并计算非零的数量并显示传递的主题的数量.
我怎样才能做到这一点?