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[]数组所有的数中都找不到匹配,那此时遍历到的数即是丢失了的数。
  • 相关阅读:
    css世界六
    电子书
    es 浏览器支持情况地址
    mac 下载文件的一些地址
    NODE_ENV production / development
    css世界五
    css世界四
    css世界三
    css世界二
    关于递归算法
  • 原文地址:https://www.cnblogs.com/lvlang/p/10586513.html
Copyright © 2011-2022 走看看