试题 基础练习 查找整数

总结

  • 整型和浮点型数组没有结束符,不能用’\0’
别人的代码
#include<iostream>
 
using namespace std;
 
int main(){
	int n,a,num[1000],seat=-1;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>num[i];
	}
	cin>>a;
	for(int j=0;j<n;j++){
		if(num[j]==a){
			seat=j+1;
			break;
		}
	}
	cout<<seat;
	return 0;
}
  • 问题代码
#include <iostream>
using namespace std;

int main() {
    int temp[1010] = {0}, n, m;
    scanf("%d",&n);

    for (int i = 0; i < n; ++i) {
        scanf("%d",&temp[i]);

    }
    scanf("%d",&m);
    temp[n] = '\0';
    int i = 0;

    while (*(temp + i) != '\0') {
        int num = *(temp + i);

        if (num == m) {
            printf("%d\n",++i);

            break;

        } else if(*(temp + i + 1) == '\0'){
            printf("-1\n");
            break;
        } else{
            i++;
        }
    }
    return 0;
}
  • 测试数据
300
38 92 38 79 90 57 58 91 15 88 56 11 2 34 72 55 28 46 62 86 75 33 69 42 44 16 81 98 22 51 21 99 57 76 92 89 75 12 0 10 3 69 61 88 1 89 55 23 2 85 82 85 88 26 17 57 32 32 69 54 21 89 76 29 68 92 25 55 34 49 41 12 45 60 18 53 39 23 79 96 87 29 49 37 66 49 93 95 97 16 86 5 88 82 55 34 14 1 16 71 86 63 13 55 85 53 12 8 32 45 13 56 21 58 46 82 81 44 96 22 29 61 35 50 73 66 44 59 92 39 53 24 54 10 45 49 86 13 74 22 68 18 87 5 58 91 2 25 77 14 14 24 34 74 72 59 33 70 87 97 18 77 73 70 63 68 92 85 2 80 13 27 2 99 27 25 43 24 23 72 61 81 3 32 5 93 25 31 92 42 22 86 64 0 87 60 13 74 70 70 35 33 11 60 96 67 85 50 40 94 95 24 19 25 76 94 58 2 71 66 78 93 51 84 18 64 19 52 0 87 60 26 10 57 70 15 76 27 43 58 64 9 82 86 65 87 77 74 25 27 29 28 23 20 2 62 23 96 37 61 95 25 64 60 2 16 30 26 11 71 11 47 53 20 90 24 88 63 40 51 62 29 0 13 58 78 65 7 77 0 58 39 3 60 57 24 77 8 13 87 
29
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彩虹编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值