top of page
  • Photo du rédacteur현지 김

Exercice sur le boucle avec la syntaxe for



L’exercice publié sur le site (https://www.acmicpc.net/problem/25314) concernant sur le boucle.


It's Hyah's interview day. She's worked hard to prepare for the interview and answered the previous questions well, but now she's asked to code the last problem directly on the board. The problem was to add two numbers. She had been reading her C++ book and wrote a simple code on the board to add two numbers. After looking at the code, the interviewer asked the following question "If the inputs and outputs are integers of size $N$ bytes, how should you implement the program?" Hyah remembered something from her book about integer data types. It said that “long int” is an integer data type that can store up to a 4-byte integer, and “long long int” is an integer data type that can store up to an 8-byte integer. Hyah thought to herself "I wonder if each additional longin front ofint adds 4 more bytes of space, so “long long long int “must be an integer type that can store up to 12 bytes, and “long long long long int” can store up to 16 bytes!" With the interviewer's bewildered face in the background, Hyah began to write the integer types on the board.
What is the name of the integer type she wrote on the board because she thought it could store up to $N$ bytes of integers? The first line gives the integer N in the problem. (4<= N<= 1000; N is a multiple of 4) Print the name of the integer data type that Hyah thinks can store up to an N-byte integer.

Si vous saisissez 4, le programme sortira “long int”, si 20, il sortira “long long long long long int”


if($N>=4&&$N<=1000&&$N%4==0){
    $long="";
    for($i=1;$i<=$N/4;$i++){
        $long .= "long ";
    }
    
    echo trim($long)."int";
}

C’est ma solution.

$long="";

J’ai initialisé la valeur $long.

$long .="long";

.= signifie la concaténation, avec le boucle la chaîne de caractères “long” sera ajoutée. Si le boucle se répète deux fois, ça sortira “long long”.

echo trim($long)."int";

ici, j’ai utilisé la fonction trim() qui permet d’ajouter l’espace devant et derrière de la chaîne de caractères.

0 vue0 commentaire

Comments


bottom of page