OJ系列

1001 难度 1 输入问题,还不能解决如何使得input的参数个数随意改变,只能先用vector动态存储,算法很简单

#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define MAX 10

int zero_count(int m, int n, int a[][MAX], int b[][MAX], int valid_column[])
{
	
	int count = 0;
	bool valid_row_flag = true;
	for (int i = 0; i < m; i++)
	{
		int j;
		for (j = 0; j < n; j++)
			if (a[i][j] + b[i][j] != 0)
			{
				valid_column[j] = 0;						//this is a invalid row
				valid_row_flag = false;
			}
		if (valid_row_flag)
			count++;	
	}
	for (int j = 0; j < n; j++)
		if (valid_column[j] != 0)
			count++;
	return count;
	
}
int main()
{
	int a[MAX][MAX];
	int b[MAX][MAX];
	int valid_column[MAX];
	int m, n;
	vector<int> parameters;
	int num;
	while (scanf("%d", &num) != EOF)
	{
		parameters.push_back(num);
	}
	while (parameters[0] != 0)
	{
		m = parameters[0];
		n = parameters[1];
		memset(valid_column, 1, MAX * sizeof (int));
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
				scanf("%d", &a[i][j]);
		}
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
				scanf("%d", &b[i][j]);
		}
		
		printf("%d", zero_count(m, n, a, b, valid_column));
		printf("\n");
	}	
}

1005 结构体 用rear很巧妙


#include <iostream>
#include <algorithm>

using namespace std;
/*定义学生类,class和struct其实一个样,可以直接变为结构体*/
class Student
{
public:
        int id;
        int ge;
        int gi;
        int choice[5];
};
/*定义学校类*/
class School
{
public:
        int quota;
        Student std[100];
        int front,rear;
};
/*学生成绩排序函数*/
bool cmp(Student a,Student b)
{
        if(a.ge+a.gi!=b.ge+b.gi )
                return a.ge+a.gi>b.ge+b.gi;
        else
                return a.ge>b.ge;
}
/*学生id排序函数*/
bool cmpa(Student a,Student b)
{
        return a.id<b.id;
}

int main()
{
        int n,m,k;
        int i,j;
        while(cin>>n>>m>>k)
        {
                Student std[100];
                School sch[100];
                /*学校信息录入*/
                for(i=0;i!=m;i++)
                {
                        cin>>sch[i].quota;
                        sch[i].front=0;
                        sch[i].rear=0;
                }
                /*学生信息录入*/
                for(i=0;i!=n;i++)
                {
                        cin>>std[i].ge>>std[i].gi;
                        for(j=0;j!=k;j++)
                                cin>>std[i].choice[j];
                        std[i].id=i;
                }
                /*学生成绩排序*/
                sort(std,std+n,cmp);
                /*按学生成绩从高到低分配学校*/
                for(i=0;i!=n;i++)
                {
                        /*按志愿先后选择学校*/
                        for(j=0;j!=k;j++)
                        {
                                /*学校还有名额时,直接入该学校的队列*/
                                if(sch[std[i].choice[j]].quota>0)
                                {
                                        sch[std[i].choice[j]].std[sch[std[i].choice[j]].rear++]=std[i];        
                                        sch[std[i].choice[j]].quota--;
                                        break;
                                }
                                /*学校没有名额时,成绩是否与最后一名录取的学生相同,同则进*/
                                else if(std[i].ge==sch[std[i].choice[j]].std[sch[std[i].choice[j]].rear-1].ge
                                        &&std[i].gi==sch[std[i].choice[j]].std[sch[std[i].choice[j]].rear-1].gi)
                                {
                                        sch[std[i].choice[j]].std[sch[std[i].choice[j]].rear++]=std[i];        
                                        sch[std[i].choice[j]].quota--;
                                        break;
                                }
                        }
                }
                /*输出每个学校录取者的id*/
                for(i=0;i!=m;i++)
                {
                        if(sch[i].front==sch[i].rear)
                                cout<<endl;
                        else
                        {
                                /*学生id排序*/
                                sort(sch[i].std,sch[i].std+sch[i].rear,cmpa);
                                cout<<sch[i].std[sch[i].front++].id;
                                while(sch[i].front!=sch[i].rear)
                                        cout<<' '<<sch[i].std[sch[i].front++].id;
                                cout<<endl;
                        }        
                }

        }
        return 0;
}

1008 最短路径, dijkstra算法,难度1,但是要注意在每次使用指针时都要检查,尤其是在循环条件中,开始循环,和开始下一次循环都要注意,使用adj,用节点作为关键字索引

#include <iostream>
#define MAX_NODE 1000
#include <vector>
#include <fstream>
#define INF 100
using namespace std;

struct Edge
{
	int fr_id, to_id;
	int cost, dist;
	int dist_source;
	Edge *next;
	Edge(int fr, int to, int co, int di, int di_so, Edge* n)
	{
		fr_id = fr;
		to_id = to;
		dist = di;
		cost = co;
		dist_source = di_so;
		next = n;
	}
};

struct Node
{
	int id;
	Edge *firstEdge;
};

int short_path(vector<Edge*> v, vector<int> &pv, int s, int d)
{
	Edge *p = v[s];											//find the first edge of source
	int min = p->dist;										
	int index = p->to_id;									
	while (p)
	{
		if (p->dist < min)
		{
			min = p->dist;
			index = p->to_id;
		}
		p = p->next;                                        //find the shortest path from source
	}															
	int distance = min;							            //distance now
	pv[index] = s;	
															//record index's parent
	while (1)									
	{	
		if (index == d)										//check if it is the destination
			break;
		p = v[index];										//find the shortest pathe from index
		if (p == 0)
			break;
		min = p->dist_source;								
		int min_index = p->to_id;
		while (p)
		{
			//find the less dist to source
			if (p->dist + distance < p->dist_source)		//update each node's distance to the source, for we don't know which is the end
			{
				p->dist_source = p->dist + distance;
				if (p->dist_source < min || (p->dist_source == min && p->cost < v[min_index]->cost))
				{
					min = p->dist_source;
					min_index = p->to_id;
				}
			}	
			p = p->next;
		}
		pv[min_index] = index;
		index = min_index;
	}
	return v[pv[d]]->dist_source;
}


int main()
{
	int nodes, edges;
	if(cin >> nodes >> edges)
	{
		//if (nodes == 0 && edges == 0)
			//break;
		vector<Edge*> a(nodes);
		vector<int> parent(nodes);
		int from, to, dist, cost;
		while (edges--)
		{
			cin >> from >> to >> dist >> cost;
			if (a[from] == 0)
			{
				Edge *edge = new Edge(from, to, cost, dist, INF, NULL);
				a[from] = edge;
			}
			else
			{
				Edge *edge = new Edge(from, to, cost, dist, INF, a[from]);
				a[from] = edge;
			}
		}
		
		int sour, dest;
		cin >> sour >> dest;
		cout << "shortest path is " << short_path(a, parent, sour, dest) << endl;
		int pp = parent[dest];
		cout << dest;
		while (pp != sour)
		{
			cout << "<-" << pp;
			pp = parent[pp];
		}
		cout << "<-" << sour << endl;
	}

}



Supongo que te gusta

Origin blog.csdn.net/juttajry/article/details/51265538
Recomendado
Clasificación