Se le da una array de strings str , la tarea es encontrar la puntuación de una string dada de la array. La puntuación de una string se define como el producto de la suma de los valores alfabéticos de sus caracteres con la posición de la string en la array.
Ejemplos:
Entrada: str[] = {“sahil”, “shashanak”, “sanjit”, “abhinav”, “mohit”}, s = “abhinav”
Salida: 228
Suma de valores alfabéticos de “abhinav” = 1 + 2 + 8 + 9 + 14 + 1 + 22 = 57
La posición de “abhinav” en str es 4, 57 x 4 = 228
Entrada: str[] = {“geeksforgeeks”, “algorithms”, “stack”}, s = “algorithms”
Salida: 244
Acercarse:
- Encuentre la string dada en la array y almacene la posición de la string.
- Luego calcule la suma de los valores alfabéticos de la string dada.
- Multiplique la posición de la string en la array dada por el valor calculado en el paso anterior e imprima el resultado.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find string score int strScore(string str[], string s, int n) { int score = 0, index; for (int i = 0; i < n; i++) { if (str[i] == s) { for (int j = 0; j < s.length(); j++) score += s[j] - 'a' + 1; index = i + 1; break; } } score = score * index; return score; } // Driver code int main() { string str[] = { "sahil", "shashanak" , "sanjit", "abhinav", "mohit" }; string s = "abhinav"; int n = sizeof(str) / sizeof(str[0]); int score = strScore(str, s, n); cout << score << endl; return 0; }
Java
// Java implementation of the approach import java.io.*; class GFG { // Function to find string score static int strScore(String str[], String s, int n) { int score = 0, index=0; for (int i = 0; i < n; i++) { if (str[i] == s) { for (int j = 0; j < s.length(); j++) score += s.charAt(j) - 'a' + 1; index = i + 1; break; } } score = score * index; return score; } // Driver code public static void main (String[] args) { String str[] = { "sahil", "shashanak" , "sanjit", "abhinav", "mohit" }; String s = "abhinav"; int n = str.length; int score = strScore(str, s, n); System.out.println( score); } } // This code is contributed by anuj_67..
Python3
# Python3 implementation of the approach # Function to find string score def strScore(str, s, n): score = 0 index = 0 for i in range(n): if (str[i] == s): for j in range(len(s)): score += (ord(s[j]) - ord('a') + 1) index = i + 1 break score = score * index return score # Driver code str = ["sahil", "shashanak", "sanjit", "abhinav", "mohit" ] s = "abhinav" n = len(str) score = strScore(str, s, n); print(score) # This code is contributed # by sahishelangia
C#
// C# implementation of the approach using System; class GFG { // Function to find string score static int strScore(String []str, String s, int n) { int score = 0, index = 0; for (int i = 0; i < n; i++) { if (str[i] == s) { for (int j = 0; j < s.Length; j++) score += s[j] - 'a' + 1; index = i + 1; break; } } score = score * index; return score; } // Driver code public static void Main (String[] args) { String []str = { "sahil", "shashanak", "sanjit", "abhinav", "mohit" }; String s = "abhinav"; int n = str.Length; int score = strScore(str, s, n); Console.Write( score); } } // This code is contributed by 29AjayKumar
PHP
<?php // Function to find string score function strScore($str, $s, $n) { $score = 0; $index; for ($i = 0; $i < $n; $i++) { if ($str[$i] == $s) { for ($j = 0; $j < strlen($s); $j++) $score += (ord($s[$j]) - ord('a')) + 1; $index = ($i + 1); break; } } $score = $score * $index; return $score; } // Driver code $str = array( "sahil", "shashanak", "sanjit", "abhinav", "mohit" ); $s = "abhinav"; $n = sizeof($str); $score = strScore($str, $s, $n); echo $score, "\n"; // This code is contributed by jit_t ?>
Javascript
<script> // javascript implementation of the approach // Function to find string score function strScore(str, s , n) { var score = 0, index=0; for (i = 0; i < n; i++) { if (str[i] == s) { for (j = 0; j < s.length; j++){ score += s.charAt(j).charCodeAt(0) - ('a').charCodeAt(0) + 1; } index = i + 1; break; } } score = score * index; return score; } // Driver code str = [ "sahil", "shashanak" , "sanjit", "abhinav", "mohit" ]; s = "abhinav"; var n = str.length; var score = strScore(str, s, n); document.write( score); // This code contributed by shikhasingrajput </script>
228
Publicación traducida automáticamente
Artículo escrito por SURENDRA_GANGWAR y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA