因为51cto不允许博文超过8万字符。我将把这些练习放到接下来的博客中,但是它们其实是连着的。
- #pragma warning(disable:4786)
- #include <iostream>
- #include <sstream>
- #include <set>
- #include <algorithm>
- #include <map>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <deque>
- #include <IOMANIP>
- #include <stack>
- #include <list>
- using namespace std;
- #define is_year(year) ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) ? 1 : 0
- int month_day[13][2] = {
- {0,0},
- {31,31},
- {28,29},
- {31,31},
- {30,30},
- {31,31},
- {30,30},
- {31,31},
- {31,31},
- {30,30},
- {31,31},
- {30,30},
- {31,31}
- };
- class date
- {
- public:
- int year;
- int month;
- int day;
- date(){};
- date(int year,int month,int day);
- void display();
- void nextday();
- };
- date::date(int year,int month,int day)
- {
- this->year = year;
- this->month = month;
- this->day = day;
- }
- void date::display()
- {
- cout<<this->year<<'-'<<this->month<<'-'<<this->day<<endl;
- }
- void date::nextday()
- {
- ++day;
- if(month_day[month][is_year(year)] < day)
- {
- day = 1;
- ++month;
- if(month > 12)
- {
- month = 1;
- year++;
- }
- }
- }
- int days[5001][13][32];
- void main41()
- {
- date temp(0,1,1);
- int day_count = 0;
- while(temp.year < 5001)
- {
- days[temp.year][temp.month][temp.day] = day_count++;
- temp.nextday();
- // cout<<temp.year<<' '<<temp.month<<' '<<temp.day<<' '<<day_count<<endl;
- }
- int y,m,d;
- while(cin>>y>>m>>d)
- {
- cout<<days[y][m][d]<<endl;
- }
- }
- int qixi[50001];
- void main44()
- {
- int i,j,num;
- for(i=0; i<=50000; ++i)
- qixi[i] = 1;
- for(i=2; i<=50000; ++i)
- for(j=2; i*j <= 50000; ++j)
- qixi[i*j] += i;
- while(cin>>num)
- {
- cout<<qixi[num]<<endl;
- }
- }
- //因子筛选
- /*for(int i=0; i<=500000; ++i)
- qixi[i] = 1;
- for(int i=2; i<=500000; ++i)
- for(int j=2; j*i<=500000; ++j)
- qixi[i*j] += i;
- */
- //素数筛选
- bool bPrime[1001]; //必须得单独赋值
- void main45()
- {
- int i,j;
- bPrime[0] = bPrime[1] = false;
- for(i=2; i<=1000; ++i) bPrime[i] = true;
- for(i=2; i<=1000; ++i)
- {
- if(bPrime[i])
- {
- for(j=i*i; j<=1000; j+=i)
- {
- // cout<<j<<endl;
- bPrime[j] = false;
- }
- }
- }
- int num;
- while(cin>>num)
- {
- if(bPrime[num]) cout<<"yes"<<endl;
- else cout<<"no"<<endl;
- }
- }
- void main46()
- {
- char ch[10];
- cin.get(ch,10);
- cout<<ch;
- getchar();
- cin.get(ch,10);
- cout<<ch;
- }
- void main47()
- {
- string s("-122");
- istringstream iss(s); //使用istringstream可以对输入进行格式化输入,不管是整数还是负数
- int a;
- iss>>a;
- cout<<a;
- vector<int> v;
- v.push_back(3);
- v.push_back(4);
- }
- #include <algorithm>//必须要有这个头文件。。。。。
- int main48(int argc, char*argv[])
- {
- std::string str = "song";
- reverse(str.begin(), str.end());
- return 0;
- }
- void main49()
- {
- char aa[100];
- int a;
- /*
- while(cin>>a)
- {
- itoa(a,aa,10);
- cout<<aa<<endl;
- memset(aa,'0',sizeof(aa));
- }
- */
- while(cin>>hex>>a)
- {
- sprintf(aa,"%b",a);
- cout<<aa<<endl;
- }
- }
- void main50()
- {
- int a,sum = 0,max = -100;
- while(cin>>a && a != -1000)
- {
- sum += a;
- if(sum > max)
- max = sum;
- if(sum < 0)
- sum = 0;
- }
- cout<<max<<endl;
- }
- int gcd(int a,int b)
- {
- if(b == 0)
- return a;
- else return gcd(b,a%b);
- }
- void main51()
- {
- int a,b;
- while(cin>>a>>b)
- {
- cout<<gcd(a,b)<<endl;
- }
- }
- //求N!阶乘中乘5的次数
- void main52()
- {
- int cnt = 0,n;
- cin>>n;
- for(int i=1; i<=n; ++i)
- {
- int j = i;
- while(j % 5 == 0)
- {
- ++cnt;
- j /= 5;
- }
- }
- cout<<cnt<<endl;
- }
- void main53()
- {
- int cnt=0;
- int n;
- cin>>n;
- while(n)
- {
- cnt += n/5;
- n /= 5;
- }
- cout<<cnt<<endl;
- }
- //并查集的操作
- #define N 1000
- int tree[N];
- int findRoot(int x)
- {
- int ret = x;
- if(tree[x] != -1)
- {
- ret = findRoot(tree[x]);
- tree[x] = ret;
- }
- return ret;
- }
- void main54()
- {
- int n,m;
- while(cin>>n && n != 0)
- {
- cin>>m;
- for(int i=0; i<N; ++i) tree[i] = -1;
- while(m--)
- {
- int a,b;
- cin>>a>>b;
- a = findRoot(a);
- b = findRoot(b);
- if(a != b)
- tree[a] = b;
- }
- int ans = 0;
- for(i=1; i<=n; ++i)
- if(tree[i] == -1) ++ans;
- cout<<--ans<<endl;
- }
- }
- int sum[N]; //只有当tree[i]中的值为真时候才有效
- //涉及到求并查集中的元素个数
- void main55()
- {
- int n;
- while(cin>>n)
- {
- for(int i=0; i<N; ++i)
- {
- tree[i] = -1;
- sum[i] = 1;
- }
- while(n--)
- {
- int a,b;
- cin>>a>>b;
- a = findRoot(a);
- b = findRoot(b);
- if(a != b)
- {
- tree[a] = b;
- sum[b] += sum[a];
- }
- }
- int ans = 0;
- for(i=1; i<=N; ++i)
- {
- if(tree[i] == -1 && sum[i] > ans)
- ans = sum[i];
- }
- cout<<ans<<endl;
- }
- }
- struct Edge1
- {
- int a,b,cost;
- bool operator < (struct Edge1 e)
- {
- return cost < e.cost;
- }
- }edge[N];
- void main56()
- {
- int n;
- while(cin>>n)
- {
- for(int i=1; i<=n*(n-1)/2; ++i)
- cin>>edge[i].a>>edge[i].b>>edge[i].cost;
- sort(edge+1,edge+1+n*(n-1)/2);
- for(i=0; i<N; ++i)
- tree[i] = -1;
- int ans = 0;
- for(i=1; i<=n*(n-1)/2; ++i)
- {
- int a = edge[i].a;
- int b = edge[i].b;
- a = findRoot(a);
- b = findRoot(b);
- if(a != b)
- {
- tree[a] = b;
- ans += edge[i].cost;
- }
- }
- cout<<ans<<endl;
- }
- }
- struct Edge2
- {
- int a,b;
- double cost;
- bool operator < (const Edge2 &e) const
- {
- return cost < e.cost;
- }
- }edge2[N];
- struct Point
- {
- double x,y;
- double getDistance(Point p)
- {
- return sqrt(pow(abs(x-p.x),2)+pow(abs(y-p.y),2));
- }
- }list1[N];
- void main57()
- {
- int n;
- while(cin>>n)
- {
- for(int i=1; i<=n; ++i)
- cin>>list1[i].x>>list1[i].y;
- int size = 0;
- for(i=1; i<=n; ++i)
- {
- for(int j=i+1; j<=n; ++j)
- {
- edge2[size].a = i;
- edge2[size].b = j;
- edge2[size].cost = list1[i].getDistance(list1[j]);
- ++size;
- }
- }
- sort(edge2+1,edge2+size);
- for(i=0; i<=N; ++i)
- tree[i] = -1;
- double ans = 0;
- for(i=0; i<size; ++i)
- {
- int a = findRoot(edge2[i].a);
- int b = findRoot(edge2[i].b);
- if(a != b)
- {
- tree[a] = b;
- ans += edge2[i].cost;
- }
- }
- printf("%.2lf\n",ans);
- // cout<<setprecision(2)<<ans<<endl;
- }
- }
- int ans[N][N]; //利用一个矩阵保存图,边达不到则为-1
- void main58()
- {
- int n,m;
- while(cin>>n>>m && n && m)
- {
- for(int i=1; i<=n; ++i)
- {
- for(int j=1; j<=n; ++j)
- ans[i][j] = -1;
- ans[i][i] = 0;
- }
- while(m--)
- {
- int a,b,c;
- cin>>a>>b>>c;
- ans[a][b] = ans[b][a] = c; //此式中为无向图
- }
- for(int k=1; k<=n; ++k)
- {
- for(i=1; i<=n; ++i)
- for(int j=1; j<=n; ++j)
- if(ans[i][k]==-1 || ans[k][j] == -1)
- continue;
- else if(ans[i][j] == -1 || ans[i][k] + ans[k][j] < ans[i][j])
- ans[i][j] = ans[i][k] + ans[k][j];
- }
- cout<<ans[1][n]<<endl;
- }
- }
- struct E
- {
- int node;
- int cost;
- };
- vector<E> vEdge[N];
- bool mark[N];
- int dist[N];
- void main59()
- {
- int n,m;
- while(cin>>n>>m && n && m)
- {
- for(int i=0; i<N; ++i) vEdge[i].clear();
- while(m--)
- {
- int a,b,c;
- cin>>a>>b>>c;
- E tmp;
- tmp.cost = c;
- tmp.node = b;
- vEdge[a].push_back(tmp);
- tmp.node = a;
- vEdge[b].push_back(tmp);
- }
- for(i=0; i<N; ++i)
- {
- dist[i] = -1;
- mark[i] = false;
- }
- dist[1] = 0;
- mark[1] = true;
- int newP = 1; //初始节点
- for(i=1; i<n; ++i)
- {
- for(int j=0; j<vEdge[i].size(); ++j)
- {
- int t = vEdge[newP][j].node;
- int c = vEdge[newP][j].cost;
- if(mark[t]) continue;
- if(dist[t] == -1 || dist[newP] + c < dist[t])
- dist[t] = dist[newP] + c;
- }
- int min = 123123123;
- for(j=1; j<=n; ++j)
- {
- if(mark[i] || dist[j] == -1) continue;
- if(dist[j] < min)
- {
- min = dist[j];
- newP = j;
- }
- }
- mark[newP] = true;
- }
- cout<<dist[n]<<endl;
- }
- }
- //字符串替换函数
- void replace(char str[],char key[],char swap[])
- {
- int l1,l2,l3,i,j,flag;
- char tmp[100];
- l1 = strlen(str);
- l2 = strlen(key);
- l3 = strlen(swap);
- for(i=0; i<=l1-l2; ++i)
- {
- flag = 1;
- for(j=0; j<l2; ++j)
- if(str[i+j] != key[j])
- {
- flag = 0;
- break;
- }
- if(flag)
- {
- strcpy(tmp,str);
- strcpy(tmp+i,swap);
- strcpy(tmp+i+l3,str+l2+i);
- strcpy(str,tmp);
- i += l3-1; //不要忘了for循环中有++i操作
- l1 = strlen(str);
- }
- }
- }
- void main60()
- {
- char str[100] = "rei wreant treo replace re";
- char key[100] = "re";
- char swap[100] = "no";
- replace(str,key,swap);
- cout<<str<<endl;
- }
- int BFMatch(char *s,char *p)
- {
- int i=0,j;
- while(i<=strlen(s)-strlen(p))
- {
- j=0;
- while(s[i] == p[j] && j<strlen(p))
- {
- ++i;
- ++j;
- }
- if(j == strlen(p))
- return i-j;
- i = i-j+1;
- }
- return -1;
- }
- void main61()
- {
- char s1[100] = "i need to find a str in this";
- char s2[100] = "str";
- cout<<BFMatch(s1,s2)<<endl;
- }
- void getnext(char *p,int *next)
- {
- int j=0,k=-1;
- next[j] = k;
- while(j<strlen(p)-1)
- {
- if(k==-1 || p[j] == p[k])
- {
- ++k;
- ++j;
- next[j] = k;
- }else
- k = next[k];
- }
- }
- int KMPMatch(char *s,char *p)
- {
- int next[N];
- int i=0,j=0; //i用来遍历s主串,j用来遍历模式串
- getnext(p,next);
- while(i<strlen(s))
- {
- if(j==-1 || p[j] == s[i])
- {
- ++j;
- ++i;
- }else
- j = next[j];
- if(j == strlen(p))
- return i-j;
- }
- return -1;
- }
- void main62()
- {
- char ch[100] = "asbabcababa";
- char c[100] = "aba";
- // copy(next,next+100,ostream_iterator<int>(cout," "));
- cout<<KMPMatch(ch,c)<<endl;
- }
- struct Edge3
- {
- int node,cost,c;
- };
- vector<Edge3> vE[N];
- int cost1[N];
- void main63()
- {
- int n,m;
- int S,T;
- while(cin>>n>>m && n && m)
- {
- for(int i=0; i<N; ++i) vE[i].clear();
- while(m--)
- {
- int a,b,c,cost;
- cin>>a>>b>>c>>cost;
- Edge3 tmp;
- tmp.c = c;
- tmp.cost = cost;
- tmp.node = b;
- vE[a].push_back(tmp);
- tmp.node = a;
- vE[b].push_back(tmp);
- }
- cin>>S>>T;
- for(i=0; i<N; ++i)
- {
- mark[i] = false;
- dist[i] = -1;
- cost1[i] = 0;
- }
- int newP = S;
- mark[newP] = true;
- dist[newP] = 0;
- for(i=1; i<n; ++i)
- {
- for(int j=0; j<vE[newP].size(); ++j)
- {
- int t = vE[newP][j].node;
- int c = vE[newP][j].c;
- int co = vE[newP][j].cost;
- if(!mark[t])
- {
- if(dist[t] == -1 || dist[newP] + c < dist[t] || (dist[newP] + c == dist[t] && cost1[newP] + co < cost1[t]))
- {
- dist[t] = c+dist[newP];
- cost1[t] = co + cost1[newP];
- }
- }
- }
- int min = 123123123;
- for(j=1; j<=n; ++j)
- {
- if(!mark[j] && dist[j] != -1)
- {
- if(dist[j] < min)
- {
- newP = j;
- min = dist[j];
- }
- }
- }
- mark[newP] = true;
- }
- cout<<dist[T]<<" "<<cost1[T]<<endl;
- }
- }
- vector<int> iv[N]; //拓扑排序适合使用链表表示法
- queue<int> q;
- void main64()
- {
- int inDegree[N];
- int n,m;
- while(cin>>n>>m && n && m)
- {
- for(int i=0; i<N; ++i)
- {
- iv[i].clear();
- inDegree[i] = 0;
- }
- while(m--)
- {
- int a,b;
- cin>>a>>b;
- ++inDegree[b];
- iv[a].push_back(b); //有向树
- }
- while(!q.empty()) q.pop(); //清空
- for(i=0; i<n; ++i)
- if(inDegree[i] == 0)
- q.push(i);
- int cnt = 0;
- while(!q.empty())
- {
- int newP = q.front();
- q.pop();
- ++cnt;
- for(i=0; i<iv[newP].size(); ++i)
- {
- int k=iv[newP][i];
- if(--inDegree[k] == 0)
- {
- q.push(k);
- }
- }
- }
- if(cnt == n)
- cout<<"yes"<<endl;
- else cout<<"no"<<endl;
- }
- }