Page 3 a = input() s = 0 ll = ['c','f','i','l','o','r','u','x','!'] hh = ['b','e','h','k','n','q','t','w','z',','] pp = ['a','d','g','j','m','p','s','v','y','.',' '] for i in range(len(a)): if a[i] in ll: s+=3 elif a[i] in hh: s+=2 elif a[i] in pp: s+=1 print(s) #include <iostream> #include <string> using namespace std; int main() {string s; int o=0; getline(cin, s); string b1 = "adgjmpsvy. "; string b2 = "behknqtwz,"; string b3 = "cfilorux!"; for (int k=0; k<s.size();k++) { if (b1.find(s[k]) != string::npos) o++; if (b2.find(s[k]) != string::npos) o+=2; if (b3.find(s[k]) != string::npos) o+=3; } cout<<o; return 0; } use modulo for a-z: (ascii_char_code + 2) % 3 + 1, Nice idea, used it and it worked perfectly. Only 60 characters of code to AC. Идея замечательная, успешно применил ее в своей программе. Удалось уложиться в 60 символов. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace phrase { class Program { static int First(string input, string[]array) { int col = Convert.ToInt32(input.Length); if (col>0&&col<=1000) { int countArr = 0,countStr = 0,c = Convert.ToInt32(array.Length),count = 0,indexStr=0; for (; countArr < c; countArr++) { for (; (indexStr = input.IndexOf(array[countArr], countStr)) !=-1; countStr = indexStr + 1) { count++; } countStr = 0; } return count; } return 0; } static void Main(string[] args) { string[] one = new string[] { "a", "d", "g", "j", "m", "p", "s", "v", "y", ".", " " }; string[] two= new string[] { "b", "e", "h", "k", "n", "q", "t", "w", "z", "," }; string[] three = new string[] { "c", "f", "i", "l", "o", "r", "u", "x", "!" }; string input = Console.ReadLine(); int a = First(input,one); int b = First(input,two); int c = First(input,three); Console.WriteLine(a+(b*2)+(c*3)); } } } a=input() c=0 c+=a.count('a') c+=a.count('d') c+=a.count('g') c+=a.count('j') c+=a.count('m') c+=a.count('p') c+=a.count('s') c+=a.count('v') c+=a.count('y') c+=a.count('y') c+=a.count('.') c+=a.count(' ') c+=a.count('b')*2 c+=a.count('e')*2 c+=a.count('h')*2 c+=a.count('k')*2 c+=a.count('n')*2 c+=a.count('q')*2 c+=a.count('t')*2 c+=a.count('w')*2 c+=a.count('z')*2 c+=a.count(',')*2 c+=a.count('c')*3 c+=a.count('f')*3 c+=a.count('i')*3 c+=a.count('l')*3 c+=a.count('o')*3 c+=a.count('r')*3 c+=a.count('u')*3 c+=a.count('x')*3 c+=a.count('!')*3 print(c) dic={'a':1,'d':1,'g':1,'j':1,'m':1,'p':1,'s':1,'v':1,'y':1,'.':1,' ':1, 'b':2,'e':2,'h':2,'k':2,'n':2,'q':2,'t':2,'w':2,'z':2,',':2, 'c':3,'f':3,'i':3,'l':3,'o':3,'r':3,'u':3,'x':3,'!':3} m=input() total=0 for i in range(len(m)): total+=dic[m[i]] print(total) I got WA 1, but when I test this code myself, I always get a correct answer. What's wrong? #include <iostream> using namespace std; int main() { char speech[1001]; cin.getline(speech, 1001); int cost = 0; for (int i = 0; i < 1001; i++) { switch (speech[i]) { case 'a': case 'd': case 'g': case 'j': case 'm': case 'p': case 's': case 'v': case 'y': case '.': case ' ': cost += 1; break; case 'b': case 'e': case 'h': case 'k': case 'n': case 'q': case 't': case 'w': case 'z': case ',': cost += 2; break; case 'c': case 'f': case 'i': case 'l': case 'o': case 'r': case 'u': case 'x': case '!': cost += 3; break;
} } cout << cost << endl;
return 0; } Edited by author 29.08.2018 19:40 why are you iterating to 1001 all the time? maybe to string length? though looks fine with case, but nevertheless worth trying ll n, m, ans; st s; int main(){ while(cin >> s){ for(int i = 0;i < s.sz;++i){ if(s[i] == 'b' || s[i] == 'e' || s[i] == 'h' || s[i] == 'k' || s[i] == 'n' || s[i] == 'q' || s[i] == 't' || s[i] == 'w' || s[i] == 'z' || s[i] ==',') ans+=2; if(s[i] == 'a' || s[i] == 'd' || s[i] == 'g' || s[i] == 'j' || s[i] == 'm' || s[i] == 'p' || s[i] == 's' || s[i] =='v'|| s[i] == 'y' || s[i] == '.') ans+=1; if(s[i] == 'c' || s[i] == 'f' || s[i] == 'i' || s[i] == 'l' || s[i] == 'o' || s[i] == 'r' || s[i] == 'u' || s[i] == 'x'|| s[i] == '!') ans+=3; } ans++; } cout << ans - 1; }
using System; namespace SMSSpam { class Program { static void Main(string[] args) { string spam = Console.ReadLine(); Console.WriteLine(spamPrice(spam.ToLower())); Console.ReadKey(); } static int spamPrice (string spam) { int price = 0; foreach (char letter in spam) { price += LetterPrice(letter); } return price; } static int LetterPrice (char letter) { string charSet = "abcdefghijklmnopqrstuvwxyz*.,! "; return (charSet.IndexOf(letter) % 3) + 1; } } } #include <iostream> #include <string> #include <map> #include <vector> #include <cctype> #include <algorithm> #include <math.h> #include <iostream> using namespace std; int main() { int N = 0, k = 0; string s; getline (cin, s); char name; for (auto i = 0; i < (int)s.length(); i++) { name = s[i]; if (name == 'a' || name == 'd' || name == 'g' || name == 'j' || name == 'm' || name == 'p' || name == 's' || name == 'v' || name == 'y' || name == '.' || name == ' ') { k = 1; N += k; } if (name == 'b' || name == 'e' || name == 'h' || name == 'k' || name == 'n' || name == 'k' || name == 't' || name == 'w' || name == 'z' || name == ',') { k = 2; N += k; } if (name == 'c' || name == 'f' || name == 'i' || name == 'l' || name == 'o' || name == 'r' || name == 'u' || name == 'x' || name == '!') { k = 3; N += k; } } cout << N; return 0; } You forgot 'q' in the second if using System; using System.Linq; namespace Csharp_1567 { class Program { static void Main(string[] args) { string str1 = Console.ReadLine(); uint sum = 0; int lin = (from n in str1 select n).Count(); for (int i =0;i<lin;i++) { if (str1[i] == 'a' || str1[i] == 'd' || str1[i] == 'g' || str1[i] == 'j' || str1[i] == 'm' || str1[i] == 'p' || str1[i] == 's' || str1[i] == 'v' || str1[i] == 'y' || str1[i] == '_' || str1[i] == '.' || str1[i] == ' ') { sum += 1; continue; } else if (str1[i] == 'b' || str1[i] == 'e' || str1[i] == 'h' || str1[i] == 'k' || str1[i] == 'n' || str1[i] == 'q' || str1[i] == 't' || str1[i] == 'w' || str1[i] == 'z' || str1[i] == ',') { sum += 2; continue; } else sum += 3; } Console.WriteLine(sum); } } } This is my code!!! #include<iostream> #include<string> using namespace std; int main() { char s; int count = 0; while (cin >> s) { if ((s=='a')|| (s=='d')|| (s=='g')|| (s=='j')|| (s=='m')|| (s=='p')|| (s=='s')|| (s=='v')|| (s=='y')|| (s=='.')|| (s==' ')) {count+=1;} if ((s=='b')|| (s=='e')|| (s=='h')|| (s=='k')|| (s=='n')|| (s=='q')|| (s=='t')|| (s=='w')|| (s=='z')|| (s==',')) {count+=2;} if ((s=='c')|| (s=='f')|| (s=='i')|| (s=='l')|| (s=='o')|| (s=='r')|| (s=='u')|| (s=='x')|| (s=='!')) {count+=3;} } cout << count; system("pause"); return 0; } Possibly this part (s==' ')) If anyone is confused about this, note that cin ignores spaces. You should read the line as a whole with getline. string s; getline(cin, s); import java.util.Scanner; public class SMSspam2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s =in.nextLine(); char a[] = s.toCharArray();
int count = 0; for(int i=0;i<s.length();i++){ count = count+getCount(a[i]); } System.out.println(count); in.close(); } private static int getCount(char a) { int count =0; if (a == 'a' || a == 'd' || a == 'g' || a== 'j' || a== 'm' || a == 'p' || a == 's' || a== 'v' || a == 'y' || a== '.' || a== ' ') { count++; } if (a== 'b' || a == 'e' || a== 'h' || a == 'k' || a == 'n' || a == 'q' || a== 't' || a== 'w' || a== 'z' || a == ',') { count = count + 2; } if (a== 'c' || a== 'f' || a == 'i' || a == 'l' || a== 'o' || a== 'r' || a == 'u' || a== 'x' || a == '!') { count = count+ 3; } return count; } } Page 2 #include<iostream> #include<string.h> using namespace std; int main() { char s[1000]; long l=0; cin.getline(s, sizeof(s)); for(int i=1; i<=strlen(s); i++) switch (s[i]) { case 'a':l++; break; case 'd':l++; break; case 'g':l++; break; case 'j':l++; break; case 'm':l++; break; case 'p':l++; break; case 's':l++; break; case 'v':l++; break; case 'y':l++; break; case '.':l++; break; case 'b':l+=2; break; case 'e':l+=2; break; case 'h':l+=2; break; case 'k':l+=2; break; case 'n':l+=2; break; case 'q':l+=2; break; case 't':l+=2; break; case 'w':l+=2; break; case 'z':l+=2; break; case ',':l+=2; break; case ' ':l++; break; case 'c':l+=3; break; case 'f':l+=3; break; case 'i':l+=3; break; case 'l':l+=3; break; case 'o':l+=3; break; case 'r':l+=3; break; case 'u':l+=3; break; case 'x':l+=3; break; case '!':l+=3; break; default: l++; } cout << l; } char s[1000]; - change it on 1001 ( /0 is symbol ) #include <iostream> #include <string> #include <vector> #include <set> #include <queue> #include <map> #include <stack> #include <algorithm> #include <bitset> #include <cstring> #include <cmath> #include <cstdlib> #include <cstdio> #include <iomanip> #define F first #define S second #define ll long long #define len length() #define sqr(x) x*x #define pb push_back #define mp make_pair #define sz(x) ((int) (x).size()) #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() #define bp(x) __builtin_popcount(x) #define INF numeric_limits<long long int>::max() #define frp freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++) const int maxn = (int)1e6; const int mod = (int)1e9 + 7; using namespace std;
string s; int sum; main(){ getline(cin,s); for(int i=0; i < s.len; i++){ switch(s[i]){ case 'a':case'd':case 'g':case 'j':case 'm':case 'p':case 's':case 'v':case 'y':case '.':case ' ':sum+=1; break; case 'b':case'e':case 'h':case 'k':case 'n':case 'q':case 't':case 'w':case 'z':case ',':sum+=2; break; case 'c':case'f':case 'i':case 'l':case 'o':case 'r':case 'u':case 'x':case '!':sum+=3; break; } } printf("%d",sum);
return 0; } #include <iostream> #include <vector> using namespace std; int main(int argc, const char * argv[]) { char c; int total = 0; vector<char> ab1; vector<char> ab2; vector<char> ab3; for (char a = 'a'; a <= 'z'; a+= 3) { ab1.push_back(a); ab2.push_back(a + 1); ab3.push_back(a + 2); } ab1.push_back('.'); ab1.push_back('#'); ab2.push_back(','); ab2.push_back(' '); ab3.push_back('!');
while (cin.get(c)) {
if (find(ab1.begin(), ab1.end(), c) != ab1.end()) total += 1;
if (find(ab2.begin(), ab2.end(), c) != ab2.end()) total += 2;
if (find(ab3.begin(), ab3.end(), c) != ab3.end()) total += 3;
}
cout << total; } I've started learning C++ two weeks ago. And I can't read all the line with spaces. I include <istream>, but can't use "getline". Visual studio 2012 express. Пример в задаче неправильный. Исправьте стоимость на 111. Example isn't correct. Change cost from 114 to 111. Oh, how clever you are! 5000+ authors ACed this very difficult problem, and only you noticed the incorrect example. Are you counted it? ahhaha you are very patient It is correct. The answer is 114. import java.util.*; public class Test{ public static void main(String [] args){
Scanner scan = new Scanner(System.in); String s = scan.nextLine(); char c [] = s.toCharArray(); int sum = 0; for(int i=0;i<s.length();i++){ switch(c[i]){ case 'a':case'd':case 'g':case 'j':case 'm':case 'p':case 's':case 'v':case 'y':case '.':case ' ':sum=sum+1;break; case 'b':case'e':case 'h':case 'k':case 'n':case 'q':case 't':case 'w':case 'z':case ',':sum=sum+2;break; case 'c':case'f':case 'i':case 'l':case 'o':case 'r':case 'u':case 'x':case '!':sum=sum+3;break; } } System.out.println(sum); } } |
|