C语言程序设计:数组中查询数字

C语言程序设计:数组中查询数字,第1张

#include <iostream>

#include <time.h>

using namespace std

class point{

private:

int x

int y

public:

point(){

x=0

y=0

}

point(int x,int y){

this->x=x

this->y=y

}

void setxy(int x,int y){

this->x=x

this->y=y

}

int getx()const{return x}

int gety()const{return y}

}

void pring(int const se[][10]){

for(int i=0i<10i++){

for(int j=0j<10j++){

cout<<se[i][j]<<"\t"拍数

}

cout<<endl

}

}

int getint(int const se[][10],int uint,point po[]){

int sun=0

for(int i=0i<10i++){

for(int j=0j<10j++){

if(se[i][j]==uint){

po[sun++].setxy(i,j)

}

}

}

return sun

}

void main()

{

point po[100]//用链表最好,不过本人很懒,懒得写了

int sele[10][10]

for(int i=0i<10i++){

for(int j=0j<10j++){

sele[i][j]=rand()

}

}

//sele[6][2]=12316//如果随机数没有重复的,就在这手动改成几个重复的

int uint

pring(sele)

cout<<"请输入一个你要查询的数字"<<endl

cin>>uint

time_t timer,timer2

timer=time(NULL)

int sun=getint(sele,uint,po)

timer2=time(NULL)

if(sun){

cout<<"用户输入数字:"<<uint<<闷慧"在数组中的个数:"<<sun<<"在数组中的位置:"<<endl

for(int z=0z<sunz++){

cout<<"第"<<po[z].getx()<<"行,第"<<袭罩首po[z].gety()<<"列"<<endl

cout<<"查询结果花费:"<<difftime(timer,timer2)<<"秒"<<endl

//cout<<timer<<"---"<<timer2<<endl

}

}else{

cout<<"not found!"<<endl

}

}

本人能力有限,时间只能精确到秒。

#include <stdio.h>消丛

#include <string.h>

typedef char* String

String names[7] = {"Allen", "Bob"}

int query(char const *name){

int index = 0

while (*(names + index)){

        //如果字符串相等就意味查询到结果,break退出循环

if (strcmp(*(names + index), name) == 0){

break

}

index++

}

return index

}

int main(){

int index = query("Bob")

printf("%i\n", index)

}

使用顺序查找的方法,看程序!

运或陵行结果,查询Bob,Bob的下标为1,返回正确!拿团樱


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12488884.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-25
下一篇2025-08-25

发表评论

登录后才能评论

评论列表(0条)

    保存