關於一些初級ACM競賽題目的分析和題解(七)
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for bkilos (You don't need to care about what "yuan" is), the same as a / b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10 - 6.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if .
3 5
1 2
3 4
1 3
1.66666667
2 1
99 100
98 99
0.98989899
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5 / 3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98 / 99yuan.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int n;
double c,a,b,d,m; // 定義含浮點的變量
cin>>n>>m; // 輸入n,m
d=1000; // 先整一個比較大的數
while(n--)
{cin>>a>>b;
c=a/b;
if(c<=d)d=c;
}
printf("%.8lf",d*m); // 輸出小數點後八位的結果
}
若有小數出現,就在定義變量時用double,而輸出時根據小數的位數若要8位就printf("%.8lf",a); %.nlf表示n位小數,Vasya has recently learned to type and log on to the Internet. He immediately entered a chat room and decided to say hello to everybody. Vasya typed the word s. It is considered that Vasya managed to say hello if several letters can be deleted from the typed word so that it resulted in the word "hello". For example, if Vasya types the word "ahhellllloou", it will be considered that he said hello, and if he types "hlelo", it will be considered that Vasya got misunderstood and he didn't manage to say hello. Determine whether Vasya managed to say hello by the given word s.
The first and only line contains the word s, which Vasya typed. This word consisits of small Latin letters, its length is no less that 1 and no more than 100 letters.
If Vasya managed to say hello, print "YES", otherwise print "NO".
ahhellllloou
YES
hlelo
NO
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int j,l,c;
char a[1000],b[5]; // 定義字符串
b[0]='h'; // 字符串賦值
b[1]='e';
b[2]='l';
b[3]='l';
b[4]='o';
gets (a); // 輸入字符串
j=0;
l=strlen(a); // 找到字符串長度
for (int i=0;i<=l-1;i++)
{if (a[i]==b[j])
j++; // 計數
if(j==5)
return 0*printf("YES");
}
if(j!=5) printf("NO"); //輸出
}
注意定義字符串時,如b[5]表示可容納五個字符,即讀不到b[5],辣雞monster_ayb定義的是b[4]結果老是出錯,多虧cy1999巨巨指出才發現,感謝cy1999