题目描述:
最大最小(zdzx)
Description
在正整数 L、D 之间,有一些各位数字的和是 X 的数,请找出其中最小的数 N 和最大的数 M
Input
第 1 行输入整数 L(1 ≤ L ≤ 10,000)
第 2 行输入整数 D(L ≤ D ≤ 10,000)
第 3 行输入整数 X(1 ≤ L ≤ 36)
Output
第 1 行,输出整数 N。
第 2 行,输出整数 M.
Sample Input 1
1 100 4
Sample Output 1
4 40
Sample Input 2
100 500 12
Sample Output 2
129 480
Sample Input 3
1 10000 1
Sample Output 3
1 10000
解析:
水题······
代码:
#include <bits/stdc++.h>
using namespace std;
int l,d,n=21474800,m,x,t,sum;
int main()
{
cin>>l;
cin>>d;
cin>>x;
for(int i=l;i<=d;i++)
{
t=i;
sum=0;
while(t!=0)
{
sum+=t%10;
t/=10;
}
if(sum==x)
{
n=min(i,n);
m=max(i,m);
}
}
cout<<n<<endl<<m;
return 0;
}