1008
题目链接
Sample Input 1:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
Sample Output 1:
ERROR: aaa is not a legal number
ERROR: 9999 is not a legal number
ERROR: 2.3.4 is not a legal number
ERROR: 7.123 is not a legal number
The average of 3 numbers is 1.38
Sample Input 2:
2
aaa -9999
Sample Output 2:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined
Note1
- 题意:输入n个数,找到绝对值小于等于1000且最多两位小数的部分计算平均值
- 思路: 寻找’.’,之前的存到到整数部分,后面存到小数部分,最后修正输出两部分
- 问题: number的单复数被坑到了
- 改进,利用sscanf和sprintf效率更高
Code1
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
const int maxn = 1e4;
int main(){
#ifdef _DEBUG
ifstream cin("data2.txt");
#endif
int num;
int sum1 = 0,sum2 = 0, valid_cnt = 0;
cin >> num;
for(int i =0; i < num; i++){
string s;
int temp1 = 0, temp2 = 0, desined_flag = 0;
cin >>s;
int str = 0, flag = 0;
if(s[0] == '-'){ desined_flag =1; str++; if(s.size() == 1) flag = 1;}
while(s[str] >= '0' && s[str] <= '9'){
temp1 = 10 * temp1 + s[str] - '0';
str++;
}
if(str != s.size() && s[str] != '.' || abs(temp1)> 1000 || s.size() == 0) flag = 1;
if(s[str] == '.'){
int to_max_double_Cnt= 0;
str++;
while(str != s.size() ){
to_max_double_Cnt++;
if(s[str] < '0' || s[str] > '9'){flag = 1; break;}
if(to_max_double_Cnt > 2) {flag = 1; break;}
if(to_max_double_Cnt == 1)
temp2 = 10 *( s[str] - '0' );
else temp2 += s[str] - '0';
str++;
}
}
if(flag == 1) cout << "ERROR: "<< s << " is not a legal number" << endl;
else{
valid_cnt++;
if(desined_flag == 1) {sum1 -= temp1; sum2 -= temp2;}
else{ sum1 += temp1; sum2 += temp2;}
if(sum2 > 100){
sum2 -= 100;
sum1 += 1;
}
else if(sum2 < 0)
{
sum2 += 100;
sum1 -= 1;
}
}
}
double out = (sum1 + sum2 * 0.01) * 1.0 / valid_cnt;
if(valid_cnt == 0){
printf("The average of 0 numbers is Undefined" );
return 0;
}
printf("The average of %d numbers is %.2lf", valid_cnt, out);
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
code2
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<fstream>
using namespace std;
const int maxn = 1e4;
char a[50], b[50];
int main(){
#ifdef _DEBUG
ifstream cin("data2.txt");
#endif
int num, cnt = 0;
double temp, sum = 0.0;
cin >> num;
for(int i =0; i < num; i++){
string s;
cin >>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 || abs(temp) > 1000){
cout << "ERROR: "<< a << " is not a legal number" << endl;
continue;
}else{
sum += temp;
cnt++;
}
}
if(cnt == 1) printf("The average of %d number is %.2lf\n", cnt, sum / cnt);
else if(cnt >= 1) printf("The average of %d numbers is %.2lf\n", cnt, sum / cnt);
else printf("The average of 0 numbers is Undefined\n");
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
A1103 1109 Group Photo (25 分)
题目链接
Sample Input:
10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159
Sample Output:
Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John
Note1
-
题意,给定照片排队,高的在前矮的在后,高的在中间矮的在旁边(先左后右)
2.思路,把人分成line行,一行 (line == 0) ? num /row_num + num % row_num : num / row_num; 个人,从高往矮输入二维数组 -
问题: 注意数组开的大小,
Code2
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
const int maxn = 1e4;
struct node{
string name;
int height;
}a[maxn];
bool cmp1(node a, node b){
if(a. height == b.height) return a.name < b.name;
else return a.height > b.height;
}
int main(){
#ifdef _DEBUG
ifstream cin("data.txt");
#endif
int num, row_num;
cin >>num >> row_num;
for(int i = 0; i < num; i++){
cin >> a[i].name >> a[i].height;
}
sort(a, a+ num, cmp1);
int line = 0, all_cnt = 0;
string res[10][10000];
while(line < row_num){
int line_num = (line == 0) ? num /row_num + num % row_num : num / row_num;
int pos = line_num / 2 + 1;
int center = pos, step = 1;
for(int cnt = 0; cnt < line_num; ){
res[line][pos - 1] = a[all_cnt++].name;
cnt++;
if(cnt >= line_num) break;
if((cnt )% 2 == 1) pos = center - step ;
else {pos = center + step ; step++;}
}
line++;
}
for(int i = 0; i < line; i++){
cout << res[i][0];
int temp = (i == 0) ? num /row_num + num % row_num : num / row_num;
for(int j = 1 ; j < temp; j++){
cout << " "<< res[i][j];
}
cout << endl;
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
1110 Complete Binary Tree (25 分)
题目链接
Note
- 题意判断是否为完全二叉树
- 思路: 竟然想不到很好的办法,想用bfs查到 若队列不空的情况下有节点没孩子来排除不完全二叉树,但是对于最后一排的排列情况没有限制
- 问题:
a. 输入的节点数不一定是一位,不能 a - ‘0’
Code1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#include<queue>
using namespace std;
const int maxn = 1e3;
struct node{
int left;
int right;
}a[maxn];
int main(){
#ifdef _DEBUG
ifstream cin("data3.txt");
#endif
int num, m, n, tempvisited[maxn];
cin >> num;
for(int i = 0; i < num; i++){
char tempa, tempb;
cin >> tempa >> tempb;
if(tempa == '-') a[i].left = -1;
else {a[i].left= tempa - '0'; tempvisited[tempa - '0'] = 1;}
if(tempb == '-') a[i].right = -1;
else {a[i].right = tempb - '0';tempvisited[tempb - '0'] = 1;}
}
int start = 0, cnt = 0, result, realcnt = 0, flag = 0;
while(tempvisited[start] == 1) start ++;
queue<node> q;
node temp;
q.push(a[start]);
while(q.size() != 0 ){
temp = q.front();
q.pop();
realcnt++;
if(temp.left != -1){
q.push(a[temp.left]);
cnt++;
if(cnt == num - 1) result = temp.left;
if(flag == 1) {flag = 2; break;}
}
if(temp.right != -1){
q.push(a[temp.right]);
cnt++;
if(cnt == num - 1) result = temp.right;
if(flag == 1) {flag = 2; break;}
}
if((temp.right == -1 || temp.left == -1 )&& q.size() != 0 ){
flag = 1;
}
}
if(flag ==2)
cout << "No " << start;
else
cout << "YES " << result;
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
Note2
- 思路:bfs访问,一直计数,如果是-1直接break,若cnt == num输出result
Code2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#include<queue>
using namespace std;
const int maxn = 1e3;
struct node{
int left;
int right;
}a[maxn];
int main(){
#ifdef _DEBUG
ifstream cin("data3.txt");
#endif
int num, m, n, tempvisited[maxn];
cin >> num;
for(int i = 0; i < num; i++){
string tempa, tempb;
cin >> tempa >> tempb;
if(tempa == "-") a[i].left = -1;
else {a[i].left= stoi(tempa); tempvisited[stoi(tempa)] = 1;}
if(tempb == "-") a[i].right = -1;
else {a[i].right = stoi(tempb); tempvisited[stoi(tempb)] = 1;}
}
int start = 0, cnt = 0, result = 0, realcnt = 0, flag = 0;
while(tempvisited[start] == 1) start ++;
queue<int> q;
int temp;
q.push(start);
while(q.size() != 0 ){
temp = q.front();
q.pop();
if(temp != -1){
cnt++;
result = temp;
}else{
if(cnt != num)
printf("NO %d", start);
else
printf("YES %d", result);
return 0;
}
q.push(a[temp].left);
q.push(a[temp].right);
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
1111 Online Map (30 分)
题目链接
Sample Input 1:
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5
Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5
Sample Input 2:
7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5
Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5
Note1
- 找到时间最短的路和距离最短的路,距离最短可能多条,找经过结点最少的路
- 思路: 常规dijkstra, 码量很大,错误很多,要多敲几遍啊
- 错误; 很多很多。。
a.题目理解错误,距离相同输出耗时最短。时间相同的输出经过节点个数最少的,注意句号,不是逗号
b. 逻辑错误,Time是从源节点到该节点的最短时间,不一定是最短距离上的路径,因此距离相同时的耗时需要从新算
Code1
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
#define INF 1e9
const int maxn = 1e3;
int visited[maxn] = {0}, Time_visted[maxn] = {0}, Counst[maxn] = {0};
int Time[maxn] = {0}, dis[maxn] = {0}, path_Time[maxn], path_length[maxn] ;
int num, edge;
struct node{
int length;
int t;
} a[maxn][maxn];
int flag = {0};
void DijkstraDis(int n){
visited[n] = 1;
while(1){
int min = INF, ptr = 0;
for(int i = 0; i < num; i++){
if(visited[i] == 0 && dis[i] < min){
min = dis[i];
ptr = i;
}
}
if(min == INF) break;
visited[ptr] = 1;
for(int i = 0; i < num; i++){
if(visited[i] == 0 && dis[i] > dis[ptr] + a[ptr][i].length){
dis[i] = dis[ptr] + a[ptr][i].length;
Counst[i] = Counst[ptr] + 1 ;
path_length[i] = ptr;
}
else if(visited[i] == 0 && dis[i] == dis[ptr] + a[ptr][i].length ){
if(Time[ptr] + a[ptr][i].t < Time[i]){
Counst[i] = Counst[ptr] + 1;
path_length[i] = ptr;
}
else if(Time[ptr] + a[ptr][i].t == Time[i]){
if(Counst[i] > Counst[ptr] + 1);
Counst[i] = Counst[ptr] + 1;
path_length[i] = ptr;
}
}
}
}
}
void DjikstraTime(int n){
Time_visted[n] = 1;
while(1){
int min = INF, ptr = 0;
for(int i =0 ; i < num; i++){
if(Time_visted[i] == 0 && Time[i] < min){
ptr = i;
min = Time[i];
}
}
if(min == INF) break;
Time_visted[ptr] = 1;
for(int i = 0; i < num; i++){
if(Time_visted[i] == 0 && Time[ptr] + a[ptr][i].t < Time[i]){
Time[i] = Time[ptr] + a[ptr][i].t;
path_Time[i] = ptr;
}
}
}
}
int main(){
#ifdef _DEBUG
ifstream cin("data4.txt");
#endif
cin >> num >> edge;
fill(dis, dis + num + 1, INF);
fill(Time_visted, Time_visted + num + 1, 0);
fill(Time, Time + num + 1, INF);
fill(visited, visited + num + 1, 0);
for(int i = 0; i <= num; i++){
for(int j = 0; j <= num; j++){
a[i][j].length = INF;
a[i][j].t = INF;
}
}
for(int i = 0; i < edge; i++){
int aa, b, c,d, e;
cin >> aa >> b >> c >> d >> e;
a[aa][b].length = d;
a[aa][b].t = e;
if(c == 0){
a[b][aa].length = d;
a[b][aa].t = e;
}
}
int start, des;
cin >> start >> des;
path_length[start] = -1;
path_Time[start] = -1;
for(int i = 0; i < num; i++){
if(a[start][i].length < INF) {dis[i] = a[start][i].length; Counst[i] = 1; path_length[i] = start;}
if(a[start][i].t < INF){ Time[i] = a[start][i].t; path_Time[i] = start;}
}
DijkstraDis(start);
DjikstraTime(start);
int flag = 0;
int temp = des, cnt = 0;
while(path_length[temp] != -1 &&path_Time[temp] != -1 ){
if(path_length[temp] != path_Time[temp] ) { flag = 1; break;}
else temp = path_length[temp];
}
if(flag == 1){
cout << "Distance = " << dis[des] << ": " << start << " -> ";;
int temp = des, cnt = 0, resulta[maxn];
while(path_length[temp] != -1){
resulta[cnt++] = temp;
temp = path_length[temp];
}
cout << resulta[cnt - 1];
for(int i = cnt - 2; i >= 0; i--){
cout << " -> " << resulta[i];
}
cout << endl;
}
else cout << "Distance = " << dis[des] <<"; ";
cout << "Time = " << Time[des] << ": " << start << " -> ";
temp = des; cnt = 0;
int resuiltb[maxn];
while(path_Time[temp] != -1){
resuiltb[cnt++] = temp;
temp = path_Time[temp];
}
cout << resuiltb[cnt - 1];
for(int i = cnt - 2; i >= 0; i--){
cout << " -> " << resuiltb[i];
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
Code2
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
#define INF 1e9
const int maxn = 1e3;
int visited[maxn] = {0}, Time_visted[maxn] = {0}, Counst[maxn] = {0};
int Time[maxn] = {0}, dis[maxn] = {0}, path_Time[maxn], path_length[maxn], weight[maxn] ;
int num, edge, start, des;
vector <int> Time_path, Dis_path;
void DfsTime(int n){
Time_path.push_back(n);
if(n == start) return;
DfsTime(path_Time[n]);
}
void DfsPath(int n){
Dis_path.push_back(n);
if(n == start) return;
DfsPath(path_length[n]);
}
struct node{
int length;
int t;
} a[maxn][maxn];
int flag = {0};
void DijkstraDis(int n){
visited[n] = 1;
while(1){
int min = INF, ptr = 0;
for(int i = 0; i < num; i++){
if(visited[i] == 0 && dis[i] < min){
min = dis[i];
ptr = i;
}
}
if(min == INF) break;
visited[ptr] = 1;
for(int i = 0; i < num; i++){
if(visited[i] == 0 && dis[i] > dis[ptr] + a[ptr][i].length){
dis[i] = dis[ptr] + a[ptr][i].length;
path_length[i] = ptr;
weight[i] = weight[ptr] + a[ptr][i].t;
}
else if(visited[i] == 0 && dis[i] == dis[ptr] + a[ptr][i].length ){
if(weight[i] > weight[ptr] + a[ptr][i].t){
path_length[i] = ptr;
weight[i] = weight[ptr] + a[ptr][i].t;
}
}
}
}
}
void DjikstraTime(int n){
Time_visted[n] = 1;
while(1){
int min = INF, ptr = 0;
for(int i =0 ; i < num; i++){
if(Time_visted[i] == 0 && Time[i] < min){
ptr = i;
min = Time[i];
}
}
if(min == INF) break;
Time_visted[ptr] = 1;
for(int i = 0; i < num; i++){
if(Time_visted[i] == 0 && Time[ptr] + a[ptr][i].t < Time[i]){
Time[i] = Time[ptr] + a[ptr][i].t;
path_Time[i] = ptr;
Counst[i] = Counst[ptr] + 1;
}
else if(Time_visted[i] == 0 && Time[ptr] + a[ptr][i].t == Time[i]){
if((Counst[i] > Counst[ptr] + 1)){
Counst[i] = Counst[ptr] + 1;
path_Time[i] = ptr;
}
}
}
}
}
int main(){
#ifdef _DEBUG
ifstream cin("data4.txt");
#endif
cin >> num >> edge;
fill(dis, dis + num + 1, INF);
fill(Time_visted, Time_visted + num + 1, 0);
fill(Time, Time + num + 1, INF);
fill(visited, visited + num + 1, 0);
fill(Counst, Counst + num + 1, 0);
fill(weight, weight + num + 1, 0);
for(int i = 0; i <= num; i++){
for(int j = 0; j <= num; j++){
a[i][j].length = INF;
a[i][j].t = INF;
}
}
for(int i = 0; i < edge; i++){
int aa, b, c,d, e;
cin >> aa >> b >> c >> d >> e;
a[aa][b].length = d;
a[aa][b].t = e;
if(c == 0){
a[b][aa].length = d;
a[b][aa].t = e;
}
}
cin >> start >> des;
path_length[start] = -1;
path_Time[start] = -1;
for(int i = 0; i < num; i++){
if(a[start][i].length < INF) {dis[i] = a[start][i].length; Counst[i] = 1; path_length[i] = start;}
if(a[start][i].t < INF){ Time[i] = a[start][i].t; path_Time[i] = start; weight[i] = a[start][i].t;}
}
DjikstraTime(start);
DijkstraDis(start);
int flag = 0;
int temp = des, cnt = 0;
DfsPath(des);
DfsTime(des);
if(Dis_path == Time_path ) flag = 1;
if(flag == 0){
cout << "Distance = " << dis[des] << ": ";
for(int i = Dis_path.size() - 1; i >= 0; i--) {
cout << Dis_path[i];
if(i != 0) cout << " -> ";
}
cout << endl;
}
else cout << "Distance = " << dis[des] <<"; ";
cout << "Time = " << Time[des] << ": " ;
for(int i = Time_path.size() - 1; i >= 0; i--) {
cout <<Time_path[i];
if(i != 0) cout << " -> ";
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}