String
Problem Description
Avin has a string. He would like to uniform-randomly select four characters (selecting the same character is allowed) from it. You are asked to calculate the probability of the four characters being ”avin” in order.
Input
The first line contains n (1 ≤ n ≤ 100), the length of the string. The second line contains the string. To simplify the problem, the characters of the string are from ’a’, ’v’, ’i’, ’n’.
Output
Print the reduced fraction (the greatest common divisor of the numerator and denominator is 1), representing the probability. If the answer is 0, you should output “0/1”.
Sample Input
4
avin
4
aaaa
Sample Output
1/256
0/1
统计每个字母的个数,然后相乘就好了,然后除以4*n,再求一波最大公因数
Code:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxx=100010;
int main()
{
int n;
char m[105];int a[5];
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
scanf("%s",m);
for(int i=0; i
Traffic
Problem Description
Avin is observing the cars at a crossroads. He finds that there are n cars running in the east-west direction with the i-th car passing the intersection at time ai . There are another m cars running in the north-south direction with the i-th car passing the intersection at time bi . If two cars passing the intersections at the same time, a traffic crash occurs. In order to achieve world peace and harmony, all the cars running in the north-south direction wait the same amount of integral time so that no two cars bump. You are asked the minimum waiting time.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 1, 000). The second line contains n distinct integers ai (1 ≤ ai ≤ 1, 000). The third line contains m distinct integers bi (1 ≤ bi ≤ 1, 000).
Output
Print a non-negative integer denoting the minimum waiting time.
Sample Input
1 1
1
1
1 2
2
1 3
Sample Output
1
0
读懂题意就出来了,一个模拟题。
要全部的b车往后等待1s。
Code:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxx=100010;
int main()
{
int n,m;
int a[1005],b[1005],x[4005];
while(cin>>n>>m)
{
memset(x,0,sizeof(x));
int sum=0;
for(int i=0; i>a[i];
x[a[i]]=1;
}
for(int i=0; i>b[i];
for(int j=0; j
Budget
Problem Description
Avin’s company has many ongoing projects with different budgets. His company records the budgets using numbers rounded to 3 digits after the decimal place. However, the company is updating the system and all budgets will be rounded to 2 digits after the decimal place. For example, 1.004 will be rounded down
to 1.00 while 1.995 will be rounded up to 2.00. Avin wants to know the difference of the total budget caused by the update.
Input
The first line contains an integer n (1 ≤ n ≤ 1, 000). The second line contains n decimals, and the i-th decimal ai (0 ≤ ai ≤ 1e18) represents the budget of the i -th project. All decimals are rounded to 3 digits.
Output
Print the difference rounded to 3 digits…
Sample Input
1
1.001
1
0.999
2
1.001 0.999
Sample Output
-0.001
0.001
0.000
因为题目说,小数最多3位然后精确到第二位小数,所以直接从最后开始算就好了。它的大小有1e18次,double不够用。
Code:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int n;
char s[1005];
scanf("%d",&n);
double x=0;
for(int i=0;i='5')
x+=10.0-(s[l-1]-'0');
else
x-=(s[l-1]-'0');
}
printf("%.3f\n",x/1000);
return 0;
}
Worker
Problem Description
Avin meets a rich customer today. He will earn 1 million dollars if he can solve a hard problem. There are n warehouses and m workers. Any worker in the i-th warehouse can handle ai orders per day. The customer wonders whether there exists one worker assignment method satisfying that every warehouse handles the same number of orders every day. Note that each worker should be assigned to exactly one warehouse and no worker is lazy when working.
Input
The first line contains two integers n (1 ≤ n ≤ 1, 000), m (1 ≤ m ≤ 1018). The second line contains n integers. The i-th integer ai (1 ≤ ai ≤ 10) represents one worker in the i-th warehouse can handle ai orders per day.
Output
If there is a feasible assignment method, print “Yes” in the first line. Then, in the second line, print n integers with the i-th integer representing the number of workers assigned to the i-th warehouse.
Otherwise, print “No” in one line. If there are multiple solutions, any solution is accepted.
Sample Input
2 6
1 2
2 5
1 2
Sample Output
Yes
4 2
No
求他们的最小公倍数,然后算出他们的基数,判断基数是不是小于总数m,和是不是m的倍数。
Code:
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn = 1005;
LL a[maxn];
LL gcd(LL x,LL y)
{
if(y==0)
return x;
else
gcd(y,x%y);
}
LL lcm(LL x,LL y)
{
return x*y/gcd(x,y);
}
int main(void)
{
LL n,m;
while(~scanf("%lld%lld",&n,&m))
{
LL gbs = 1,flag = 0;
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
if(flag==0&&gbs*n>m)
{
flag = 1;
}
else if(flag==0)
gbs = lcm(a[i],gbs);
// printf("%lld",gbs*n);
}
if(flag==1)
{
printf("No\n");
continue;
}
LL sum = 0;
for(int i=1; i<=n; i++)
{
a[i] = gbs/a[i];
sum+=a[i];
//printf("%d ",a[i]);
}
if(m%sum==0&&m>=sum)
{
printf("Yes\n");
sum = m/sum;
for(int i=1; i<=n; i++)
{
if(i==1)printf("%lld",a[i]*sum);
else printf(" %lld",a[i]*sum);
}
printf("\n");
}
else
{
printf("No\n");
}
}
return 0;
}
Class
Problem Description
Avin has two integers a, b (1 ≤ a, b ≤ 1, 000).
Given x = a + b and y = a - b, can you calculate a*b?
Input
The first line contains two integers x, y.
Output
Print the result of a*b.
Sample Input
4 2
Sample Output
3
Code:
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxx=100010;
int main()
{
int x,y;
cin>>x>>y;
int a=(x+y)/2;
int b=(x-y)/2;
printf("%d\n",a*b);
return 0;
}