数字应输入为:
53,55,57,58,54
输出:
缺少的数字是56
输入应该能够与想要的数量一样多.
这是我到目前为止:
#include#include #include using namespace std; int getMissingNo (int a[], int n) { int i, total; total = (n+1)*(n+2)/2; for ( i = 0; i< n; i++) total -= a[i]; return total; } int main() { int a[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; cout << "Enter number of numbers in sequence: "; int numInSeq; cin >> numInSeq; cout << endl; cout << "Enter numbers in sequence: "; for (int i = 0; i < numInSeq; i++) { cin >> a[i]; } cout << endl; int miss = getMissingNo(a,numInSeq); cout << "Missing number: " << miss << endl << endl; return 0; }
我唯一缺少的是能够输入用逗号分隔的数字,我需要编辑getMissingNo
所以它可以是任何数字序列,而不仅仅是以1开头的数字序列.
如果你知道范围,简单的解决方案.
对于给定范围,让S为该范围内所有数字的总和.
对于具有缺失数字的给定数组,让MS为此数组中数字的总和.
缺少的数字是S - MS