zoukankan      html  css  js  c++  java
  • C/C++ Find the missing number

    题目描述

    There are five numbers 1,2,3,4 and 5,now you can see four of them,do you know what is the remaining one?

    输入格式

     Four different  integers in range 1 to 5.

    输出格式

     One line,the missing number.

    样例输入
    将样例输入复制到剪贴板
    2 4 1 5
    样例输出
    3

    代码:
    #include <iostream> 
    using namespace std;
    
    int getMissNo(int a[])
    {
    	for(int j = 1,i = 0; j <= 5;)
    	{
    		if(j != a[i] && i >= 4)
    		{
    			return j;
    		}
    		if(j == a[i])
    		{
    			j++;
    			i = 0;
    		}	
    		if(j != a[i] && i < 4)
    			i++;
    	}
    }
    
    int main()
    {
    	int a[4];
    	for(int i = 0; i <= 3; i++)
    	{
    		cin>>a[i];
    	}
    	int miss = getMissNo(a);
    	cout<<miss<<endl;
    	
    	return 0;
    }


    我这里采用的是遍历的方法,也就是先设置好一个包含1到5所有数的数组(用循环遍历的方式代替数组),让输入的数组a[]中的每一个值与之一一比较,如果发现在a[]数组所有的数中都找不到匹配,那此时遍历到的数即是丢失了的数。
  • 相关阅读:
    PyQt4信号与槽
    Amazon Redshift数据库
    NoSQL数据库的认识
    如何划分子网
    VPC见解
    Linux之添加交换分区
    MySQL基础之 标准模式通配符
    MySQL基础之 LIKE操作符
    MySQL基础之 AND和OR运算符
    MySQL基础之 索引
  • 原文地址:https://www.cnblogs.com/lvlang/p/10586513.html
Copyright © 2011-2022 走看看