📜  仅更改/添加一个字符并精确打印20次“ *”

📅  最后修改于: 2021-05-26 01:07:05             🧑  作者: Mango

在下面的代码中,仅更改/添加一个字符并精确打印20次“ *”。

int main()
{
    int i, n = 20;
    for (i = 0; i < n; i--)
        printf("*");             
    getchar();
    return 0;
}

解决方案:
1.在for循环的第三个表达式中将i替换为n

C
#include 
int main()
{
    int i, n = 20;
    for (i = 0; i < n; n--)
        printf("*");
    getchar();   
    return 0;
}


C++
#include 
using namespace std;
int main()
{
    int i, n = 20;
    for (i = 0; i < n; n--)
        cout << "*";
    getchar();
    return 0;
}


Java
// Java code
class GfG {
public static void main(String[] args)
{
    int i, n = 20;
    for (i = 0; i < n; n--)
        System.out.print("*");
}
}


Python3
# Python3 programe to implement 
# the above approach
if __name__ == '__main__':
  n = 20;
  for i in range(0, n):
    print("*"); n -= 1;
 
# This code is contributed by gauravrajput1


C#
// C# code
using System;
class GfG
{
    public static void Main()
    {
        int i, n = 20;
        for (i = 0; i < n; n--)
            Console.Write("*");
    }
}
 
// This code is contributed by SoumikMondal


C
#include 
int main()
{
    int i, n = 20;
    for (i = 0; -i < n; i--)
        printf("*");          
    getchar();   
    return 0;
}


C++
#include
using namespace std;
 
int main()
{
    int i, n = 20;
    for (i = 0; -i < n; i--)
        cout<<"*";          
      
    return 0;
}
 
// This code is contributed by rutvik_56.


Java
// Java code
import java.util.*;
public class GFG
{
  public static void main(String[] args)
  {
    int i, n = 20;
    for (i = 0; -i < n; i--)
      System.out.print("*");
  }
}
 
// This code is contributed by divyesh072019.


C#
// C# code
using System;
class GfG
{
    public static void Main()
    {
        int i, n = 20;
        for (i = 0; -i < n; i--)
            Console.Write("*");
    }
}
 
// This code is contributed by divyeshrabadiya07.


c
#include 
int main()
{
    int i, n = 20;
    for (i = 0; i + n; i--)
       printf("*");
    getchar();
    return 0;
}


c
#include 
int main()
{
    int i, n = 20;
    for (i = 0; ~i < n; i--)
        printf("*");
    getchar();
    return 0;
}


2.在for循环的第二个表达式中,在我之前加上“-”

C

#include 
int main()
{
    int i, n = 20;
    for (i = 0; -i < n; i--)
        printf("*");          
    getchar();   
    return 0;
}

C++

#include
using namespace std;
 
int main()
{
    int i, n = 20;
    for (i = 0; -i < n; i--)
        cout<<"*";          
      
    return 0;
}
 
// This code is contributed by rutvik_56.

Java

// Java code
import java.util.*;
public class GFG
{
  public static void main(String[] args)
  {
    int i, n = 20;
    for (i = 0; -i < n; i--)
      System.out.print("*");
  }
}
 
// This code is contributed by divyesh072019.

C#

// C# code
using System;
class GfG
{
    public static void Main()
    {
        int i, n = 20;
        for (i = 0; -i < n; i--)
            Console.Write("*");
    }
}
 
// This code is contributed by divyeshrabadiya07.

3.在for循环的第二个表达式中用<代替+

C

#include 
int main()
{
    int i, n = 20;
    for (i = 0; i + n; i--)
       printf("*");
    getchar();
    return 0;
}

让我们扩展一下这个问题。
仅更改/添加一个字符,并精确打印21次“ *”。
解决方案:将否定运算符放在i中for循环的第二个表达式中。
说明:负运算符将数字转换为其补码。

No.              One's complement
 0 (00000..00)            -1 (1111..11)                         
-1 (11..1111)             0 (00..0000)                        
-2 (11..1110)             1 (00..0001)                            
-3 (11..1101)             2 (00..0010)
...............................................
-20 (11..01100)           19 (00..10011)

C

#include 
int main()
{
    int i, n = 20;
    for (i = 0; ~i < n; i--)
        printf("*");
    getchar();
    return 0;
}

如果您找到上述问题的更多解决方案,请发表评论。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”