华为机考题 | 多行输入输出的乘积

2020-02-26  本文已影响0人  金融测试民工

题目描述

    给定多行的输入,每行两个数字num1和num2,num1与num2均为大于等于0的正整数,需要返回每行num1与num2的乘积。

示例 1:

输入: 

0 1

9988 8899

输出: 

0

88883212

解题思路:

    判题系统的输入输出,对于这种有函数定义的题目,你只要完成函数,返回相关的值就可以,不需要处理任何输入输出,不要在函数里输出任何东西。

python3 的实现:

法一:

import sys 

for line in sys.stdin:

    a =line.split()

 print(int(a[0]) * int(a[1]))

法二:

import sys

while True:

    line = sys.stdin.readline()

    if not line:

        break

    a, b = (int(x) for x in line.split())

    print(a * b)

python2.7 的实现:

import sys

try:

    whileTrue:

        line = sys.stdin.readline().strip()

        if line == '':

            break

        lines = line.split()

        print int(lines[0]) * int(lines[1])

except:

    pass

C的实现: 64位输出请用printf("%lld")

#include <stdio.h>

int main() {

    int a,b;

    while(scanf("%d %d",&a, &b) != EOF)//注意while处理多个case

        printf("%d\n",a*b);

    return0;

}

C++ 的实现:64位输出请用printf("%lld")

#include <iostream>

using namespace std;

int main() {

    int a,b;

    while(cin >> a >> b)//注意while处理多个case

        cout << a*b << endl;

}

JAVA的实现,注意类名必须为Main, 不要有任何package xxx信息

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner in = newScanner(System.in);

        while(in.hasNextInt()) {//注意while处理多个case              int a = in.nextInt();

      int b = in.nextInt();

            System.out.println(a * b);

        }

    }

}

Php:

<?php  

    while(fscanf(STDIN, "%d %d", $a, $b) == 2)  

 echo($a* $b)."\n";

C#:

public class Program {

  public static void Main() {

    string line;

    while((line = System.Console.ReadLine ()) != null) {//注意while处理多个case

      string[] tokens = line.Split();

      System.Console.WriteLine(int.Parse(tokens[0]) * int.Parse(tokens[1]));

    }

  }

}

上一篇下一篇

猜你喜欢

热点阅读