1.大整数加减
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct Big {
int d[1000];
int len;
//构造函数,方便每一次定义一个大整数时都进行初始化
Big()
{
len = 0;
memset(d, 0, sizeof(d));
//关于memset()函数
//解释:复制字符 c(一个无符号字符)到参数 str 所指向的字符串的前 n 个字符。
//作用:是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法
}
};
//将字符串转化为大整数
Big change(char str[])
{
Big a;
a.len = strlen(str);
int count = 0;
//去除大整数高位0
for (int i = 0; i < a.len; i++)
{
if (str[i] == '0')
count++;
else
break;
}
//将字符串转换为整形存入int a.d[]中
for (int i = 0; i < a.len - count; i++)
{
a.d[i] = str[a.len - i - 1] - '0';
}
a.len -= count;
return a;
}
//比较两数大小
int compare(Big a, Big b)
{
if (a.len > b.len)
return 1;
else if (a.len == b.len)
{
for (int i = a.len - 1; i > 0; i--)
{
if (a.d[i] > b.d[i])
return 1;
else if (a.d[i] < b.d[i])
return -1;
}
return 0;
}
else
return -1;
}
//高精度+
Big add(Big a, Big b)
{
Big c;
int carry = 0; //进位
for (int i = 0; i < a.len || i < b.len; i++)
{
int temp = a.d[i] + b.d[i] + carry;
c.d[c.len++] = temp % 10; //加法得到该位
carry = temp / 10; //新进位
}
if (carry != 0) //加法位数超出任意两位加数
c.d[c.len++] = carry;
return c;
}
//高精度-
Big sub(Big a, Big b)
{
Big c;
for (int i = 0; i < a.len || i < b.len; i++)
{
if (a.d[i] < b.d[i]) //不够减
{
a.d[i + 1]--;
a.d[i] += 10;
}
c.d[c.len++] = a.d[i] - b.d[i];
}
//两数相减得到c位数小于两位减数
while (c.len - 1 >= 1 && c.d[c.len - 1] == 0)
c.len--;
return c;
}
//打印大整数
void print(Big a)
{
for (int i = a.len - 1; i >= 0; i--)
{
cout << a.d[i];
}
cout << endl;
}
int main()
{
char str1[100], str2[100];
while (cin >> str1 >> str2)
{
Big a = change(str1);
Big b = change(str2);
cout << "a+b=";
print(add(a, b));
cout << endl;
cout << "a-b=";
if (compare(a, b) < 0)
{
cout << '-';
print(sub(b, a));
}
else
print(sub(a, b));
}
return 0;
}
2.多关键词排序
#define MAX 100
#define Isleap(x) x%4==0&&x%100!=0||x%400==0 //闰年判断
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int Day[13] = { 0,31,28,30,31,30,31,31,30,31,30,31 };
typedef struct {
char date[100]; //日期
int grade; //成绩
}Record;
bool comp(Record a, Record b)
{
if (a.grade != b.grade)
return a.grade > b.grade;
else
return a.date < b.date;
}
//日期有效性检查
bool isValid(char* str)
{
int y, m, d;
int dd; //2月天数
bool valid = true;
//sscanf()会将参数str的字符串根据参数format字符串来转换并格式化数据。格式转换形式请参考scanf()。转换后的结果存于对应的参数内。
//成功则返回参数数目,失败则返回 - 1,错误原因存于errno中。
sscanf_s(str, "%d/%d/%d", &y, &m, &d);
if (y < 1996 || y>2100)
valid = false;
if (m < 1 || m>12) //月份错位
valid = false;
if (Isleap(y) && m == 2) //闰年2月
dd = Day[m] + 1;
else
dd = Day[m];
if (d<1 || d>Day[m]) //月份对应天数错误
valid = false;
return valid;
}
int main()
{
Record res[MAX];
bool flag[MAX];
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
flag[i] = true;
cin >> res[i].date >> res[i].grade;
}
sort(res, res + n, comp);
for (int i = 0; i < n; i++)
if (isValid(res[i].date))
cout << res[i].date << " " << res[i].grade << endl;
return 0;
}
- 201503-3原题网址
#include<iostream>
using namespace std;
int months[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
bool Isyear(int year)
{
if ((!(year % 4) && year % 100) || !(year % 400))
return true;
else
return false;
}
int Countdays(int y1, int y2)
{
int days = 0;
for (int i = y1; i < y2; i++)
{
if (Isyear(i))
days += 366;
else
days += 365;
}
return days;
}
int main()
{
int a, b, c, y1, y2;
cin >> a >> b >> c >> y1 >> y2;
int count = Countdays(1850, y1);
for (int i = y1; i <= y2; i++)
{
int days = count;
days += Countdays(y1, i);
if (Isyear(i))
months[1] = 29;
else
months[1] = 28;
for (int j = 0; j < a - 1; j++)
days += months[j];
int week = 2 + days % 7; //a月1日星期week
if (week > 7)
week -= 7;
int day = (b - 1) * 7;
if (week <= c)
day += c - week + 1;
else
day += 8 - week + c;
if (day > months[a - 1])
cout << "none" << endl;
else
printf("%d/%02d/%02d\n", i, a, day);
}
return 0;
}
- 201912-1原题网址
#include<iostream>
using namespace std;
bool have7(int x)
{
if (x % 7 == 0)
return true;
while (x)
{
if (x % 10 == 7)
return true;
x = x / 10;
}
return false;
}
int main()
{
int n;
cin >> n;
int lue[4] = { 0 };
int count = 0;
for (int i = 1,count=1; count<=n; i++)
{
if (i % 4 == 1)
{
count++;
if(have7(i))
{
lue[0]++;
count--;
}
}
else if (i % 4 == 2)
{
count++;
if(have7(i))
{
lue[1]++;
count--;
}
}
if (i % 4 == 3)
{
count++;
if(have7(i))
{
lue[2]++;
count--;
}
}
if (i % 4 == 0)
{
count++;
if(have7(i))
{
lue[3]++;
count--;
}
}
}
for (int i = 0; i < 4; i++)
cout << lue[i] << endl;
return 0;
}