作业

1. HELLO WORLD!

#include<iostream>
using namespace std;
int main(void)
{
	cout<<"HELLO WORLD!";
	return 0;
}

2. 乘法表

#include<iostream>
#include<iomanip>
using namespace std;
int main(void)
{
	int x = 0, y = 0;
	cin >> y;
	for (x = 1; x <= y; x++) {
		for (int z = 1; z <= x; z++) {
			cout<<z<<" * "<<x<<" = "<< setiosflags(ios::right) << setw(2)<<x*z<<"  ";
		}
		cout << endl;
	}
	return 0;
}

3.金字塔

#include<iostream>
using namespace std;
int main(void)
{
	int n=0;
	cin >> n;
	for (int i = 1; i <= n; i++) {
			for (int z = 1; z <= n - i; z++) {
				cout << " ";
			}
			for (int z = 1; z <= 2 * i - 1; z++) {
				cout << "*";
			}
			cout << endl;
		}
	return 0;
}

4. 蛇形数组

#include<iostream>
#include<iomanip>
using namespace std;
int a[100][100] = { 0 };
int main(void)
{
	int n=0,count=0;
	cin >> n;
	int i = 0,j = 0,k=1;
	while (count != n) {
		a[i][j] = k;
		k++;
		while (j + 1 <= n && i - 1 >= 0 && a[i - 1][j + 1] == 0) {
			a[i - 1][j + 1] = k;
			k++;
			i--;
			j++;
		}
		count++;
		i = count;
		j = 0;
	}
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			if (a[i][j] != 0) {
				cout<<setiosflags(ios::left) << setw(2)<<a[i][j]<<" ";
			}
		}
		cout << endl;
	}
	return 0;
}

5.报数

#include<iostream>
using namespace std;
int main() 
{
	int m=0,n=0,count = 0;
	cin >>m;
	cin >>n;
	for (int i = 2; i <= m; i++) {
		count = (count + n) % i;
	}
	cout << count+1;
	return 0;
}

6.插入排序

#include<iostream>
using namespace std;
int a[100] = { 0 };
int main() 
{
	int n=0,t=0;
	cin >> n;//n为有多少个需要排序的数
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i < n; i++) {
		for (int j = 0; j < i; j++) {
			if (a[i] < a[j]) {
				t = a[i];
				for (int k=i;k>j;k--) {
					a[k] = a[k-1];
				}
				a[j] = t;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << a[i] << " ";
	}
	return 0;
}

7.A+B

#include<iostream>
#include<string>
using namespace std;
int main() 
{
	string s1, s2;
	cin >> s1>>s2;
	int length_s1, length_s2;
	length_s1 = size(s1);
	length_s2 = size(s2);
	int max = 0;
	if (length_s1 >= length_s2) {
		max=length_s1;
	}
	else {
		max= length_s2;
	}
	int a[100] = { 0 }, t = 0;
	for (int i= length_s1-1; i>=0; i--) {
		a[t++] = s1[i] - '0';
	}
	t = 0;
	for (int i = length_s2-1; i >=0; i--) {
		a[t++] += s2[i] - '0';
	}
	int flag = 0;
	for (int i =0; i<max; i++) {
		if (i == max - 1) {
			if (a[i] >= 10) {
				a[i] -= 10;
				a[i + 1]++;
				flag = 1;
			}
	 }
     else if (a[i] >= 10) {
			a[i] -=10;
			a[i + 1]++;
		}
	}
	if (flag==1) {
		for (int i = max; i>=0; i--) {
			cout << a[i];
		}
	}
	else {
		for (int i = max-1; i >= 0; i--) {
			cout << a[i];
		}
	}
	return 0;
}

posted @ 2021-01-12 02:10  训哥儿  阅读(110)  评论(0编辑  收藏  举报