我正在尝试编写一个接受n个整数输入的程序,并找出在给定输入中出现最大次数的程序.我正在尝试运行t案例的程序.为此,我实现了一种类似于算法的计数排序(可能有点过时),它计算输入中每个数字的出现次数.如果有多个具有相同最大出现次数的数字,我需要返回其中较小的数字.为此,我实施了排序.
我面临的问题是,每次我在Visual C++上运行程序时,我都会收到一个错误,告诉"向量下标超出范围".在Netbeans下,它产生的返回值为1并退出.请帮我找到问题所在
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int findmax(vector<int> a, int n)
{
int i,ret;
ret = 0;
for ( i = 0; i <n; i++)
{
if (a[i] > ret) {
ret = a[i];
}
}
return ret;
}
int main() {
int i = 0, j = 0, k = 0, n,m,r1,r2;
vector<int> a;
int t;
vector<int> buff;
cin>>t;
while(t--) {
cin>>n;
a.clear();
buff.clear();
for ( i …Run Code Online (Sandbox Code Playgroud) 我正在学习套接字网络API.在这个过程中,我编写了一个使用TCP的简单Echo服务器.我编写代码的方式是,只要服务器运行,客户端控制台上输入的任何内容都应该回显给它.但是,我无法做到这一点.虽然,对于第一次输入,我得到回声,从下次开始,我没有得到任何消息.我知道,我们可以实现它来运行许多客户端使用fork(),但我想知道阻止客户端背后的原因,以及可能的方法来纠正它.这是客户端的代码:
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#define MAXCOUNT 1024
int main(int argc, char* argv[])
{
int sfd;
char msg[MAXCOUNT];
char blanmsg[MAXCOUNT];
struct sockaddr_in saddr;
memset(&saddr,0,sizeof(saddr));
sfd = socket(AF_INET,SOCK_STREAM,0);
saddr.sin_family = AF_INET;
inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr);
saddr.sin_port = htons(5004);
connect(sfd,(struct sockaddr*) &saddr, sizeof(saddr));
for(; ;) {
memset(msg,0,MAXCOUNT);
memset(blanmsg,0,MAXCOUNT);
fgets(msg,MAXCOUNT,stdin);
send(sfd,msg,strlen(msg),0);
recv(sfd,blanmsg,sizeof(blanmsg),0);
printf("%s",blanmsg);
fflush(stdout);
}
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
这是服务器的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#define MAXCOUNT 1024
int …Run Code Online (Sandbox Code Playgroud) 这是我在Interviewstreet 代码印刷中遇到的一个问题.我无法找到解决方案,甚至没有想到它的方向.如果有人能帮助我找到灵魂,或者解释我需要如何解决这个问题,我会感激不尽.
给定数字1,2,3,...,N,按顺序排列它们,使得生长数字的乘积之和最大化.
例如:如果N = 3,我们将它们命名为(1,2,3),产品总和为1*2 + 2*3 = 8,如果我们将它们命名为(1,3,2),则总和产品是1*3 + 3*2 = 9.
输入格式:
输入的第一行包含T,测试用例的数量.然后按照T行,每行包含一个整数N.
输出格式 :
对于每个测试用例,打印相邻数字的最大乘积和.
样本输入:
2 2 4
样本输出:
2 23
说明:
在给出置换的第一个测试案例中是(1,2).所以产品的最大总和是1*2.在第二个测试案例中,数字是(1,2,3,4).排列1,3,4,2具有相邻数字的乘积之和为1*3 + 3*4 + 4*2 = 23.没有其他排列的相邻数的乘积之和超过23.
制约因素:
1 <= T <= 10 1 <= N <= 200000
我需要一个可以处理像C++这样的重复键的容器multimap.虽然Guava中存在一个已知的实现,但我还是喜欢标准的Java API.提前致谢
我正在用 C++ 编写一个程序,该程序需要以快速方式查找和存储 IP 地址(所有 IPv4)。每个 IP 地址都有与之关联的数据。如果它已经存在于树中,我打算将树中的 IP 地址数据与新地址数据合并。如果它不存在,我打算将它作为新条目添加到树中。不需要删除 IP 地址。
为了实现这一点,我需要设计一个 Patricia Trie。但是,我无法想象除此之外的设计。我似乎很天真,但我想到的唯一想法是将 IP 地址更改为二进制形式,然后使用特里树。然而,我对如何实现这一点一无所知。
如果你能帮助我解决这个问题,我将非常感谢你。请注意,我确实在这里找到了类似的问题。问题或更具体的答案超出了我的理解,因为 CPAN 网站中的代码对我来说不够清楚。
另请注意,我的数据格式如下
10.10.100.1:“汤姆”、“杰克”、“史密斯”
192.168.12.12:“琼斯”、“丽兹”
12.124.2.1:“吉米”、“乔治”
10.10.100.1:“迈克”、“哈利”、“詹妮弗”
请帮我看看Java中的Counting Sort的以下实现.我是Java和调试新手,所以我不确定错误.下面的代码的问题是,虽然它编译,我没有在屏幕上得到任何输出.请仔细阅读代码并向我推荐一些内容.也许存在一些逻辑错误.谢谢
import java.io.*;
import java.lang.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Cnt {
public static void main(String[] args) throws java.lang.Exception {
BufferedReader R = new BufferedReader(new InputStreamReader(System.in));
int[] a ;
int[] b;
String inp = R.readLine();
int N = Integer.parseInt(inp);
a = new int[N];
b = new int[N];
for ( int i = 0; i< N; i++) {
a[i] = Integer.parseInt(R.readLine());
}
int key = findmax(a);
int k = key+1;
int c[] = new int[k];
for ( int i …Run Code Online (Sandbox Code Playgroud) 我想实现一个vector<int>内部的vector<Type>C++中.但是每当我运行以下代码时,我都会收到错误信息
std::vector<std::vector<int> >::const_iterator’ has no member named ‘begin’
std::vector<std::vector<int> >::const_iterator’ has no member named ‘end’
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
typedef vector<int> vector1D ;
typedef vector<vector1D > vector2D ;
void showarr(const vector2D& v)
{
for (vector<vector1D >::const_iterator it1 = v.begin(); it1 != v.end(); ++it1) {
for(vector<int>::const_iterator it2 = *it1.begin(); it2 != *it1.end(); ++it2) {
cout<<*it2<<endl;
}
}
}
int main(int argc, char *argv[])
{
int rownum;
cin>>rownum;
vector2D a; …Run Code Online (Sandbox Code Playgroud) 我是java的初学者,并为postfix evaluationataion编写了以下问题.虽然该程序适用于控制台IO.使用文件时出现问题.我试图在文件中获取程序的答案output00.txt,但文件是空的.这是代码.我遗漏了对这个问题无关紧要的方法.
public static void main(String[] args) throws Exception {
FileReader input = new FileReader("input00.txt");
BufferedReader in = new BufferedReader(input) ;
FileWriter output = new FileWriter("output00.txt");
BufferedWriter out = new BufferedWriter(output);
while (true) {
String lineReader = in.readLine();
if (lineReader==null) {
break;
}
Stack<String> m_Stack = new Stack<String>();
m_Stack.addAll(Arrays.asList(lineReader.trim().split("[ \t]+")));
if (m_Stack.peek().equals("")){
continue;
}
try {
double finVal = evaluateRPN(m_Stack);
if (!m_Stack.empty()) {
throw new Exception();
}
out.write(finVal + "\n");
}
catch (Exception e) {System.out.println("error");}
}
}
Run Code Online (Sandbox Code Playgroud)
文件input00.txt包含 …
我试图直接从控制台使用getline将用户输入转换为C++字符串对象.但是我没有这样做,因为编译器给了我以下错误.
main.cpp: In function ‘int main(int, char**)’:
main.cpp:52:28: error: no matching function for call to ‘std::basic_ifstream<char>::getline(std::ifstream&, std::string&)’
main.cpp:52:28: note: candidates are:
/usr/include/c++/4.6/istream:599:5: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize, std::basic_istream<_CharT, _Traits>::char_type) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::char_type = char, std::streamsize = int]
/usr/include/c++/4.6/istream:599:5: note: candidate expects 3 arguments, 2 provided
/usr/include/c++/4.6/istream:408:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::char_type = char, std::streamsize = int]
/usr/include/c++/4.6/istream:408:7: …Run Code Online (Sandbox Code Playgroud) 我是Python的新手,并且模数有问题.
这是代码:
for i in range(ord('a'), ord('z')+1):
print chr(((i+2) % 97) + 97 )
Run Code Online (Sandbox Code Playgroud)
检测到的结果是cdef...a.但是,一旦我们到达,我就无法获得所需的模块行为z.