Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
why this code below have a different output, would you like to explain it ?
input :
10
3 2
4 2
1 1
1 2
1 3
9 3
9 11
9 12
9 1000
8 11
Code #1 :
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll t , limak_max , bob_max , limak_total = 0, bob_total = 0;
cin >> t;
while(t--){
cin >> limak_max >> bob_max;
int i = 1;
while(1){
if(i&1){
limak_total += i;
}
else{
bob_total += i;
}
if(limak_total > limak_max){
cout << "Bob" << '\n';
break;
}
else if(bob_total > bob_max){
cout << "Limak" << '\n';
break;
}
i++;
}
}
}
Code #2 :
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll t , limak_max , bob_max , limak_total , bob_total;
cin >> t;
while(t--){
cin >> limak_max >> bob_max;
limak_total = 0;
bob_total= 0;
int i = 1;
while(1){
if(i&1){
limak_total += i;
}
else{
bob_total += i;
}
if(limak_total > limak_max){
cout << "Bob" << '\n';
break;
}
else if(bob_total > bob_max){
cout << "Limak" << '\n';
break;
}
i++;
}
}
}
Well, just look at where the two codes differ. For Code #2 there are these two lines:
limak_total = 0;
bob_total = 0;
Code #1 does not contain these two lines.
In between different iterations of your while(t--), which I'm assuming to be your different testcases, Code #1 does not reset limak_total and bob_total, which you use when determining your output. Code #2 does.
Therefore they have different outputs.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
i appeared for a campus placement exam few days ago. then while solving it. I found it quite difficult to solve. And atlast i unable to solve it.
the question is as follows:
suppose we input a string of n length. then we have to give second input.that is number of charecters after which space needs to put.
example input:
joebiden
3
expected output
joe bid en
i code something like this.
#include <iostream>
using namespace std;
int main()
{
string str;
int space, con1 = 0, con2 = 0, con3 = 0, i = 0;
cin >> str;
cin >> space;
con1 = str.size();
for (con2 = 0; con2 < con1; con2++) {
for (con3 = 0; con3 < space; con3++) {
cout << str[con2];
if (con3 < (space - 1)) {
con2++;
}
}
cout << " ";
}
return 0;
}
You can use modulo operator to find on which index you should put space.
#include <iostream>
using namespace std;
int main() {
string str;
cin >> str;
int space;
cin >> space;
int n = str.length();
for (int i = 0; i < n; ++i) {
if (i % space == 0) cout << " ";
cout << str[i];
}
return 0;
}
This would be a working and also a shorter approach for your problem:
#include <iostream>
#include <string>
int main()
{
std::string input; std::cin >> input; // Ask the user for input
int segment_len; std::cin >> segment_len; // Ask the user for segment_len
if (segment_len > 0)
{
for (int i = segment_len; i < input.size(); i += segment_len)
{
input.insert(i++, 1, ' ');
}
}
std::cout << input;
return 0;
}
This application firstly takes in a string, then the segment length, and then adds a space after every segment_len characters.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am new in programming and trying to solve some questions and in this question, I am trying to find prime numbers up to n digits using only 1 loop statement.
One loop, and recursion:
#include <iostream>
#include <cmath>
#define NUDIGS 3 //number of digits
bool TestPrime(int n, int ndiv)
{
if ( n == ndiv )
return true;
if ( (n % ndiv) == 0 )
return false;
return TestPrime (n, ++ndiv) ;
}
int main()
{
//calculate the max number with BUDIGITS
int max = pow(10, NUDIGS) - 1;
std::cout << "testing numbers from 2 to " << max << std::endl;
int ntot = 0;
//Loop testing every number. '1' is not considered as a prime number.
for (int i=2; i <= max; i++)
{
//call a recursive test
if ( TestPrime(i, 2) )
{
std::cout << i << " is prime " << std::endl;
ntot++;
}
}
std::cout << ntot << " prime numbers found" << std::endl;
}
//this program will print prime number from 2 to N . and break loop
without using break statment
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int range;
cin>>range;
int num=2;
for(int i=2;i<=num;i++){
if(num>range){
i=num+1;
}
else{
if(i==num){
cout<<num<<" ";
i=1;
num=num+1;
}
else if(num%i==0){
i=1;
num=num+1;
}
}
}
cout<<endl;
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I would like to find the opposite letter using for loop. Also, I would like to note that I am trying to find the opposite letter. For example, replacing "a" with "z", "b" with "y"...
For example, the user inputs this: "3 feg", and the output from this program will be: "uvt". Also, my constraint is 1<=n<=100. The input format is "n input_string_of_length_n", and the output format is "encrypted_string_of_length_n". As a new beginner to programming, I am lost and I do not know how to solve this. Any help will be very much appreciated.
This is my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
`int` user_input_number;
string user_input_text;
cout << "Type: ";
cin >> user_input_number;
cout << "Type: ";
cin >> user_input_text;
for(char i = 'a'; i <= 'z'; i++)
{
cout << << endl;
}
return 0;
}
Here is one solution using ASCII arithmetic:
string s = "abc";
for(int i = 0; i < s.length(); i++){
s[i] = 219 - s[i];
}
cout << s; // "zyx"
The reason it works is that all ASCII characters are between 0 and 127, and this way the values loop back around
// Example program
#include <iostream>
#include <string>
#include <map>
using namespace std;
string encrypt(int n, string s) {
map<char, int> alphabetMap = { };
string alpha = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < 26; i++){
alphabetMap.insert({alpha[i],i});
}
string coded = "";
for (int i = 0; i < n; i++) {
int index = alphabetMap[s[i]];
coded += alpha[25 - index];
}
return coded;
}
int main()
{
int n = 0;
string s = "";
cout << "Enter n and string: " ;
cin >> n >> s;
cout << encrypt(n,s) << endl;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
My arrray looks like
int myarray[]={6,5,2,6,7,8,6};
I want my program to say
if a user enters '6'
"your number is found 3 times at 0,3,6".
And if its not found say"not found".
Here is my effort:
#include <iostream>
using namespace std;
int main() {
bool flag=false;
int x[]={9,11,6,7,6,4,6};
int count=0;
int n,i,j,c;
cout<<"What number do you want to search : ";
cin >>n;
for(int i=0;i<7;i++){
if(x[i]==n){
count++;
flag=true;
}
}
if(flag){
cout <<n<<" is found "<<count<<" times in :";
for(int i=0;i<7;i++){
if(x[i]==n) cout <<i<<',';
}
} else {
cout <<n<<" is not found"<<endl;
}
}
Here is a starting example:
#include <iostream>
using namespace std;
int main(void) {
int myarray[7] = {6,5,2,6,7,8,6};
int value;
cin >> value;
bool not_found = true;
for(int i = 0; i < 7; ++i) {
if(myarray[i] == value) {
cout << "Found value " << value << " at position " << i << "\n";
not_found = false;
}
}
if(not_found)
cout << "Not found\n";
return 0;
}
which, when the user inputs 6, will output:
Found value 6 at position 0
Found value 6 at position 3
Found value 6 at position 6
If you want to match the exact same output as the one in your question, you could store the indices (all the is in an std::vector for example), and then print them.
this is the code to search element in array .this method is linear search
int main()
{
int myarray[100]={6,5,2,6,7,8,6};
int sch,i,count;
int flag=1;
count=sizeof( myarray )/sizeof( myarray[0] );
cout<<"enter element to search";
cin>>sch;
for(i=0;i<count;i++)
{
if(myarray[i]==sch){
cout<<"element"<<sch<<"found. it is in position of"<<i+1;
int flag=0;
}
}
if(flag==1)
cout<<"not found";
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
There are four errors that show; i understand none of them.
Error 1: type name not allowed line 11.
Error 2: expected a ',' line 11(after bool).
Error 3: expected a '}' line 11(after true).
Error 4: expected a declaration line 12(before for).
I am a novice programmer and i kind of have o idea what i'm doing; so please help. Here's the code:
#include "stdafx.h"
#include <iostream>
using namespace std;
1 int main{
2 bool alternate = true ;
3 for (int x = 0; x < 8; x++)
4 {
5 for (int y = 0; y < 4; y++)
6 {
7 if (alternate)
8 {
9 cout << "X ";
10 cout << "O ";
11
12 }
13 else
14 {
15 cout << "O ";
16 cout << "X ";
17
18 }
19 }
20 alternate = !alternate;
21
22 cout << endl;
23 }
24 }
main is a function, so you should declare it as:
int main() {...
The
#include "stdafx.h" is microsoft specific and won't compile on other platforms.
stdafx.h is not necessary for this code to work.
Well, you've just missed some Syntax in the above code. The correct code is :
#include <iostream>
using namespace std;
int main ()
{
bool alternate = true ;
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 4; y++)
{
if (alternate)
{
cout << "X ";
cout << "O ";
}
else
{
cout << "O ";
cout << "X ";
}
}
alternate = !alternate;
cout << endl;
}
return 0;
}
The mistake in your code was that you missed "()" after the main method. Also, you forgot to write the return statement.