文章目录
Contest100000588 - 《算法笔记》5.1小节——数学问题->简单数学
5.1 简单数学
例题B1019 数字黑洞 (20分)
链接:
https://pintia.cn/problem-sets/994805260223102976/problems/994805302786899968
//5.1例题B1019数字黑洞
#include <cstdio>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)//递减排序
{
return a > b;
}
void to_array(int n,int num[])//将n的每一位存到num数组中
{
for(int i=0;i<4;i++)
{
num[i] = n%10;
n /= 10;
}
}
int to_number(int num[])//将num数组转换为数字
{
int sum = 0;
for(int i=0;i<4;i++)
{
sum = sum * 10 + num[i];
}
return sum;
}
int main()
{
//MINN和MAX分别表示递增排序和递减排序后得到的最小值和最大值
int n,MIN,MAX;
scanf("%d",&n);
int num[5];
while(1)
{
to_array(n,num);//将数n转化为数组
sort(num,num+4);//从小到大排序
MIN = to_number(num);//获取最小值
sort(num,num+4,cmp);//从大到小排序
MAX = to_number(num);//获取最大值
n = MAX - MIN; //得到下一个数
printf("%04d - %04d = %04d\n",MAX,MIN,n);
if(n == 0 || n == 6174)//下一个数是0或6174则退出
break;
}
return 0;
}
Codeup习题
1939ProblemA守形数
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=0
//1939 Problem A 守形数
//关键在于守形数判断时候的情况分类
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)//输入多组测试数据 n
{
int m = n*n;//求得平方
while(n)//输入数据不断除以10直至为0
{
if(m%10 != n%10)//取余不一样则肯定不为守形数,跳出循环
{
printf("No!\n");
break;
}
m /= 10;
n /= 10;
}
if(!n)//如果m为0,说明循环过程都满足,则为守形数
{
printf("Yes!\n");
}
}
return 0;
}
1940ProblemB反序数
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=1
//1940 Problem B 反序数
//整数提取各个位上的数的方法
#include <cstdio>
#include <iostream>
using namespace std;
void to_array(int n,int num[])
{
for(int i=0;i<4;i++)
{
num[i] = n%10;
n /= 10;
}
}
int main()
{
int num1[5],num2[5];
for(int i=1000;i<=1111;i++)//注意循环次数不是1000-9999
{
to_array(i,num1);
to_array((i*9),num2);
int j;
for(j=0;j<4;j++)
{
if(num1[j] != num2[3-j])
break;
}
if(j==4)
{
printf("%d\n",i);
}
}
return 0;
}
/*
//法2
int main()
{
int num,num9,sum;
for(int i=1000;i<=1111;i++)
{
num=i;num9 = 9*i;
//千位变个位,百位变十位,十位变百位,个位变千位
sum = num/1000*1 + num%1000/100*10 + num%100/10*100 + num%10*1000;
if(sum == num9)
printf("%d\n",num);
}
return 0;
}
*/
1957ProblemC百鸡问题
X=2,10,y=1,3,z=97,97/3=32…1
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=2
//1957ProblemC 百鸡问题
/*
设公鸡数量为x,母鸡数量为y,小鸡数量为z;根据已知可列方程
x+y+z=100
5x+3y+1/3*z<=n
化简整理得:y<=25-x/4*7;
*/
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
//法一,暴力破解
/*
int main()
{
int money;
while(cin>>money)
{
int x,y,z;
for(int x=0;x<=100;x++)
for(int y=0;y<=100;y++)
for(int z=0;z<=100;z++)
{
int zmoney;//注意z作为分数的处理
if(z%3 == 0) zmoney=z/3;
else zmoney=z/3+1;
if(x+y+z==100 && 5*x+3*y+zmoney<= money)
{
cout<<"x="<<x<<",y="<<y<<",z="<<z<<endl;
}
}
}
return 0;
}
*/
//法二,循环优化时间复杂度
int main()
{
int money;
while(cin>>money)//输入钱数
{//注意for循环范围的选定,直接数字浪费很多
for(int x=0;5*x <= money;x++)
{
if(x>100) break;
for(int y=0;3*y+5*x <= money;y++)
{
if(x + y > 100) break;
int tz = (money - 5*x - 3*y) * 3;//计算买x/y后的余额还能买多少z
//z的最终值有两种情况,即若总数超出100,则tz=100-x-y;若总数不超100,则取tz
int maxz = tz+x+y > 100 ? 100-x-y : tz;
if(x+y+maxz==100)//满足共100的个数限制条件则输出最终结果
cout<<"x="<<x<<",y="<<y<<",z="<<maxz<<endl;
}
}
}
return 0;
}
1958ProblemDabc
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=3
//1958 Problem D abc
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int a,b,c;
for(a=0;a<10;a++)
for(b=0;b<10;b++)
for(c=0;c<10;c++)
{
if(a*100+b*10+c + b*100+c*10+c == 532)
{
cout<<a<<" "<<b<<" "<<c<<endl;
}
}
return 0;
}
1968ProblemE众数
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=4
//1968 Problem E 众数
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
//法一:双重数组法
int main()
{
int num[20]={0};
while(cin>>num[0])//对于数组输入多点测试的处理
{
int cnt[11]={0};
cnt[num[0]]++;
for(int i=1;i<20;i++)
{
cin>>num[i];
cnt[num[i]]++;
}
int max = cnt[1];
int max_index = 1;
int j;
for(j=2;j<11;j++)
{
if(max < cnt[j])
{
max = cnt[j];
max_index = j;
}
}
cout<<max_index<<endl;
}
return 0;
}
//法二:结构体法
typedef struct
{
int num;//数据
int cnt;//数据出现次数计数
}Num;
bool cmp(Num a,Num b)
{
if(a.cnt != b.cnt) return a.cnt > b.cnt;
//如果存在一样多次数的众数,则输出权值较小的那一个
return a.num < b.num;
}
int main()
{
Num num[11];
int temp;
while(cin>>temp)//测试数据有多组,输入第一个数据
{
for(int i=1;i<11;i++)//初始化
{
num[i].num = i;
num[i].cnt = 0;
}
num[temp].cnt++;//数据计数加一
for(int i=1;i<20;i++)//输入剩下19个数据
{
cin>>temp;
num[temp].cnt++;
}
sort(num+1,num+11,cmp);//排序
cout<<num[1].num<<endl;
}
}
1970ProblemF计算两个矩阵的乘积
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=5
//1970 Problem F 计算两个矩阵的乘积
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int a[2][3],b[3][2];
while(cin>>a[0][0])//注意多点测试
{
cin>>a[0][1]>>a[0][2];
for(int i=1;i<2;i++)
for(int j=0;j<3;j++)
{
cin>>a[i][j];
}
for(int i=0;i<3;i++)
for(int j=0;j<2;j++)
{
cin>>b[i][j];
}
int res[2][2];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
res[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j];
if(j==0)//j=0就错了
cout<<res[i][j]<<" ";//注意格式
else
cout<<res[i][j]<<endl;
}
}
return 0;
}
1996ProblemG加法等式
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=6
//1996 Problem G 加法等式
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int a,b,c;
for(a=0;a<10;a++)
for(b=0;b<10;b++)
for(c=0;c<10;c++)
{
if(a*100+b*10+c + b*100+c*10+c == 532)[添加链接描述](http://codeup.cn/problem.php?cid=100000588&pid=7)
{
cout<<a<<" "<<b<<" "<<c<<endl;
}
}
return 0;
}
2000ProblemH整数和
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=7
//2000 Problem H 整数和
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int m;
cin>>m;
while(m--)
{
int n,sum=0;
cin>>n;
if(n>=0)
{
for(int i=n;i<=2*n;i++)
sum+=i;
}
else
{
for(int i=2*n;i<=n;i++)
sum+=i;
}
cout<<sum<<endl;
}
return 0;
}
2001ProblemI反序相等
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=8
//2001 Problem I 反序相等
//整数提取各个位上的数的方法
#include <cstdio>
#include <iostream>
using namespace std;
void to_array(int n,int num[])
{
for(int i=0;i<4;i++)
{
num[i] = n%10;
n /= 10;
}
}
int main()
{
int num1[5],num2[5];
for(int i=1000;i<=1111;i++)//注意循环次数不是1000-9999
{
to_array(i,num1);
to_array((i*9),num2);
int j;
for(j=0;j<4;j++)
{
if(num1[j] != num2[3-j])
break;
}
if(j==4)
{
printf("%d\n",i);
}
}
return 0;
}
/*
//法2
int main()
{
int num,num9,sum;
for(int i=1000;i<=1111;i++)
{
num=i;num9 = 9*i;
//千位变个位,百位变十位,十位变百位,个位变千位
sum = num/1000*1 + num%1000/100*10 + num%100/10*100 + num%10*1000;
if(sum == num9)
printf("%d\n",num);
}
return 0;
}
*/
2024ProblemJ多项式的值
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=9
// 2024 Problem J 多项式的值
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int m;
cin>>m;
while(m--)
{
int n;
cin>>n;
int coeffi[n];
for(int i=0;i<=n;i++)
{
cin>>coeffi[i];//多项式系数
}
int x;cin>>x;
int sum=0,weight = 1;//多项式权重
for(int i=0;i<=n;i++,weight *= x)
{
sum += coeffi[i] * weight;//求和
}
cout<<sum<<endl;
}
return 0;
}
2039ProblemK迭代求立方根
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=10
//2039 Problem K 迭代求立方根
#include <cstdio>
#include <iostream>
using namespace std;
/*递归耗时太多
double y(int n,double x)
{
if(n==0)
return x;
else
return y(n-1,x)*2/3 + x/(3*y(n-1,x)*y(n-1,x));
}
int main()
{
double x;
int n;
while(cin>>x>>n)
{
printf("%.6f\n",y(n,x));
// cout<<y(n,x)<<endl;
}
return 0;
}
*/
//用数组法
int main()
{
double x;
int n;
double y[100010];
while (cin >> x >> n)
{
y[0] = x;
for (int i = 1; i <= n; ++i)
y[i] = y[i - 1] * 2 / 3 + x / (3 * y[i - 1] * y[i - 1]);
printf("%.6f\n", y[n]);
}
}
2048ProblemL与7无关的数
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=11
//2048 Problem L 与7无关的数
#include <iostream>
#include <cstdio>
using namespace std;
bool Judge7(int num)
{
if(num % 7 == 0) return false;
while(num && num%10 != 7)//判断该数是否某个位数上的数字为7
num /= 10;
if(num) return false;
return true;
}
int main()
{
int n;
while(cin>>n)
{
int sum = 0;
for(int i=1;i<=n;i++)
{
if(Judge7(i))
{
sum += i*i;//与7无关数的平方和
}
}
cout<<sum<<endl;
}
return 0;
}
2068ProblemM鸡兔同笼
题目链接:http://codeup.cn/problem.php?cid=100000588&pid=12
//2068 Problem M 鸡兔同笼
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
int a;
cin>>a;
int min,max;
if(a%2)//脚的数目一定是偶数才有解
{
min=max=0;
}
else if(a%4)//若a不能整除4,则一定有鸡
{
min = a/4 + 1;
max = a/2;
}
else//若a能整除4,则可以全是兔子
{
min = a/4;
max = a/2;
}
cout<<min<<" "<<max<<endl;
}
return 0;
}
总结下:
数学问题要把握对数字的处理,提取数字各个位上的数字,会用数组表示多项式,解方程等
还有CSDN的标题列表不能
有空格,否则无法跳转