#include<iostream> intmain() { int i = 50,sum = 0; while(i <= 100){ sum += i; i++; } }
1.10
1 2 3 4 5 6 7 8 9
#include<iostream> intmain() { int i = 10; while (i >= 0){ std::cout<<" "<<i; i--; } }
1.11
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream> intmain() { int i,j,low; std::cout<<"Please input two int numbers:"; std::cin>>i>>j; low = i > j? j:i; high = i > j? i:j; while(low <= high){ std::cout<<low<<" "; low++; } }
1.12
程序是将 -100 到 100 的所有整数相加,sum 终值为 0。
1.16
1 2 3 4 5 6 7 8 9 10 11
#include<iostream> intmain() { int sum = 0, value = 0; while (std::cin>>value) { sum += value; } std::cout<<sum; return0; }
Chapter 2
2.1
An int, long, long long and short types are representation of integral types of a chunk of allocated memory. The difference between them is in the size of the representation (in bytes) of the respective type, which is often implementation dependent.
The unsigned type represents a positive value, while an unsigned represents both positive an negative values of an integral type (except for bool). The limits range of the value depends on the size of integral type. Given that unsigned holds only positive value, it has a higher max limit.
A float and a double are floating-point types that represent "single-, double-, and extended-precision values." Their representation is implementation dependent. Most compilers follow the IEEE 754 standard at least for float and double.
int i = 42; intmain() { int i = 100; int j = i; } // j 为 100
2.14
1 2 3 4 5 6
int i = 100, sum = 0; for(int i = 0; i != 10; i++){ sum += i; } std::cout<< i <<" "<< sum <<endl; // 将会输出 100 45
2.15
合法;(b) 非法;(c) 合法;(d) 非法
2.16
合法;(b) 合法;(c) 合法;(d) 合法
2.17
1 2 3 4
int i, &ri = i; i = 5; ri = 10; std::cout<<i<<" "<<ri<<std::endl; // 输出 10 10
2.19
reference:
the use of & before an type declares a reference. E.g.: int &i = i;
not an object in memory
must be initialized
its binding cannot be changed after initial definition
pointer:
declared with * symbol. E.g.: int *p;
assigned an address with an assign operator. E.g.: p = &a;
assigned the value to which a pointer points to is dereferenced with . E.g.: b = p
is an object in memory
can be declared but not initialized
holds an address as a value
its value can be change to point to another address
can be used in pointer asthmatics
2.20
1 2 3 4
int i = 42; int *pi = &i; *pi = *pi * *pi; // 等价于 i = i * i
2.24
p is a pointer to void pointing to an int, while lp is a pointer to a long pointing to an int. p is legal and lp is not because a pointer must match the type of object it is pointing to, unless it is a void pointer.
#include<iostream> #include<vector> using namespace std; int main() { vector<int> ivec; int c; while( cin>>c ){ if ( c%2 != 0 ) c = c*2; ivec.push_back(c); } for (auto c : ivec) cout<< c <<" "; return0; }
voidswap(int *(&a), int *(&b)) { int *t; t = a, a = b, b = t; } intmain() { int a = 6, b = 8; int *x = &a, *y = &b; cout<<x<<" "<<y<<endl; swap(x, y); cout<<x<<" "<<y; return0; }
intsum( initializer_list<int> il ){ int res = 0; for (auto i : il){ res += i; } return res; } intmain() { int ans = sum({ 1, 2, 3, 6, 8, 16}); cout<<ans; }