int main() { int a = 0 , b = 0; cin >> a >> b; cout << "The sum of "; cout << a; cout << " and "; cout << b; cout << " is "; cout << a + b << endl; return 0; }
int main() { int a = 0,b = 0,t = 0; cout << "Please enter two numbers: "; cin >> a >> b; if(a>b){ t = a; a = b; b = t; } while(b >= a){ cout << b << " "; b -- ; } cout << endl; return 0; }
1.4.2节练习
1.12 for循环将-100~100的 数相加,sum = 0。
1.13 利用for循环,重做1.4.1节练习
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream> using namespace std;
int main() { int sum = 0, i = 50; for( i; i <= 100; i++){ sum += i; } cout << sum << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream> using namespace std;
int main() { int i = 10; for( i; i > 0; i--){ cout << i << " "; } cout << endl; return 0; }
int main() { int a = 0 , b =0, t = 0; cout << "Please enter two numbers : "; cin >> a >> b; if(a>b){ t = a; a = b; b = t; } for( a; a <= b; a++){ cout << a << " "; } cout << endl; return 0; }
1.14和1.15 同样不做解答。 1.17,1.18,1.19不做答案。
1.5节练习
1.20 读取一组书籍销售记录,将每条记录打印在标准输出上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream> #include "Sales_item.h"
using namespace std;
int main(){ Sales_item book; while(cin >> book){ cout << book << endl; } return 0; }