【发布时间】:2017-03-15 19:16:24
【问题描述】:
我正在尝试将标准输入的 C++ 程序调试为
2 1 /newline/
4
调试器的输出(onlinegdb):-
当第二行被读取以获取问题提供的另一组输入时;这一次,向量 arr 没有显示输出 4
设置参数
从 /home/a.out 读取符号...完成。
(gdb) 继续
程序没有运行。
(gdb) 运行
Starting program: /home/a.out </home/input.txt
2 1
P.S. The program successfully compiles and runs for user provided input
3 2
3
4
附:那么,为什么程序中的第一个 for 循环没有正确执行?
源代码-
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int m;
cin>>m;
int t;
cin>>t;
cout<<m<<" "<<t<<"\n";
int sum=0;
vector<int> arr(t);
for(int i=0;i<t;i++)
cin>>arr[i]; //arr[i]=k digit nos
for(int i=0;i<t;i++)
cout<<arr[i]<<" ";
int comb(int a, int b); //6,2
for(int i=0;i<t;i++)
{
if(m==arr[i]){
cout<<m<<" a "<<9*pow(10,m-1)<<"\n";
}
if(m>arr[i]){
cout<<m<<" b "<<9*pow(10,m-1)<<"\n";
}
if(m<arr[i]){
/*if(m==arr[i]-1){
cout<<m<<" c "<<9*pow(10,arr[i]-1)-9<<"\n";
}
else{*/
sum=pow(10,arr[i])-1;
for(int j=arr[i]-1;j>m;j++)
sum=sum-comb(arr[i],j)*pow(9,arr[i]-j+1);
cout<<" d "<<sum<<"\n";
//}
}
}
//for m upto 10^4, k<=10^5; else k=10^5
return 0;
}
int comb(int a, int b) { //6,2
int j=1;
int s=a;
for(int k=a-1;k>b;k--) //5,4,3
s=s*k;
for(int l=1;l<=b;l++)
s=s/l;
return s;
}
//It is a problem involving displaying number of integers with no digit //repeating more than m (=2 here) times for t(=1) inputs with each input //being a arr[i] (=4) digit number.
【问题讨论】:
标签: c++ algorithm vector runtime-error c++14