错题本

L1-009 N个数求和 (20分)
 

本题的要求很简单,就是求N个数字的和。麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和也必须是有理数的形式。

输入格式:

输入第一行给出一个正整数N≤100)。随后一行按格式a1/b1 a2/b2 ...给出N个有理数。题目保证所有分子和分母都在长整型范围内。另外,负数的符号一定出现在分子前面。

输出格式:

输出上述数字和的最简形式 —— 即将结果写成整数部分 分数部分,其中分数部分写成分子/分母,要求分子小于分母,且它们没有公因子。如果结果的整数部分为0,则只输出分数部分。

输入样例1:

5
2/5 4/15 1/30 -2/60 8/3
 

输出样例1:

3 1/3
 

输入样例2:

2
4/3 2/3
 

输出样例2:

2
 

输入样例3:

3
1/3 -1/6 1/8
 

输出样例3:

7/24
解:此题当时感觉无从下手,其实只要划分板块,逐个击破即可。

#include<bits/stdc++.h>
#include<algorithm>
#define ll long long
using namespace std;
const ll nl=1e5+5;
int main(){
ll n;
cin>>n;
ll i,j,k;
ll al=0,bl=0;
ll all,bll;
while(n--){
ll a,b;
scanf("%lld/%lld",&a,&b);
if(al==0){
al=a;
bl=b;
}else{
al*=b;
al+=a*bl;
bl*=b;
//cout<<al<<"/"<<bl<<endl;
}
all=al;
bll=bl;
al/=__gcd(all,bll);
bl/=__gcd(all,bll);
//cout<<al<<"/"<<bl<<endl;
}
ll num;
num=al/bl;
al%=bl;
if(num!=0&&al!=0){
cout<<num<<" "<<al<<"/"<<bl;
}else if(num!=0&&al==0){
cout<<num;
}else if(num==0){
if(al!=0){
cout<<al<<"/"<<bl;
}else{
cout<<"0";
}
}
}

 

打表题:

You are given a positive number xx. Find the smallest positive integer number that has the sum of digits equal to xx and all digits are distinct (unique).

Input

The first line contains a single positive integer tt (1t501≤t≤50) — the number of test cases in the test. Then tt test cases follow.

Each test case consists of a single integer number xx (1x501≤x≤50).

Output

Output tt answers to the test cases:

  • if a positive integer number with the sum of digits equal to xx and all digits are different exists, print the smallest such number;
  • otherwise print -1.
Example
input
Copy
4
1
5
15
50
output
Copy
1
5
69
-1

 

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int num;
cin>>num;
if(num/9==0||num==9)cout<<num<<'\n';
else if(num>=10&&num<=17)
{
cout<<num-9<<"9"<<'\n';
}
else if(num>=18&&num<=24)
{
cout<<num-17<<"89"<<'\n';
}
else if(num>=25&&num<=30)
{
cout<<num-24<<"789"<<'\n';
}
else if(num>=31&&num<=35)
{
cout<<num-30<<"6789"<<'\n';
}
else if(num>=36&&num<=39)
{
cout<<num-35<<"56789"<<'\n';
}
else if(num>=40&&num<=42)
{
cout<<num-39<<"456789"<<'\n';
}
else if(num>=43&&num<=44)
{
cout<<num-42<<"3456789"<<'\n';
}
else if(num==45)cout<<"123456789"<<'\n';
else cout<<-1<<'\n';
}
return 0;
}

 

posted @ 2020-11-22 13:38  yyscn  阅读(142)  评论(0)    收藏  举报