c_cpp 编程练习 Posted 2021-05-14
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 编程练习相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
strings = ['foobarbaz', 'a', '', 'this is a test']
reversed = [str[::-1] for str in strings]
print reversed
#!/usr/bin/env perl
use warnings;
use strict;
my @strings = qw(foobarbaz f 123);
foreach (@strings) {
print strrev($_) . "\n";
}
sub strrev {
my $str = shift;
my @chars = split('', $str);
my $rev = join('', reverse(@chars));
return $rev;
}
#!/usr/bin/env php
<?php
$strings = array(
'foo bar baz',
'',
'a',
'quickfox',
);
foreach ($strings as $str) {
echo _strrev($str) . "\n";
}
function _strrev($str)
{
$rev = '';
$i = strlen($str);
while ($i > 0) {
$rev .= $str[$i-1];
$i--;
}
return $rev;
}
#include <stdio.h>
int main()
{
/**
* 0b01111111000000000000000000000001
* 2130706433
* 0x7f000001
* 127.0.0.1
*/
int ip = 2130706433;
char *p = NULL;
p = (char *)&ip;
printf("dotted quad: %d.%d.%d.%d => %d\n", p[3], p[2], p[1], p[0], ip);
return 0;
}
#!/usr/bin/env perl
#
# when will baz($arr, 0, 0) == true ?
#
use strict;
use warnings;
my $arr = [
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0],
[2, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 3, 0, 0, 0, 0, 3, 0, 0, 7],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[2, 0, 1, 0, 0, 0, 0, 0, 9, 9],
[2, 0, 2, 1, 0, 0, 2, 0, 0, 0],
[4, 0, 3, 0, 0, 0, 0, 0, 0, 0],
[5, 0, 0, 0, 0, 0, 1, 0, 0, 0],
];
if (baz($arr, 0, 0)) {
print "baz() => True\n";
}
sub baz {
my ($arr, $x, $y) = @_;
my $result;
my $count = 0;
for (my $i = 0; $i < 10; $i++) {
if ($$arr[$i][$x] == 0) {
$count++;
}
}
$result = ($count == 10);
$count = 0;
for (my $i = 0; $i < 10; $i++) {
if ($$arr[$y][$i] == 0) {
$count++;
}
}
return ($result || ($count == 10));
}
/**
* long2ip
*/
#include <stdio.h>
int main()
{
int ip = 0b01111111000000000000000000000001; // dotted-quad 127.0.0.1 (for visualizing)
int a, b, c, d;
int mask = 0xFF;
a = (ip >> 24) & mask;
b = (ip >> 16) & mask;
c = (ip >> 8) & mask;
d = ip & mask;
printf("dotted quad: %d.%d.%d.%d\n", a, b, c, d);
return 0;
}
以上是关于c_cpp 编程练习的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 使用无序地图练习
c_cpp func指针练习
c_cpp 练习链表cpp
c_cpp C ++指针使用练习
c_cpp 练习最左边的c
c_cpp c ++中的模板练习