第一题是打印日期,拿下。
#include <stdio.h>
using namespace std;
void Nextday(int &year, int &month, int &day){int dayofmonth[] = {0, 31, 28, 31, 30,31,30,31,31,30,31,30,31};if(year%400 == 0||year%4==0&&year%100!=0){dayofmonth[2] = 29;}day++;if(day>dayofmonth[month]){day = 1;month++;}if(month>12){month = 1;year++;}
}
int main(){int year, daynum, month, day;while(scanf("%d%d", &year, &daynum)!=EOF){month = 1;day =0;for(int i = 0; i<daynum;i++){Nextday(year, month, day);}printf("%04d-%02d-%02d\n", year, month, day);}
}
第二题是日期。
#include <stdio.h>
using namespace std;
void Nextday(int &month, int &day){int dayofmonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};day++;if(day>dayofmonth[month]){month++;day = 1;}
}
int main(){int month, day;scanf("%d%d", &month, &day);int i = 3;int month1 = 4;int day1 = 12;while(day1!=day||month1!=month){Nextday(month1, day1);i = (i+1)%7;}if(i == 0) printf("Monday\n");else if(i == 1) printf("Tuesday\n");else if(i == 2) printf("Wednesday\n");else if(i == 3) printf("Thursday\n");else if(i == 4) printf("Friday\n");else if(i == 5) printf("Saturday\n");else printf("Sunday\n");}
下面进入动态数组vector的学习与使用,从增删查改4个角度理解vector,学习迭代器iterator用法。
#include <stdio.h>
#include <vector>//vector不需要.h后缀
using namespace std;
struct MyType{int val1;double val2;
};//自定义类
int main(){//初始化 构造 vector<int> vec1;//vector不是类型 vector<type>才是类型//vector<double> vec2;//vector<MyType> vec3;//vector<vector<int>> vec4;// 二维动态数组,动态数组的动态数组//vector<int> arr[10];//机试推荐,动态数组的静态数组 图算法 邻接表vector<int> vec2(100);//vec2一开始就有100个元素//push_back 往动态数组的尾部插入车辆// int a;
// while(scanf("%d", &a) != EOF){
// vec1.push_back(a);
// }//查找vector<int> vec3={1, 3, 5,7,9};
// int i = 0;
// printf("%d", vec3[i]);
// int size = vec3.size();
// for(int i = 0; i < vec3.size();i++){
// printf("%d\n", vec3[i]);
// }//迭代器
// vector<int>::iterator it;
// for(it = vec3.begin();it!=vec3.end();it++){
// printf("*it=%d", *it);
// }
// vector<int>::iterator it;
// it = vec3.begin();
// vec3.insert(it, 2);//插入
// for(it = vec3.begin(); it != vec3.end();it++){
// printf("%d\n", *it);
// } //迭代器可以获取元素的位置//insert会修改动态数组的结构,插入完成后it的指向无意义,重新赋值it//it++只能在vector中使用//删除//vec3.clear();//vec3.pop_back();//删除最后元素vector<int>::iterator it;it = vec3.begin() + 3;vec3.erase(it);}
然后学习list用法,与vector完全一样。
第三题是完数与盈数,拿下。
#include <stdio.h>
#include <vector>
using namespace std;;
int main() {vector<int> vec2;//完数数组vector<int> vec3;//盈数数组int num;for (num = 2; num < 61; num++) {int sum = 0;for (int i = 1; i < num; i++) {if (num % i == 0) {sum += i;}}if (sum == num) vec2.push_back(num);if (sum > num) vec3.push_back(num);}printf("E:");for (int i = 0; i < vec2.size(); i++) {printf(" %d", vec2[i]);}printf("\n");printf("G:");for (int i = 0; i < vec3.size(); i++) {printf(" %d", vec3[i]);}
}
第四题是剩下的树。
#include <stdio.h>
using namespace std;
#include <vector>
int main() {int l, m;vector<int> tree;scanf("%d%d", &l, &m);for (int i = 0; i < (l + 1); i++) {tree.push_back(1);}for (int i = 0; i < m; i++) { //m组整数int left, right;scanf("%d%d", &left, &right);for (int j = left; j <= right; j++) {tree[j] = 0;}}int result = 0;for (int i = 0; i < tree.size(); i++) {if (tree[i] == 1) result++;}printf("%d", result);
}
第五题是糖果分享问题,vector初试化想要访问数组下标的时候一定要提前分配内存,如vector<int> vec(N),若不分配就用push_back。
#include <stdio.h>
#include <vector>
using namespace std;
bool issame(vector<int> vec){//判断糖果数量是否相同for(int i = 0; i < (vec.size()-1);i++){if(vec[i]!=vec[i+1]) return false;}return true;
}
void exchange(vector<int>&candy){//传递糖果vector<int> cost(candy.size());for(int i =0; i<candy.size();i++){cost[i] = candy[i]/2;//所有人拿出来一半的糖果}for(int i = 0;i < candy.size();i++){candy[i] -= cost[i];candy[(i+1)%candy.size()] += cost[i];}//传递糖果结束//奇数数量的糖果加一for(int i = 0; i < candy.size();i++){if(candy[i]%2==1) candy[i]++;}}
int main(){int N;while(scanf("%d", &N)!=EOF){if(N==0) break;vector<int> candy(N);for(int i = 0;i < N;i++){scanf("%d", &candy[i]);//读入每个学生的糖果数量}int round = 0;while(issame(candy)==false){exchange(candy);round++;}printf("%d %d\n",round, candy[0]);}
}
第六题是一端进,两端出。不会,看答案了。
#include <stdio.h>#include <list>#include <vecctor>using namespace std;int main() {int n, k;scanf("%d%d", &n, &k);vector<int> insertVec(n);for (int i = 0; i < n; ++i) {scanf("%d", &insertVec[i]);}for (int i = 0; i < k; ++i) {vector<int> outputVec(n);for (int j = 0; j < n; ++j) {scanf("%d", &outputVec[j]);}bool flag = true;list<int> myQueue;int j = 0, k = 0;for (int j = 0; j < n; ++j) {myQueue.push_back(insertVec[j]);while (!myQueue.empty() && (myQueue.front() == outputVec[k] || myQueue.back() == outputVec[k] )) {if (myQueue.front() == outputVec[k]) {myQueue.pop_front();}else {myQueue.pop_back();}++k;}}if (myQueue.empty()) {printf("yes\n");}else {printf("no\n");}}return 0;}
第七题是合并链表,简单。
#include <stdio.h>
#include <vector>
using namespace std;
int main(){int S1,S2;scanf("%d", &S1);vector<int> vec1;vector<int> vec2;for(int i =0;i< S1;i++){int elm;scanf("%d", &elm);vec1.push_back(elm);}scanf("%d", &S2);for(int i = 0; i < S2;i++){int elm;scanf("%d", &elm);vec2.push_back(elm);}vector<int> vec3;int i ,j=0;while(i < S1&&j < S2){if(vec1[i]<=vec2[j]) {vec3.push_back(vec1[i]);i++;}else {vec3.push_back(vec2[j]);j++;}}while(i<S1){vec3.push_back(vec1[i]);i++;}while(j<S2){vec3.push_back(vec2[j]);j++;}for(int i = 0; i < vec3.size();i++){printf("%d ", vec3[i]);}
}