YES
1. 守形数
分析:从后向前一位一位比较即可
#include <stdio.h>
int main(){
int n;
while(~scanf("%d",&n)){
int pown = n*n;
while(n){
//比较每一位
if(n%10 != pown%10) break;
n /= 10, pown /= 10;
}
if(n) printf("No!\n");
else printf("Yes!\n");
}
return 0;
}
2.遍历链表
#include <iostream>
#include <list>
using namespace std;
int main(){
list<int> al;
int n;
while(cin >> n){
int val;
for(int i=1;i<=n;i++){
cin >> val;
al.push_back(val); //Complexity:constant
}
al.sort(); //默认升序
cout << al.front();
al.pop_front();
while(!al.empty()){
cout << " " << al.front();
al.pop_front();
}
cout << '\n';
}
return 0;
}
3.成绩排序
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1005;
typedef struct{
char name[100];
int age;
int score;
}Stu;
int n;
Stu stu[N];
void Read() {
for (int i = 0; i < n; i++) {
scanf("%s%d%d",stu[i].name,&stu[i].age,&stu[i].score);
}
}
bool Cmp(Stu &s1, Stu &s2) {
if (s1.score < s2.score) return true;
else if(s1.score == s2.score){
if (strcmp(s1.name, s2.name) < 0) return true; //less
else if (strcmp(s1.name, s2.name) == 0) {
if (s1.age < s2.age) return true;
else return false;
}
else return false;
}
else return false;
}
void Solve() {
sort(stu,stu+n,Cmp);
for (int i = 0; i < n; i++) {
printf("%s %d %d\n",stu[i].name,stu[i].age,stu[i].score);
}
}
int main() {
while (~scanf("%d", &n)) {
Read();
Solve();
}
return 0;
}
//heap_sort
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N = 35;
typedef struct{
char name[11];
int age;
int employ_id;
}Worker;
int n;
vector<Worker> w;
void Read() {
Worker worker;
for (int i = 0; i < n; i++) {
scanf("%d%s%d",&worker.employ_id,worker.name,&worker.age);
w.push_back(worker);
}
}
bool Cmp(Worker &s1, Worker &s2) {
if (s1.age > s2.age) return true;
else if (s1.age == s2.age) {
if (s1.employ_id > s2.employ_id) return true;
else if (s1.employ_id == s2.employ_id) {
if (strcmp(s1.name, s2.name) > 0) return true;
else return false;
}
else return false;
}
else return false;
}
void Solve() {
make_heap(w.begin(),w.end(),Cmp); //建小顶堆,注意Cmp函数
for (int i = 0; i < min(n,3); i++) {
Worker _w = w.front();
printf("%d %s %d\n",_w.employ_id,_w.name,_w.age);
pop_heap(w.begin(), w.end(),Cmp); //调整堆
w.pop_back(); //删除原来的根结点
}
}
int main() {
while (~scanf("%d", &n)) {
Read();
Solve();
}
return 0;
}
5 . A+B
#include <stdio.h>
int a, b;
char s1[20],s2[20];
int GetN(char *s) {
int ret = 0;
char sign = '+';
for (int i = 0; s[i]!= '\0'; i++) {
if (s[i] == '-') sign = '-';
else if (s[i] == ',') continue;
else {
ret = ret * 10 + s[i] - '0';
}
}
if (sign == '-') ret *= -1;
return ret;
}
void Solve() {
int a = GetN(s1);
int b = GetN(s2);
printf("%d\n", a + b);
}
int main() {
while (~scanf("%s %s",s1,s2)){ //scanf:%s不能接收空格、换行
Solve();
}
return 0;
}
6 . 打印日期
#include <iostream>
#include <iomanip> //操作符
using namespace std;
class Date {
public:
Date(int y) :year(y), month(1), day(0) {} //初始化列表
bool LeapYear(int y) {
if ((y % 4 == 0 && y % 100) || y % 400 == 0)
return true;
else return false;
}
void Cal(int y, int n) {
if (LeapYear(y))
m[2] = 29; //修改为29天
for (int i = 1; i <= 12; i++) {
if (n > m[i]) {
month ++;
n -= m[i];
}
else {
day = n;
break;
}
}
}
void Print() {
//setw只对直接跟在<<后的输出数据起作用
cout << year << "-" << setw(2) << setfill('0') << month
<< "-" << setw(2) << setfill('0') << day << '\n';
}
private:
int year;
int month;
int day;
int m[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //按平年初始化
};
int main() {
int y, n;
while (cin >> y >> n) {
Date date(y);
date.Cal(y, n);
date.Print();
}
return 0;
}
7 . 大整数排序
//利用字符串比较来实现大整数排序
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105;
const int Len = 1005;
typedef struct {
char ch[Len];
}Num;
Num num[N];
int n;
void Read() {
for (int i = 0; i<n; i++)
cin >> num[i].ch;
}
bool Greater(Num &num1, Num &num2) {
if(strlen(num1.ch) == strlen(num2.ch))
return strcmp(num1.ch, num2.ch)<0;
return strlen(num1.ch) < strlen(num2.ch);
}
void Solve() {
sort(num, num + n, Greater);
for (int i = 0; i<n; i++)
cout << num[i].ch << '\n';
}
int main() {
while (cin >> n) {
Read();
Solve();
}
return 0;
}