目录
1108 Finding Average (20分) (sscanf sprintf)
1109 Group Photo (25分)
1112 Stucked Keyboard (20分) (map set)
1113 Integer Set Partition (25分) (水题)
1114 Family Property (25分) (并查集)
1116 Come on! Let's C (20分) (水题)
1117 Eddington Number (25分)(水题)
1118 Birds in Forest (25分)(并查集)
#include
#include
#include
#include
#include
#include
#include
#include
#include
正解:
#include
#include
#include
using namespace std;
int main() {
int n, cnt = 0;
char a[50], b[50];
double temp, sum = 0.0;
cin >> n;
for(int i = 0; i < n; i++) {
scanf("%s", a);
sscanf(a, "%lf", &temp);
sprintf(b, "%.2f",temp);
int flag = 0;
for(int j = 0; j < strlen(a); j++) if(a[j] != b[j]) flag = 1;
if(flag || temp < -1000 || temp > 1000) {
printf("ERROR: %s is not a legal number\n", a);
continue;
} else {
sum += temp;
cnt++;
}
}
if(cnt == 1)
printf("The average of 1 number is %.2f", sum);
else if(cnt > 1)
printf("The average of %d numbers is %.2f", cnt, sum / cnt);
else
printf("The average of 0 numbers is Undefined");
return 0;
}
题意:输出拍照的方阵队形(k行)
思路:先利用sort对所有人按身高递减排序,然后一行一行的输出,最后一行人数=n-(n/k)*(k-1) ;其他行人数=n/k;
每行最高的在中间m/2位置,然后,按照“方阵中每行最高的人”的右->左,给人排队
#include
#include
#include
#include
#include
#include
#include
#include
#include
18分待改
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int n,sum,sum1;
int num[maxn];
int main(){
scanf("%d",&n);
for(int i=0;i
本题很有借鉴意义的
注意点:union规则: father[大id]=小id
void Union(int a, int b) {
int faA = find(a);
int faB = find(b);
if(faA > faB)
father[faA] = faB;
else if(faA < faB)
father[faB] = faA;
}
#include
#include
using namespace std;
struct DATA {
int id, fid, mid, num, area;
int cid[10];
}data[1005];
struct node {
int id, people;
double num, area; //家庭成员的最⼩编号 家庭⼈⼝数 ⼈均房产套数 ⼈均房产⾯积
bool flag = false;
}ans[10000];
int father[10000];
bool visit[10000];
int find(int x) {
return x==father[x]?x:father[x]=find(father[x]);
}
void Union(int a, int b) {
int faA = find(a);
int faB = find(b);
if(faA > faB)
father[faA] = faB;
else if(faA < faB)
father[faB] = faA;
}
int cmp1(node a, node b) {
if(a.area != b.area)
return a.area > b.area;
else
return a.id < b.id;
}
int main() {
int n, k, cnt = 0;
scanf("%d", &n);
for(int i = 0; i < 10000; i++)
father[i] = i;
for(int i = 0; i < n; i++) {
scanf("%d %d %d %d", &data[i].id, &data[i].fid, &data[i].mid, &k);
visit[data[i].id] = true;
if(data[i].fid != -1) {
visit[data[i].fid] = true;
Union(data[i].fid, data[i].id);
}
if(data[i].mid != -1) {
visit[data[i].mid] = true;
Union(data[i].mid, data[i].id);
}
for(int j = 0; j < k; j++) {
scanf("%d", &data[i].cid[j]);
visit[data[i].cid[j]] = true;
Union(data[i].cid[j], data[i].id);
}
scanf("%d %d", &data[i].num, &data[i].area);
} //构造并查集
for(int i = 0; i < n; i++) {
int id = find(data[i].id); //每个cluster的最小id是这个cluster的father
ans[id].id = id;
ans[id].num += data[i].num;
ans[id].area += data[i].area;
ans[id].flag = true;
}
for(int i = 0; i < 10000; i++) {
if(visit[i])
ans[find(i)].people++;
if(ans[i].flag)
cnt++; //cluster数
}
for(int i = 0; i < 10000; i++) {
if(ans[i].flag) {
ans[i].num = (double)(ans[i].num * 1.0 / ans[i].people);
ans[i].area = (double)(ans[i].area * 1.0 / ans[i].people);
}
}
sort(ans, ans + 10000, cmp1);
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++)
printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people, ans[i].num,ans[i].area);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
"Eddington number", E -- that is, the maximum integer E such that it is for E days that one rides more than E miles.
Sample Input:
10
6 7 6 9 3 10 8 2 7 8
Sample Output:
6
一连4天,每次骑行超过6 miles
本题重点在于题意理解:在数组a中存储n天的公⾥数,对n个数据从⼤到⼩排序,i表示了骑⻋的天数,那么满⾜a[i] > i+1
#include
#include
using namespace std;
int a[1000000];
int main() {
int n, e = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a+n, greater());
while(e < n && a[e] > e+1) e++;
printf("%d", e);
return 0;
}
#include
using namespace std;
int n, m, k;
const int maxn = 10010;
int fa[maxn] = {0}, cnt[maxn] = {0};
int findFather(int x) {
return x==fa[x]?x:fa[x]=findFather(fa[x]);
}
void Union(int a, int b) {
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB) fa[faA] = faB;
}
bool exist[maxn];
int main() {
scanf("%d", &n);
for(int i = 1; i <= maxn; i++)
fa[i] = i;
int id, temp;
for(int i = 0; i < n; i++) {
scanf("%d%d", &k, &id);
exist[id] = true;
for(int j = 0; j < k-1; j++) {
scanf("%d", &temp);
Union(id, temp);
exist[temp] = true;
}
}
for(int i = 1; i <= maxn; i++) {
if(exist[i] == true) {
int root = findFather(i);
cnt[root]++;
}
}
int numTrees = 0, numBirds = 0;
for(int i = 1; i <= maxn; i++) {
if(exist[i] == true && cnt[i] != 0) {
numTrees++;
numBirds += cnt[i];
}
}
printf("%d %d\n", numTrees, numBirds);
scanf("%d", &m);
int ida, idb;
for(int i = 0; i < m; i++) {
scanf("%d%d", &ida, &idb);
printf("%s\n", (findFather(ida) == findFather(idb)) ? "Yes" : "No");
}
return 0;
}