Nvidia 2019 笔试题
## 源代码 ```perl #!/usr/bin/perlopen(IN,"<nv_data1.txt") or die "File does not exist!";
while (
chomp; #删除
print("(_
");
if()i==0){next}; #跳过第一行
((power,)area) = calc($_);#统计当前行单元的面积和功耗
$total_power += $power; #统计总面积和功耗
$total_area += $area;
if(/^ALU/i){
$alu_power += $power;
}
elsif(/^RAM/i){
$ram_area += $area;
}
}continue{
$i++;
}
功耗和面积计算函数
sub calc{
my (string = scalar()_);
@tmp = split(/s+/,$string); #根据空格划分单元格内容
return ($tmp[1]*$tmp[3], $tmp[1]*$tmp[2]);
}
close IN;
输出
print("Total area is $total_area
");
print("Total power is $total_power
");
print("RAM area is $ram_area
");
print("ALU power is $alu_power
");
$RAM_area_percentage = (ram_area/)total_area100;
$ALU_power_percentage = (alu_power/)total_power100;
print("The percentage of RAM area is $RAM_area_percentage %
");
print("The percentage of ALU power is $ALU_power_percentage %
");
## 程序输出结果
Name: Instance_number Area-per-instance Power-per-instance
ALU-Adder: 32 945 0.0333
ALU-Multiplier 16 6347 0.2235
RAM-Small 8 2239 0.0542
RAM-Large 2 10943 1.9312
Control 1 8345 0.2344
Data-pipe 1 10123 1.3423
Processor 1 24231 1.0212
Total area is 214289
Total power is 11.5355
RAM area is 39798
ALU power is 4.6416
The percentage of RAM area is 18.5721152275665 %
The percentage of ALU power is 40.2375276320922 %