OJ水题

1.已知含有10个整数的查找表如下:(9,13,15,7,45,32,56,89,60,36),从键盘上输入一个整数,用顺序查找的方法在查找表中查找该整数。若存在,输出该元素的下标值,否则,输出-1。

Input

输入一个整数

Output

如找到输出被找元素的下标值,否则,输出-1

Sample Input

56

Sample Output

index=7
AC代码:
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4 using namespace std;
 5 #define N 10
 6 int seqSearch(int array[],int k)
 7 {
 8     int i=N;
 9     array[0]=k;
10     while(array[i]!=k) i--;
11     return i;
12 }
13 int main()
14 {
15     int array[N+1]={0,9,13,15,7,45,32,56,89,60,36};
16     int a;
17     scanf("%d",&a);
18     int p=seqSearch(array,a);
19     if(p!=0) printf("index=%d\n",p);
20     else printf("index=-1\n");
21     return 0; 
22 }
View Code

2.搜索链表

 http://oj.jxust.edu.cn/contest/problem?id=1644&pid=1

Problem Description

从键盘上输入n个学生的信息:学号,姓名, 数学, 英语, 语文的成绩,用这些信息构建一个动态链表。再从键盘上输入一个学号, 用顺序查找的方法在链表中查找该学生的信息。若找到,输出该学生的信息,没有找到,输出No such person

Input

输入学生的信息:学号,姓名, 数学, 英语, 语文的成绩, 输入学号为0时终止输入;

再输入学号进行查询.

Output

若找到,输出该学生的信息,成绩保留一位小数,没有找到,输出No such person

Sample Input

20130610 Guojin 90.5 89.5 70
20140612 Huangrong 99.5 100 90.5
20150548 HuangYaoshi 89 78 56
0 0 0 0 0
20140612

Sample Output

20140612 Huangrong 99.5 100.0 90.5

开始用scanf和printf输入输出老错,我日,后来改成cin,cout输入输出就过了;
补充一个知识点,用cout输出浮点数(要求浮点数保留小数点后一位)方法:
1 #include <iomanip>
2 cout<<setiosflags(ios::fixed)<<setprecision(1);
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#define N 30
#include <iostream>
#include <iomanip>
using namespace std;
struct node{
	string sno;
	string sname;
	double math;
	double english;
	double chinese;
}student[N];

int main()
{
	string mysno,mysname;
	double mymath,myenglish,mychinese;
	int i=0,j;
	while(cin>>mysno>>mysname>>mymath>>myenglish>>mychinese,(mysno!="0"||mysname!="0"||mymath!=0||myenglish!=0||mychinese!=0))
	{
		student[i].sno=mysno;
		student[i].sname=mysname;
		student[i].math=mymath;
		student[i].english=myenglish;
		student[i].chinese=mychinese;
		i++;
	}
	int num=i;
	cin>>student[i].sno;
	for(j=0;j<num;j++)
	{
		if(student[j].sno==student[i].sno)
		{
			cout<<setiosflags(ios::fixed)<<setprecision(1);
		    cout<<student[j].sno<<" "<<student[j].sname<<" "<<student[j].math<<" "<<student[j].english<<" "<<student[j].chinese;
			break;
		 } 
	 }
	 if(j>=num) printf("No such person\n");
	 return 0; 
}

  



猜你喜欢

转载自www.cnblogs.com/jianqiao123/p/11915751.html