26.求整数段和
#include <iostream>
using namespace std;
int main()
{
int a, b;
int sum = 0;
int j = 0;
cin >> a >> b;
for(int i = a; i <= b; i++)
{
j++;
sum += i;
cout << "\t" << i;
if(j % 5 == 0 && i != b)
{
cout << endl;
}
}
cout << endl << "Sum = " << sum;
return 0;
}
28.比较大小
#include <iostream>
using namespace std;
int main()
{
int a[4];
for(int i = 1; i <= 3; i++)
{
cin >> a[i];
}
for(int i = 1; i <= 3; i++)
{
for(int j = i+1; j <= 3; j++)
{
if(a[i] > a[j])
{
a[0] = a[j];
a[j] = a[i];
a[i] = a[0];
}
}
}
for(int i = 1; i < 3; i++)
{
cout << a[i] << "->";
}
cout << a[3];
return 0;
}
30.计算指数
#include <iostream>
using namespace std;
int main()
{
int number;
int sum = 1;
cin >> number;
for(int i = 0; i < number; i++)
{
sum *= 2;
}
cout << "2^" << number << " = " << sum;
}
31.计算阶乘和
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n;
cin >> n;
int sum;
vector<int> number;
for(int i = 1; i <= n; i++)
{
sum = 1;
for(int j = 1; j <= i; j++)
{
sum *= j;
}
number.push_back(sum);
}
sum = 0;
for(int i = 0; i < number.size(); i++)
{
sum += number[i];
}
cout << sum;
return 0;
}
33.跟奥巴马一起画方块
#include <iostream>
using namespace std;
int main()
{
int n;
char a;
cin >> n >> a;
int number = n / 2;
for(int i = 0; i < number; i++)
{
for(int j = 0; j < n; j++)
{
cout << a;
}
cout << endl;
}
return 0;
}
2018/2/6