代码总结二

#include <bits/stdc++.h> 
using namespace std;
int main()
{
  map<string,string>u;
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
  {
    string str1,str2;
    char a;
    cin>>a>>str1>>str2;
    if(a=='L')
    {
      if(u.find(str1)==u.end())//用map或者set的目的就是利用find的查询方法,set需要用interator进行查找
      cout<<"ERROR: Not Exist"<<endl;
      else if(u[str1]!=str2)
      cout<<"ERROR: Wrong PW"<<endl;
      else
      cout<<"Login: OK"<<endl;
    }
    else
    {
      if(u.find(str1)!=u.end())
      cout<<"ERROR: Exist"<<endl;
      else
      {
        cout<<"New: OK"<<endl;
        u[str1]=str2;//map的键值对的构建 
      }
    }
  }
  return 0;
}
 1 #include <iostream>
 2  
 3 using namespace std;
 4 #define N 13
 7 int main()
 8 {
 9     int map[N][N]; //邻接矩阵
10     // 初始化矩阵的值全部为0表示各个顶点间没有边连接
11     for(int i = 0; i <= N-1; i++){
12         for(int j = 0; j <= N-1; j++){
13             map[i][j] = 0;
14         }
15     }
16  
17     int a,b;  //定义两个变量,用来输入
18     int v,l;  //顶点数和边数
19  
20     cout << "请输入顶点数:";
21     cin >> v;
22     cout << "请输入边数:";
23     cin >> l;
24     cout << "请输入边:" << endl;
25  
26     for(int i = 1; i <= l; i++){
27             cin >> a >> b;
28             map[a][b] = 1; // 表示顶点a指向顶点b的边
29     }
30     cout << "邻接矩阵如下所示\n" << endl;
31     for(int i = 1; i <= N-1; i++){
32         for(int j = 1; j <= N-1; j++){
33             cout << map[i][j];
34         }
35         cout << endl;
36     }
37  
38     int k; //用于计算度数
39     int ID[N]; //用于存放入度
40     for(int i = 1; i <= v; i++){  // 计算入度
41         k = 0;
42         for(int j = 1; j <= v; j++){
43             if(map[j][i] == 1) //如果顶点j到顶点i有边,则顶点i的入度+1
44                 k++;
45         }
46         ID[i] = k;//存储每个顶点的入度 
47     }
48     //1、在有向图中选一个没有前驱的顶点并且输出
49     cout << "\n\n拓扑序列: ";
50     int count = 0;   //最后用来判断是否所有的顶点输出
51     while(1){
52         for(int i = 1; i <= v; i++){
53             if(ID[i] == 0){
54                 cout << i << " ";  //输出顶点
55                 count++;
56                 //2、从图中删除该顶点和所有以它为尾的弧,即删除所有与它有关的边。
57                 ID[i] = -1; //将此顶点入度设为-1,表示删除
58                 for(int j = 1; j <= v; j++){
59                     if(map[i][j] == 1){     //如果顶点j与顶点i有边,则删除这条边,并且顶点j的入度-1
60                         ID[j]--;//这个顶点的入度减一 
61                     }
62                 }
63             }
64         }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
65         
66         if(count == v){
67             break;  
68         }
69     }
70     return 0;
71 }
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    queue<int>q;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        q.push(a);
    }
    int cn=0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        if(q.empty())
        break;
        int y=q.front();
        if(abs(x-y)!=1)
        q.front()=min(x,y);
        if(abs(x-y)==1)
        {
            cn++;
            q.front()=min(x,y);
        }
    }
    cout<<cn;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/chuxinbubian/p/12222482.html