1. 数位之和
#include<iostream>
using namespace std;
int main(){
int n,sum=0;
scanf("%d",&n);
while(n){
sum+=n%10;
n/=10;
}
cout<<sum<<endl;
return 0;
}
20151220
13
2. 消除类游戏
#include <iostream>
#include <cmath>
using namespace std;
const int N = 30;
int grid[N][N], n, m;
int main()
{
cin >> n >> m;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++) {
cin >> grid[i][j];
}
for(int i=0; i<n; i++)
for(int j=0; j<m-2; j++) {
if(abs(grid[i][j]) == abs(grid[i][j+1]) && abs(grid[i][j+1]) == abs(grid[i][j+2])) {
if(grid[i][j] > 0)
grid[i][j] = - grid[i][j];
if(grid[i][j+1] > 0)
grid[i][j+1] = - grid[i][j+1];
if(grid[i][j+2] > 0)
grid[i][j+2] = - grid[i][j+2];
}
}
for(int j=0; j<m; j++)
for(int i=0; i<n-2; i++) {
if(abs(grid[i][j]) == abs(grid[i+1][j]) && abs(grid[i+1][j]) == abs(grid[i+2][j])) {
if(grid[i][j] > 0)
grid[i][j] = - grid[i][j];
if(grid[i+1][j] > 0)
grid[i+1][j] = - grid[i+1][j];
if(grid[i+2][j] > 0)
grid[i+2][j] = - grid[i+2][j];
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(grid[i][j] < 0)
grid[i][j] = 0;
if(j != 0)
cout << " ";
cout << grid[i][j];
}
cout << endl;
}
return 0;
}
4 5
2 2 3 1 2
3 4 5 1 4
2 3 2 1 3
2 2 2 4 4
2 2 3 0 2
3 4 5 0 4
2 3 2 0 3
0 0 0 4 4
3. 画图
#include <iostream>
#include <cstring>
using namespace std;
const int DIRECTSIZE = 4;
const int N = 100;
char grid[N+1][N+1];
struct direct {
int dx, dy;
} direct[DIRECTSIZE] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void fill(int x, int y, char c, int m, int n)
{
int nx, ny;
grid[y][x] = c;
for(int i=0; i<DIRECTSIZE; i++) {
ny = y + direct[i].dy;
nx = x + direct[i].dx;
if(0 <= nx && nx < m && 0 <= ny && ny < n &&
grid[ny][nx] != '|' && grid[ny][nx] != '-' && grid[ny][nx] != '+' &&
grid[ny][nx] != c)
fill(nx, ny, c, m, n);
}
}
int main()
{
int m, n, q, option;
int x1, y1, x2, y2;
char c;
int start, end;
memset(grid, '.', sizeof(grid));
cin >> m >> n >> q;
for(int i=1; i<=q; i++) {
cin >> option;
if(option == 0) {
cin >> x1 >> y1 >> x2 >> y2;
if(x1 == x2) {
start = min(y1, y2);
end = max(y1, y2);
for(int j=start; j<=end; j++)
if(grid[j][x1] == '-' || grid[j][x1] == '+')
grid[j][x1] = '+';
else
grid[j][x1] = '|';
} else if(y1 == y2) {
start = min(x1, x2);
end = max(x1, x2);
for(int j=start; j<=end; j++)
if(grid[y1][j] == '|' || grid[y1][j] == '+')
grid[y1][j] = '+';
else
grid[y1][j] = '-';
}
} else if(option == 1) {
cin >> x1 >> y1 >> c;
fill(x1, y1, c, m, n);
}
}
for(int i=n-1; i>=0; i--) {
for(int j=0; j<m; j++)
cout << grid[i][j];
cout << endl;
}
return 0;
}
4 2 3
1 0 0 B
0 1 0 2 0
1 0 0 A
AAAA
A--A
16 13 9
0 3 1 12 1
0 12 1 12 3
0 12 3 6 3
0 6 3 6 9
0 6 9 12 9
0 12 9 12 11
0 12 11 3 11
0 3 11 3 1
1 4 2 C
................
...+--------+...
...|CCCCCCCC|...
...|CC+-----+...
...|CC|.........
...|CC|.........
...|CC|.........
...|CC|.........
...|CC|.........
...|CC+-----+...
...|CCCCCCCC|...
...+--------+...
................
5.矩阵