题目中: 说出p 个连续的 “CocaCola”,最小的数是多少。 ( 1 <= p <= 99)
分类讨论:
当p == 1 时,输出 7;
当p == 2时,输出 27;
当 3 <= p <= 10 时,输出 70;
当 p == 11 时,输出 270
其余的,输出700;
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 100010;
typedef long long ll ;
int main(){
int t;
scanf("%d",&t);
while(t--){
int p;
scanf("%d",&p);
if (p == 1) puts("7");
else if (p == 2) puts("27");
else if (p >= 3 && p <= 10) puts("70");
else if (p == 11) puts("270");
else puts("700");
}
return 0;
}
Kruskal 算法 (最小生成树)
实际感觉是并查集的升级版,多些思维。
我们输入 e 条边 ,我们对这些边进行排序,从小到大排序(因为要求最小的和),然后进行一条边两点之间的连接(因为开始的最短,所以先链接好,后面的判断是否之前已经存在即可,如果存在连通,那么就不存),最后输出值即可。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 300;
typedef long long ll ;
char mp[N][N];
struct solve {
int x;
int y;
int num;
}ans[250000];
bool cmp(solve a,solve b){
return a.num < b.num;
}
int p[510];
int Find(int x){ // 路径压缩
if (p[x] != x ) p[x] = Find(p[x]);
return p[x];
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,e;
scanf("%d%d",&n,&e);
for (int i = 0; i < e; i++){
scanf("%d%d%d",&ans[i].x,&ans[i].y,&ans[i].num);
}
sort(ans,ans + e,cmp);
int res = 0;
for (int i = 0; i < n; i++){ // 并查集初始化 init
p[i] = i;
}
for (int i = 0; i < e;i ++){
if (Find(ans[i].x) != Find(ans[i].y)){ // 如果不是连通的,那么就 +
res += ans[i].num;
p[Find(ans[i].x)] = Find(ans[i].y); // 让他们属于一个集合
}
}
printf("%d\n",res);
}
return 0;
}
E题 解题思路:
就是求完式子的导数的系数,认真一点就OK,注意题目要求除了常数位,其他都要输出(好好读题!)
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 300;
typedef long long ll ;
int a[105];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for (int i = 0; i <= n; i++){
scanf("%d",&a[i]);
}
if (!n) {
printf("0\n");
continue;
}
for (int i = 0; i < n ;i ++){
if (i == n - 1){
printf("%d\n",a[i] * (n - i));
}
else{
printf("%d ",a[i] * (n - i));
}
}
}
return 0;
}
水题,根据不同的指标求最小最大值,输出即可
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 100010;
typedef long long ll ;
int a[N] , cnt[N];
bool p[N];
int main(){
int t;
scanf("%d",&t);
while(t--){
string st;
cin>>st;
int n;
scanf("%d",&n);
int mx = -1e9;
int mi = 1e9;
for (int i = 0; i < n; i++){
int k ;
scanf("%d",&k);
mi = min(mi,k);
mx = max(mx,k);
}
if (st == "Faster") printf("%d\n",mi);
else printf("%d\n",mx);
}
return 0;
}
给予英文字母,然后求出他的数字形式(感觉考的是输入),将 0 —— 19存入 , 20 ,30 …90,然后遇到million 就 * 1000000,遇到 thousand 就 * 1000,然后排着遍历即可。
#include <map>
#include <cmath>
#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 100010;
typedef long long ll ;
map<string,int> num;
void init(){
string a[25]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
string b[15]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
for(int i=0;i<20;i++){
num[a[i]]=i;
}
int j=0;
for(int i=20;i<=90;i+=10){
num[b[j]]=i;
j++;
}
}
int ans[2];
int main(){
int t;
init(); // 存入 map 中
scanf("%d",&t);
while(t--){
int res = 0;
string st;
ans[0] = 0; // 记得初始化
ans[1] = 0;
while(cin>>st){
char c = getchar();
if (st == "million"){
res = res * 1000000;
ans[0] = res;
res = 0;
}
else if (st == "thousand"){
res = res * 1000;
ans[1] = res;
res = 0;
}
else if (st == "hundred"){
res = res * 100;
}
else if (num.count(st)){
res += num[st];
}
if (c == '\n') break;
}
cout<<ans[0] + ans[1] + res<<endl;
}
return 0;
}
K题 解题思路:
首先是枚举点,四重for循环肯定超时超的爽,然后用组合排列,求每两行,这是n^2 ,然后对每行枚举进行计算有多少对相同的,然后利用公式 n * (n - 1)/ 2,然后最后把总和相加即可。
#include <map>
#include <cmath>
#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 300;
typedef long long ll ;
char a[N][N];
int ans[10];
int main(){
int t ;
scanf("%d",&t);
while(t--){
int n,m;
int res = 0;
scanf("%d%d",&n,&m);
for (int i = 0; i < n; i++){
scanf("%s",a[i]);
}
for (int i = 0; i < n; i++){
for (int j = i + 1;j < n; j++){
memset(ans,0,sizeof ans);
for (int k = 0; k < m; k++){
if (a[i][k] == a[j][k]){
if (a[i][k] == 'B') ans[0]++;
else if (a[i][k] == 'J') ans[1] ++;
else if (a[i][k] == 'H') ans[2] ++;
else if (a[i][k] == 'Y') ans[3] ++;
else ans[4] ++;
}
}
for (int i = 0 ; i < 5; i++){
res += ans[i] * (ans[i] - 1) / 2;
}
}
}
printf("%d\n",res);
}
return 0;
}
(感觉这些题目怎么都这么长)
给予 n 个灯,给出他的坐标(x, y , z)和亮度 l ,然后他给我们公式 E = I /(R * R) * cosa ,R就是当前灯的坐标与当前所选择的地区的距离,cosa = z / R ,因此我们带入计算即可,题目中所说,x,y 的绝对值都不会 大于 100 ,因此我们从 -100 到 100 遍历即可,因此200 * 200 * n ,最高的复杂度4e6,因此暴力是OK的,因此我们循环暴力每次选出最大值即可。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 300;
typedef long long ll ;
struct solve {
int x,y,z,l;
}a[105];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for (int i = 0; i < n; i++){
scanf("%d%d%d%d",&a[i].x ,&a[i].y ,&a[i].z , &a[i].l);
}
double mx = -1;
for (int i = -100 ; i <= 100; i++){
for (int j = -100; j <= 100; j++){
double res = 0;
for (int k = 0; k < n ; k++){
double x = (a[k].x - i);
double y = (a[k].y - j);
double z = a[k].z;
double r = x * x + y * y + z * z ;
double cos = a[k].z * 1.0 / sqrt(r);
res += a[k].l * 1.0 / r * cos;
}
mx = max(mx,res);
}
}
printf("%.2f\n",mx);
}
return 0;
}