Your Objectives:
#include <stdio.h>
int main() {
int cases,x,y;
scanf("%d",&cases);
while (cases>0) {
--cases;
scanf("%d %d",&x,&y);
printf("%d\n",x+y);
}
}
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int cases,x,y;
cin >> cases;
while (cases>0) {
--cases;
cin >> x >> y;
cout << x + y << "\n";
}
int main() {
int x,y;
while (1) {
scanf("%d %d",&x,&y);
if (x==-1 && y==-1)
break;
printf("%d\n",x+y);
}
}
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int x,y;
while (1) {
cin >> x >> y;
if (x==-1 && y==-1)
break;
cout << x + y << "\n";
}
}
int main() {
int x,y;
while (scanf("%d %d",&x,&y) && x != 1 && y != 1) {
printf("%d\n",x+y);
}
}
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int x,y;
while (cin >> x >> y && x != 1 && y != 1) {
cout << x + y << "\n";
}
}
int main() {
int x,y;
while (scanf("%d %d",&x,&y) != EOF) {}
printf("%d\n",x+y);
}
}
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int x,y;
while (cin >> x >> y) {
cout << x + y << "\n";
}
}
ios_base::sync_with_stdio(false)
; and cin.tie(NULL)
; to prevent flushing the output. (Maybe put that in your TRD!)endl
will force-flush the output. Use \n
instead.scanf
has some regular-expression like features that can be useful.On the other hand, you must match the type and pass in a reference.
Code | Meaning |
%d | Scan an integer |
%lld | Scan a long long integer |
%s | Scan a string |
%c | Scan a character |
// will read "(10,20)"
scanf("(%d,%d)");
// will read "(10,20)", "( 10, 20 )", but not "(10 ,20)"
scanf(" ( %d, %d )");
// will read "110101 eieio"
scanf("%[01] %[aeiou]");
fgets (name, 100, stdin);
will read a whole line into the string.getline(cin, name);
getline(cin >> std::ws, name);
to read leading whitespace first.%d
outputs an integer%5d
outputs an integer, using 5 characters, leading spaces.%05d
outputs an integer, using 5 characters, leading zeros.int n = 25;
printf("%d, %5d, %05d\n",n,n,n);
Output: 25, 25, 00025
cout
, use e.g., cout << width(5) << n << "\n";
flush(stdout)
every time you print, or else use endl
with cout
.#include <stdio.h>
int main() {
int x,y;
while (scanf("%d %d",&x,&y) != EOF) {
if (x==-1 && y==-1)
break;
printf("%d\n",x+y);
fflush(stdout);
}
}