在这里,我们找到具有最高总和并显示该总和的八个相邻数字。我们还需要让它显示加起来这个值的八个相邻数字。我被困在如何显示这些值。我到目前为止的代码如下:
var express = require('express');
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
var mongoose = require('mongoose');
var config = require('./config');
var Item = require('./models/item');
// Mongoose Connect
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static('public'));
/* STEP 1 - creating objects and constructors*/
var runServer = function(callback) {
mongoose.connect(config.DATABASE_URL, function(err) {
if (err && callback) {
return callback(err);
}
console.log("Connected to mongodb!");
app.listen(config.PORT, function() {
console.log('Listening on localhost:' + config.PORT);
if (callback) {
callback();
}
});
});
};
if (require.main === module) {
runServer(function(err) {
if (err) {
console.error(err);
}
});
};
/* STEP 2 - api end points */
app.get('/items', function(req, res) {
Item.find(function(err, items) {
if (err) {
return res.status(500).json({
message: 'Internal Server Error'
});
}
res.json(items);
});
});
app.post('/items', jsonParser, function(req, res) {
console.log("req:", req.body);
if (!req.body) {
return res.sendStatus(400);
}
else {
var favoritesList = req.body;
res.status(201).json(favoritesList);
}
Item.create({
name: req.body.name,
wineResults: req.body.wineResults
},
function(err, item) {
if (err) {
//return res.sendStatus(500).json({
message: err
//});
}
else {
message: "done"
//res.sendStatus(200);
}
});
});
app.use('*', function(req, res) {
res.status(404).json({
message: 'Not Found'
});
});
/* STEP 3 - server settings*/
app.listen(process.env.PORT || 8080, process.env.IP);
exports.app = app;
exports.runServer = runServer;
答案 0 :(得分:1)
这是我对两种不同方法的看法。第一种是更传统的方法,而第二种方法则使用LINQ:
Sub Main()
Dim chunkSize As Integer = 8
Dim source As String =
"73167176531330624919225119674426574742355349194934" &
"96983520312774506326239578318016984801869478851843" &
"85861560789112949495459501737958331952853208805511" &
"12540698747158523863050715693290963295227443043557" &
"66896648950445244523161731856403098711121722383113" &
"62229893423380308135336276614282806444486645238749" &
"30358907296290491560440772390713810515859307960866" &
"70172427121883998797908792274921901699720888093776" &
"65727333001053367881220235421809751254540594752243" &
"52584907711670556013604839586446706324415722155397" &
"53697817977846174064955149290862569321978468622482" &
"83972241375657056057490261407972968652414535100474" &
"82166370484403199890008895243450658541227588666881" &
"16427171479924442928230863465674813919123162824586" &
"17866458359124566529476545682848912883142607690042" &
"24219022671055626321111109370544217506941658960408" &
"07198403850962455444362981230987879927244284909188" &
"84580156166097919133875499200524063689912560717606" &
"05886116467109405077541002256983155200055935729725" &
"71636269561882670428252483600823257530420752963450"
Dim strChunk As String
Dim strMaxChunk As String = ""
Dim curSum, MaxSum As Integer
Dim values() As Integer
For i As Integer = 0 To source.Length - chunkSize
strChunk = source.Substring(i, chunkSize)
values = Array.ConvertAll(strChunk.ToCharArray, Function(c) CInt(c.ToString()))
curSum = values.Sum
If curSum > MaxSum Then
MaxSum = curSum
strMaxChunk = strChunk
End If
Next
Console.WriteLine("Traditional")
Console.WriteLine("Max Sum = " & MaxSum & " from " & strMaxChunk)
Dim sums = From chunk In Enumerable.Range(0, source.Length - chunkSize).Select(Function(x) source.Substring(x, chunkSize))
Select chunk, sum = Array.ConvertAll(chunk.ToCharArray, Function(y) CInt(CStr(y))).Sum
Order By sum Descending
Dim linqResult = sums.First
Console.WriteLine("Linq")
Console.WriteLine("Max Sum = " & linqResult.sum & " from " & linqResult.chunk)
Console.ReadLine()
End Sub