![](/img/trans.png)
[英]Custom exe completes instantly on WinPE without displaying any output
[英]Not displaying any output
即使在函數的第一和第二循環之間顯示cout語句的輸出,也不顯示該函數的第二循環的任何輸出。 為cin輸入3 4 8 5。 我需要分數為0 51。總分數為6。
碼:
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
long getMaxScore(vector < long > a){
// Complete this function
long runningSum = 0;
int n = a.size();
vector<long> scores(n);
int j=0;
long totalScore;
for(int i = 0; i < n;)
{
scores[j] = runningSum%a[n-1];
runningSum = runningSum + a[n-1];
n--;
j++;
}
cout<<scores[0]<<endl;
cout<<scores[1]<<endl;
cout<<scores[2]<<endl;
for(long k=0; k<n; k++)
{
totalScore = 0;
totalScore+=scores[k];
cout<<totalScore<<endl;
}
//return totalScore;}
int main() {
int n;
cin >> n;
vector<long> a(n);
for(int a_i = 0; a_i < n; a_i++){
cin >> a[a_i];
}
long maxScore = getMaxScore(a);
return 0;}
問題
循環
for(int i = 0; i < n;)
{
scores[j] = runningSum%a[n-1];
runningSum = runningSum + a[n-1];
n--;
j++;
}
當n
等於零時結束。
因此,當程序遇到循環時
for(long k=0; k<n; k++)
{
totalScore = 0;
totalScore+=scores[k];
cout<<totalScore<<endl;
}
由於k < n
為假,因此在循環內部沒有執行任何操作。
解
我建議在第二個循環之前添加一行以重置n
的值。
n = a.size();
for(long k=0; k<n; k++)
{
...
}
移動線
totalScore = 0;
在循環之外累積分數。
totalScore = 0;
for(long k=0; k<n; k++)
{
totalScore += scores[k];
cout << totalScore << endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.