我正在尝试编写代码来确定是否可以通过仅从该数组中删除一个元素来获得严格递增的整数数组。
我的代码适用于 17 种情况中的 16 种,但无法想出一种方法来巧妙地重写我的代码,以便解决一个数字大于它之前的数字以及小于它之后的数字的情况我已经写了这个 for 循环。这是我的代码。这不起作用的情况是数组:[1, 2, 3, 4, 3, 6],因为它不认为数组中的最后 3 个作为我的 for 循环当前构造方式的违规者.
boolean almostIncreasingSequence(int[] sequence) {
int offenderPosition = 0;
int[] arrCopy = Arrays.copyOf(sequence, sequence.length);
boolean ordered = true;
//trying to neatly rewrite this for loop
for(int i= 0; i < sequence.length; i++){
if(i<sequence.length-1){
for(int j = i+1; j < sequence.length; j++) {
if(!(sequence[i] < sequence[j])){
ordered = false;
offenderPosition = i;
}
}
}
if(i == sequence.length-1){
if(!(sequence[i] > sequence[i-1])){
ordered = false;
offenderPosition …Run Code Online (Sandbox Code Playgroud) 因此,我要遍历C语言中文件I / O的一些代码,并在临时Cat函数的一行上感到困惑。
我主要对main的这一行感到困惑:
void filecopy(FILE *, FILE *);
Run Code Online (Sandbox Code Playgroud)
我们没有指定要传递给ifp和ofp的文件的名称,所以我不确定这行在做什么。
/* filecopy: copy file ifp to ofp */
void filecopy(FILE *ifp, FILE *ofp) {
int c;
while((c = getc(ifp)) != EOF){
putc(c, ofp);
}
}
/* cat: concatenate files, version 1*/
int main(int argc, char **argv) {
FILE *fp;
void filecopy(FILE *, FILE *);
if(argc == 1){ /*no args: copy standard input */
filecopy(stdin, stdout);
}else{
while(--argc > 0){
if((fp = fopen(*++argv, "r")) == NULL){
printf("cat: can't open %s\n", *argv); …Run Code Online (Sandbox Code Playgroud)