HDUOJ 2020-2039(自用)
2020 绝对值排序
1.sort()是C++标准库中的排序函数
sort(start,end,cmp)
start-数组的起始,end-结束地址,注意是左闭右开。比如对十个数排序即为(a,a+10)
2.默认的排序是<,从小到大,可用cmpare()来自定义,缩写为cmp()函数。
cmp()函数的返回值为bool;
return a < b,返回值bool为true,升序;
return a > b,返回值bool为false,降序、
3.bool cmp(const int &a, const int &b){ return a>b; }
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int cmp(int a, int b) {
return abs(a) >abs(b);
}
int main() {
int n;
int a[101];
while (cin >> n && n) {
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n, cmp);
for (int i = 0; i < n; i++) {
printf("%d%c", a[i], (i != n - 1 ? ' ' : '\n'));
}
}
}
2021 发工资咯:)
简单的贪心问题,先取大再取小
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int main() {
int n,sum;
int a[100];
while (cin >> n&&n!=0) {
sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
//贪心
sum += a[i] / 100;
a[i] %= 100;
sum += a[i] / 50;
a[i] %= 50;
sum += a[i] / 10;
a[i] %= 10;
sum += a[i] / 2;
a[i] %= 2;
sum += a[i] ;
}
cout << sum << endl;
}
}
2022 海选女主角
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define MAX 100
int main() {
int m, n, s,x,y;
int mm[MAX][MAX];
int max ;
while (cin >> m >> n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> mm[i][j];
}
}
max = mm[0][0];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (abs(mm[i][j]) > max) {
max = abs(mm[i][j]);
x = i;
y = j;
}
}
}
cout << x+1 << ' ' << y+1 << ' ' << mm[x][y] << endl;
}
}
//不分配数组空间,循环里这样写
scanf("%lf", &t);
if (fabs(t) > fabs(a))
{
a = t;
x = i;
y = j;
}
2023 求平均成绩
1.注意s[],c[]的初始化
2.s[],c[]的求法,一个是横向求一个竖向求
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define MAX 50
int main() {
int n, m, num;
double sum;
int sc[50][5] ;
while (cin >> n >> m) {
num = 0;
int s[50] = { 0 }, c[5] = { 0 };
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> sc[i][j];
s[i] += sc[i][j];
c[j] += sc[i][j];
}
}
for (int i = 0; i < n; i++)
printf("%.2f%c", s[i]*1.0 / m, (i != n - 1 ? ' ' : '\n'));
for (int i = 0; i < m; i++)
printf("%.2f%c", c[i]*1.0 / n, (i != m - 1 ? ' ' : '\n'));
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = 0; j < m; j++) {
if (sc[i][j] < c[j]*1.0/n) {
flag = 1;
break;
}
}
if (flag == 0)
num++;
}
cout << num << "\n"<<endl;
}
}
2024 C语言合法标识符
1.字符串输出添加内容,可以考虑输出时直接添加
2.strlen()函数,gets()后紧接着使用,可以帮助界定循环范围
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<math.h>
using namespace std;
int main() {
int n, i, flag, j;
char text[51];
cin >> n;
getchar();//吸收回车
while (n--)
{
flag = 0;
i = 0;
gets(text);
j = strlen(text);
for (i = 0; i < j; i++)
{
if (i == 0)
{
if (!((text[i] <= 'Z'&&'A' <= text[i]) || (text[i] <= 'z'&&'a' <= text[i]) || text[i] == '_'))
{
flag = 1;
break;
}
}
else if (!((text[i] <= 'Z'&&'A' <= text[i]) || (text[i] <= 'z'&&'a' <= text[i]) || (text[i] <= '9'&&'0' <= text[i]) || text[i] == '_'))
{
flag = 1;
break;
}
}
if (flag)
cout << "no" << endl;
else
cout << "yes" << endl;
}
}
2025 查找最大元素
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<ctype.h>
using namespace std;
int main() {
int n;
char str[101];
while (gets(str)) {
int max = str[0];
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] >= max) {
max = str[i];
}
}
for (int i = 0; i < len; i++) {
cout << str[i];
if (str[i] == max)
cout << "(max)";
}
cout << endl;
}
}
2026 首字母变大写
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<ctype.h>
using namespace std;
int main() {
int n;
char str[101];
while (gets_s(str)) {
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (i == 0)
str[i] = toupper(str[i]);
if (str[i - 1] == ' '&&str[i] >= 'a'&&str[i] <= 'z') {
str[i] -= 32;
}
cout << str[i];
}
cout << endl;
}
}
2027 统计元音
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<ctype.h>
using namespace std;
int main() {
int n,a,e,i,o,u;
char str[101];
cin >> n;
getchar();
while (n--) {
gets_s(str);
a = e = i = o = u = 0;
int len = strlen(str);
for (int k = 0; k < len; k++) {
if (str[k] == 'a' || str[k] == 'A')
a++;
else if (str[k] == 'e' || str[k] == 'E')
e++;
else if (str[k] == 'i' || str[k] == 'I')
i++;
else if (str[k] == 'o' || str[k] == 'O')
o++;
else if (str[k] == 'u' || str[k] == 'U')
u++;
}
cout << "a:" << a << endl;
cout <<"e:" << e << endl;
cout << "i:" << i << endl;
cout << "o:" << o << endl;
cout <<"u:" << u << endl;
if(n)
cout << endl;
}
}
2028 Lowest Common Multiple Plus 求最小公倍数
最小公倍数lcm(x, y) = x * y / gcd(x, y) (其中cd()为求最大公约数函数)
所以求最小公倍数首先要求出最大公约数
gcd()使用辗转相除法:小数和大数/小数所得的余数的最大公约数一样
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<ctype.h>
using namespace std;
int gcd(unsigned int x, unsigned int y) {
if (x < y)swap(x, y);
if (x%y == 0) return y;
else
return gcd(y, x%y);
}
int lcm(unsigned int x, unsigned int y) {
return x * y / gcd(x, y);
}
int main() {
int n;
unsigned int a,l;
while (cin >> n) {
l = 1;
for (int i = 0; i < n; i++) {
cin >> a;
l = lcm(l, a);
}
cout << l << endl;
}
}
2029 Palindromes_easy version 判断回文串
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<ctype.h>
using namespace std;
int main() {
int n;
cin >> n;
getchar();
char str[1000];
while (n--) {
gets_s(str);
int len = strlen(str);
int flag = 1;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i])
flag = 0;
}
if (flag)
cout << "yes"<<endl;
else cout << "no" << endl;
}
}
2030 汉字统计
2031 进制转换(十进制转R进制)
数制转换需要逆序输出,就需要用到栈。所以我们直接用递归调用
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<ctype.h>
using namespace std;
void trans_r(int n,int r) {
if (n / r != 0) {
trans_r(n / r, r);
}
int m = n % r;
if (m > 9)
printf("%c", m % 10 + 'A');
else
cout << m;
}
int main() {
int N, R;
while (cin >> N >> R) {
if (N < 0) {
cout << "-";
N = -N;
}
trans_r(N, R);
cout << endl;
}
}
2032 杨辉三角
法1:二维数组
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<ctype.h>
using namespace std;
int main() {
int n;
int a[30][30] = { 0 };
while (cin >> n) {
for (int i = 0; i < n; i++) {
a[i][0] = 1;
for (int j = 0; j <= i; j++) {
if (i == j)
a[i][j] = 1;
if(i>=1&&j>=1)
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
if (a[i][j] != 0)
printf("%d%c", a[i][j], (i == j ? '\n' : ' '));
}
}
cout << endl;
}
}
因为f(i, j) = f(i-1, j) + f(i-1, j-1),所以我们在编程的时候,完全可以只开一个一维数组。
从i倒退到1,执行A[j] += A[j-1];这样就得到第i行的结果了~
参考源码
#include <stdio.h>
#include <string.h>
int main(void)
{
int i, j, n;
int YanHui[32];
while (scanf("%d", &n) != EOF)
{
memset(YanHui, 0, sizeof(YanHui));
YanHui[0] = 1;
for (i = 0 ; i < n ; i++)
{
printf("%d", 1);
for (j = i ; j ; j--)
YanHui[j] += YanHui[j - 1];
for (j = 1 ; j <= i ; j++)
printf(" %d", YanHui[j]);
putchar('\n');
}
putchar('\n');
}
return 0;
}
2033 人见人爱A+B
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<ctype.h>
using namespace std;
int main() {
int n;
int a[3], b[3],c[3];
cin >> n;
getchar();
while (n--) {
for (int i = 0; i < 3; i++) {
cin >> a[i];
}
for (int i = 0; i < 3; i++) {
cin >> b[i];
}
c[2] = (a[2] + b[2]) % 60;
c[1] = (a[1] + b[1] + (a[2] + b[2]) / 60) % 60;
c[0] = a[0] + b[0] + (a[1] + b[1]) / 60;
cout << c[0] << " " << c[1] << " " << c[2]<<endl;
}
return 0;
}
2034 人见人爱A-B (集合)
集合使用set容器,set的相关用法
参考源码
#include <set>
#include <cstdio>
using namespace std;
int main(void)
{
int n, m, t;
set <int> s;
set <int>::iterator it;
while (scanf("%d%d", &n, &m), n + m)
{
while (n--)
{
scanf("%d", &t);
s.insert(t);
}
while (m--)
{
scanf("%d", &t);
if (s.count(t)) s.erase(t);
}
for (it = s.begin(); it != s.end(); it++)
printf("%d ", *it);
printf(s.size() ? "\n" : "NULL\n");
s.clear();
}
return 0;
}
用数组,把重复的置为负数
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<algorithm>
using namespace std;
int main() {
int n, m;
int a[100], b[100];
while (cin >> n >> m&&(n&&m)) {
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
for (int j = 0; j < n; j++)
if (b[i] == a[j])
a[j] = -1;
}
sort(a, a + n);
int flag = 0;
for (int i = 0; i < n; i++) {
if (a[i] >= 0) {
flag = 1;
printf("%d%c", a[i], (i == n - 1 ? '\n' : ' '));
}
}
if (flag == 0)
cout << "NULL" << endl;
}
}
2035 人见人爱A^B
只需要知道后三位一直乘下去即可
#include<iostream>
#include <stdio.h>
using namespace std;
int main() {
int a, b, sum;
while (cin >> a >> b && (a&&b)) {
sum = a;
for (int i = 1; i < b; i++) {
sum = (sum*a) % 1000;
}
cout << sum << endl;
}
}
2036 改革春风吹满 多边形面积
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<algorithm>
#include<set>
using namespace std;
int main() {
int n;
int x[100], y[100];
double sum;
while (cin >> n&&n) {
sum = 0;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
if (i != 0)
sum += x[i - 1] * y[i] - x[i] * y[i - 1];
if (i == n - 1)
sum += x[i] * y[0] - x[0] * y[i];
}
sum = sum * 0.5;
printf("%.1f%c", sum, '\n');
}
}
2011 多项式求和
贪心问题:活动安排,将活动按结束时间从小到大排序
两个数组的话用到冒泡排序,用结构体则可直接sort
#include<stdio.h>
void Bubble_sort(int arr[], int size)
{
int j,i,tem;
for (i = 0; i < size-1;i ++)//size-1是因为不用与自己比较,所以比的数就少一个
{
int count = 0;
for (j = 0; j < size-1 - i; j++) //size-1-i是因为每一趟就会少一个数比较
{
if (arr[j] > arr[j+1])//这是升序排法,前一个数和后一个数比较,如果前数大则与后一个数换位置
{
tem = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tem;
count = 1;
}
}
if (count == 0) //如果某一趟没有交换位置,则说明已经排好序,直接退出循环
break;
}
}
#include<iostream>
#include <stdio.h>
#include<String.h>
#include<algorithm>
#include<set>
using namespace std;
int main() {
int n;
int s[100], e[100];
while (cin >> n && n) {
int sum = 1;
for (int i = 0; i < n; i++) {
cin >> s[i] >> e[i];
}
for (int i = 0; i < n - 1; i++) {
int flag = 0;
for (int j = 0; j < n - 1 - i; j++) {
if (e[j] > e[j + 1])
{
swap(e[j], e[j + 1]);
swap(s[j], s[j + 1]);
flag = 1;
}
}
if (flag == 0)
break;
}
int endtime = e[0];
for (int i = 1; i < n; i++) {
if (s[i] >= endtime) {
endtime = e[i];
sum++;
}
}
cout << sum << endl;
}
}
2039 三角形
易错点:a,b,c要用实数型,不能用整型
#include<iostream>
#include <stdio.h>
using namespace std;
int main() {
int m;
double a, b, c;
cin >> m;
while (m--) {
cin >> a >> b >> c;
if (a > b)swap(a,b);
if (b > c)swap(b, c);
if (a > b)swap(a, b);
if (a + b > c)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}