#include <iostream> using namespace std; int main() { double n, k1, k2, k3, pos = 2, current_pos = 2, oldsum = 0; cin >> n; cin >> k1 >> k2; while (cin >> k3) { double sum = k1 + k2 + k3; if (sum > oldsum) { oldsum = sum; pos = current_pos; } k1 = k2; k2 = k3; ++current_pos; } cout << oldsum << " " << pos; return 0; } #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n, sum1, sum2= 0; cin >> n; vector<int>dsds(n); for (int i = 0; i < dsds.size(); i++) { cin >> dsds[i]; }
vector<int> dsds2 = dsds; sort(dsds2.begin(), dsds2.end()); sum1 = dsds2[n - 1] + dsds2[n - 2] + dsds2[n - 3]; for (int i = 0; i < dsds.size(); i++) { if (dsds[i] == dsds2[n - 1] || dsds[i] == dsds2[n - 2] || dsds[i] == dsds2[n - 3]) { sum2 = i + 2; break; } } cout << sum1 << " " << sum2; return 0; } in my tests this code works 100%, but i dont know why i have WA#6 Edited by author 16.11.2023 23:08 why do I get runtime error? n = int(input()) for i in range (n) : a=list (map (int,input().split())) a.sort() c = a[::-1] d = c[0] + c[1] + c[2] e = (len(c) // 2) print(d, e) EOF when reading a line 3 #include<iostream> int main() { int32_t a,c=0,c1=0,d=0; std::cin>>a; int32_t b[a]; for(int g=0;g<a;g++){ std::cin>>b[g]; } for(int g=1;g<a-1;g++){ c=b[g-1]+b[g]+b[g+1]; if(c>c1) {c1=c;d=g+1;} } std::cout<<c1<<" "<<d; } У меня код не выдает ошибок на моем Кодеблоке, тут же выдает следующее: solution(6): error C2131: expression did not evaluate to a constant solution(6): note: failure was caused by a read of a variable outside its lifetime solution(6): note: see usage of 'a' solution(8): warning C4552: '>>': result of expression not used Дело в том, что запись "int32_t b[a];" не стоит использовать, когда в квадратных скобках стоит переменная, которая вводится с клавиатуры (не является константой). Для этого нужно использовать запись "int32_t* b= new int32_t [a];", попробуйте заменить в шестой строке. I can't find a problem Please, help my code (C++): #include <iostream> #include <string> using namespace std; int main() { int a, b, c, sum, sr, i, n; cin >> n; cin >> a >> b >> c; sum = 0; if (n == 3) { sum = a + b + c; sr = 2; } else { for (i = 3; i < n; i++) { if ((a + b + c) > sum) { sum = a + b + c; sr = i - 1; } a = b; b = c; cin >> c; } } cout << sum<< " "<<sr;
return 0; } #include <iostream> #include <math.h> #include <iomanip> #include <vector> #include <map> #include <string> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> vec; for (int i = 0; i < n; i++) { int num; cin >> num; vec.push_back(num); } int max = 0; int sum = 0, seredina = 0; int index = 0; multimap<int, int> index_sum; for (int i = 2; i < vec.size(); i++) { sum = 0; if (vec[i] >= vec[i - 1] && vec[i - 1] >= vec[i - 2]) { sum += vec[i] + vec[i-1] + vec[i-2]; index_sum.insert(pair<int, int>(i, sum)); } } for (auto it = index_sum.begin(); it != index_sum.end(); it++) { if (it->second > max) { max = it->second; sum = max; seredina = it->first; } } cout << sum << " " << seredina; return 0; } Edited by author 30.10.2019 21:02 ????? Edited by author 19.10.2019 23:42 this is a succsesful answer in C, but I want to understand it. Who can help me? #include <stdio.h> #include <stdlib.h> int main() { int n, x=1, FM; int arr[1000]; scanf("%d", &n); if(n<=1000 && n>=3) { for(int i=0; i<n; i++) { scanf("%d", &arr[i]); } FM=arr[0]+arr[1]+arr[2]; for(int i=1; i<n-2; i++) { if(arr[i]+arr[i+1]+arr[i+2]>FM) { FM=arr[i]+arr[i+1]+arr[i+2]; x=i+1; } } } printf("%d %d", FM, x+1); return 0; } Edited by author 10.10.2019 12:53 Wrong answer 1 test #include <iostream> using namespace std; int main() { int n, i, x, max; int mas[1000];
cin >> n; max = 0; for (i = 0; i < n; i++) { cin >> mas[i]; } max = mas[0] + mas[1] + mas[2]; x = 1; for (i = 1; i < n; i++) { if (mas[i] + mas[i + 1] + mas[i + 2] > max) { max = mas[i] + mas[i + 1] + mas[i + 2]; x = i + 1; } } cout << max << ' ' << x + 1 << endl; system("pause"); return (0); } Because for i = n - 1, mas[i + 1] and mas[i + 2] go out of bounds #include <iostream> using namespace std; int main() { int a[1000]; int n=0,s,sx=0,m=0; cin >> n; for(int i=0;i<n;i++){ cin >> a[i]; } for(int u=1;u<n-1;u++){ s=a[u]+a[u+1]+a[u+2]; if(s>sx){ sx=s; m=u+1; }} if (n==3){ sx=a[0]+a[1]+a[2]; m=2; } cout << sx << " " << m << endl; } #include<iostream> using namespace std; int main() { int numSection; cin >> numSection; int a[numSection]; for (int i = 0; i < numSection; i++) { cin >> a[i]; } int n = numSection - 3 + 1; int max = 0; int middleNumSec = 0; for (int i = 0; i < n; i++) { if (i + 3 > numSection) { break; } int sum = 0; for (int j = i; j < i + 3; j++) { sum += a[j]; } if (sum > max) { max = sum; middleNumSec = i + 2; } } cout << max << " " << middleNumSec << endl;
system("pause"); return 0; } My solution: #include <iostream> #include <string> #include <vector> #include <set> #include <queue> #include <map> #include <stack> #include <algorithm> #include <bitset> #include <cstring> #include <cmath> #include <cstdlib> #include <cstdio> #include <iomanip> #define F first #define S second #define ll long long #define len length() #define sqr(x) x*x #define pb push_back #define mp make_pair #define sz(x) ((int) (x).size()) #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() #define bp(x) __builtin_popcount(x) #define INF numeric_limits<long long int>::max() #define frp freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++) const int maxn = (int)1e6; const int mod = (int)1e9 + 7; using namespace std;
int n; int a[maxn]; int sum,ans; int cnt; main(){ scanf("%d",&n); for(int i=1; i <= n; i++){ scanf("%d",&a[i]); } for(int i=1; i <= n-2; i++){ sum=a[i]+a[i+1]+a[i+2]; if(ans < sum){ ans=sum; cnt=i+1; } } printf("%d %d",ans,cnt);
return 0; } OMG man, thank u) this is correctly #include <iostream> using namespace std; int a[1000], n = 0, s, sx = 0, m = 0; void main() {
cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int u = 0; u < n; u++) { s = a[u] + a[u + 1] + a[u + 2]; if (s > sx) { sx = s; m = u + 2; } } This is my code. #include <iostream> #include <vector> using namespace std; int main() { int n,a,k=0; cin>>n; vector <int> v; vector <int> sum; vector <int> index; for(int i=0; i<n; i++) { cin>>a; v.push_back(a); } for(int i=1; i<n-1; i++) { sum.push_back(v[i-1]+v[i]+v[i+1]); index.push_back(i); } int max=sum[0]; for(int i=1; i<n; i++) { if(sum[i]>max) {max=sum[i]; k=i;} } cout<<max<<" "<<index[k]+1; system("pause"); return 0; } If you look carefully, you don't get n sums and indexes, but actually n-2 sums and indexes. #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n,count = 1; long long m, sum = 0; vector<long long> coll; vector<pair<long long, int>> coll1; cin >> n; while(n--) { cin >> m; coll.push_back(m); } m = coll[0]; int i = 1; if(coll.size() > 3){ for( ; i < coll.size(); ++i) { if(m <= coll[i] && count < 3) { count++; } else if( i>=3) { sum += coll[i-1]; sum += coll[i-2]; sum += coll[i-3]; coll1.push_back({sum, i-1}); sum = 0; count = 1; } m = coll[i]; } sort(coll1.begin(), coll1.end()); cout << coll1[coll1.size()-1].first << ' ' << coll1[coll1.size()-1].second << endl; } else cout << accumulate(coll.begin(), coll.end(), 0) << ' '<<'2' << endl; coll.clear(); coll1.clear(); return 0; } Edited by author 04.07.2018 19:52 Why did you post that? What's the point? just print the max sum of three consecutive numbers and also the index of the middle element of the numbers. a=[] n=int(input()) for i in range(0,n): f=(int,input()) a.append(f) sum=0 ans=0 for i in range(0,n-2): sum=0 for f in range(i,i+3): sum=sum+a[f] if(sum>ans): ans=sum z=f print(ans) print(z) #What could be the error in this. It works on my computer. Why do they show runtime error #when submitted?? Because of the way you are getting input. http://ideone.com/COmKqTMaybe on your machine you are pressing Enter before after each number. Maybe your interpeter is smarter than mine. You should write: a=list (map (int, input (). split ())) a #include <iostream> #include <queue> using namespace std; int calculate (queue<int> wind) { int result = 0; for (int i = 0; i < 3; i++) { result += wind.front(); wind.pop(); } return result; } int main() { int n, sum = 0, max=0, el, position=2, middle=2; queue<int> window; cin >> n; for (int i = 0; i < 3; i++) { cin >> el; window.push(el); max += el; } for (int i = 0; i < n-3; i++) { cin >> el; window.push(el); window.pop(); sum = calculate(window); position++; if(sum > max) { max = sum; middle = position; } } cout << max << " " << middle; return 0; } #include <stdio.h> int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif int n; scanf("%d", &n); long *sections = new long[n]; for (int i = 0; i < n; i++){ scanf("%ld", §ions[i]); } if (n==3){ printf("%ld %d", sections[0] + sections[1] + sections[2], 2); } else{ int i = 0; int maxpoint = i; while (i + 3 < n){ int j = i + 1; if (sections[i] + sections[i+1] + sections[i+2] < sections[j] + sections[j+1] + sections[j+2]){ maxpoint = j; } i++; } printf("%ld %d", sections[maxpoint] + sections[maxpoint+1] + sections[maxpoint+2], maxpoint+2); } return 0; } Input: 5 1 2 3 4 5 Output: 12 4 ENG: Read carefully the terms. Numbers do not necessarily have to be the same. (Google Translate) RUS: Внимательно читайте условие. Числа не обязательно должны быть одинаковыми. You should not compare sum with the previous sum, but the max sum. Considering sequence: 1 6 6 6 1 1 1 2 2 2 using System; using System.Linq; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); string[] a = Console.ReadLine().Split(' '); int[] a_sum = new int[a.Length - 2]; for(int i = 0; i < a_sum.Length; i++) { a_sum[i] = int.Parse(a[i]) + int.Parse(a[i + 1]) + int.Parse(a[i + 2]); } for(int i = 0; i < a_sum.Length; i++) { if(a_sum[i] == a_sum.Max()) { Console.WriteLine("{0} {1}", a_sum.Max(), i + 2); } } } } |
|