1 约数
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
cin >> n;
int root = sqrt(n);
for(int i = 2; i <= root; i++)
{
if(0 == n % i)
{
cout << n / i;
return 0;
}
}
cout << 1;
return 0;
}
2 阶乘
#include<iostream>
using namespace std;
// zero count from tail of n!
int zeroCnt1(int n)
{
int cnt = 0;
while(n)
{
n /= 5;
cnt += n;
}
return cnt;
}
// zero count from tail of n!! when n is even
int zeroCnt2(int n)
{
n /= 10;
int cnt = n;
while(n)
{
n /= 5;
cnt += n;
}
return cnt;
}
int main()
{
int n;
cin >> n;
cout << zeroCnt1(n) << ' ';
if(n % 2)
{
cout << 0;
}
else
{
cout << zeroCnt2(n) << endl;
}
return 0;
}
3 序列
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> vec;
for(int i = 0; i < n; i++)
{
int x;
cin >> x;
vec.push_back(x);
reverse(vec.begin(), vec.end());
}
for(int i = 0; i < n; i++)
{
cout << vec[i] << ' ';
}
return 0;
}
4 糖果
#include <iostream>
#include <memory.h>
using namespace std;
int main()
{
int cnt[256];
memset(cnt, 0, sizeof(cnt));
int start = 0;
int maxLen = 0;
int len = 0;
string s;
cin >> s;
for(unsigned i = 0; i < s.size(); i++)
{
cnt[s[i]]++;
if(2 == cnt[s[i]])
{
start++;
cnt[s[i]] = 1;
}
len = i - start + 1;
if(len > maxLen)
{
maxLen = len;
}
}
cout << maxLen;
return 0;
}
5 迷宫
#include <iostream>
#include <cstdio>
using namespace std;
char maze[10][10];
int main()
{
freopen("maze.in", "r", stdin);
int n, m;
cin >> n >> m;
int row, col;
for(row = 1; row <= n; row++)
{
for(col = 1; col <= m; col++)
{
cin >> maze[row][col];
if('S' == maze[row][col])
{
maze[row][col] = '1';
}
}
}
int cnt = 0;
bool flag = true;
while(flag)
{
flag = false;
for(int row = 1; row <= n; row++)
{
for(int col = 1; col <= m; col++)
{
if(maze[row][col] == '1')
{
if(maze[row+1][col]=='*')
{
cnt++;
maze[row+1][col]='1';
flag = true;
}
if(maze[row-1][col]=='*')
{
cnt++;
maze[row-1][col]='1';
flag = true;
}
if(maze[row][col+1]=='*')
{
cnt++;
maze[row][col+1]='1';
}
if(maze[row][col-1]=='*')
{
cnt++;
maze[row][col-1]='1';
flag = true;
}
if(maze[row+1][col]=='.')
{
maze[row+1][col]='1';
flag = true;
}
if(maze[row-1][col]=='.')
{
maze[row-1][col]='1';
flag = true;
}
if(maze[row][col+1]=='.')
{
maze[row][col+1]='1';
flag = true;
}
if(maze[row][col-1]=='.')
{
maze[row][col-1]='1';
flag = true;
}
}//if
}//for
}//for
}//while
cout << cnt << endl;
return 0;
}
6 盒子
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a, a + n, greater<int>());
int cnt = 0;
int sum = 0;
for(int i = 0; i < n; i++)
{
cnt++;
sum += (a[i] + 1); // 1 means the box itself
if(sum >= n)
{
cout << cnt;
return 0;
}
}
return 0;
}
了解少儿编程、信息学竞赛请加微信307591841或QQ群581357582
信息学竞赛公众号.jpg
网友评论