Edited by author 05.12.2007 18:27 My best is 0.015 104 KB My best 0.001 How do you get 0.001 sec? I'm quite sure there's nothing to improve in my code. I get 0.015 sec using FreePascal. Where do I go wrong?? Как Вы достигаете 0.001 секунды? Я не вижу где можно улучшить код для FreePascal. Есть идеи? var c: array[32..122] of byte; n, i, sum: integer; s: string; begin c[32] := 1; c[33] := 3; c[44] := 2; c[46] := 1; c[121] := 1; c[122] := 2; for i := 97 to 120 do c[i] := (i - 97) mod 3 + 1; readln(s); n := length(s); sum := 0; for i := 1 to n do sum := sum + c[byte(s[i])]; writeln(sum); end. 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); 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 символов. 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; } 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); } } } 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 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) 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 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; } } #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; } Пример в задаче неправильный. Исправьте стоимость на 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. here's my code,what's wrong with it,i get WA on test#8. #include <iostream> using namespace std; int main(){ char str[1000]; int sum = 0; for(int i = 0; str[i] != '\0'; ++i){ if(str[i] >= 'a' && str[i] <= 'z'){ sum += (str[i] - 97) % 3 + 1; }else if(str[i] == ' ' || str[i] == '.'){ ++sum;
}else if(str[i] == '!'){ sum += 3; }else if(str[i] == ','){ sum += 2; } } cout << sum; return 0; } what s wrong this time ? anybody ? #include<iostream> #include<string> using namespace std; char napis[1001]={0}; char tab[35]={0}; int tab1[35]={0}; int main(){ tab[0]=*"a"; tab1[0]=1; tab[1]=*"b"; tab1[1]=2; tab[2]=*"c"; tab1[2]=3; tab[3]=*"d"; tab1[3]=1; tab[4]=*"e"; tab1[4]=2; tab[5]=*"f"; tab1[5]=3; tab[6]=*"g"; tab1[6]=1; tab[7]=*"h"; tab1[7]=2; tab[8]=*"i"; tab1[8]=3; tab[9]=*"j"; tab1[9]=1; tab[10]=*"k"; tab1[10]=2; tab[11]=*"l"; tab1[11]=3; tab[12]=*"m"; tab1[12]=1; tab[13]=*"n"; tab1[13]=2; tab[14]=*"o"; tab1[14]=3; tab[15]=*"p"; tab1[15]=1; tab[16]=*"q"; tab1[16]=2; tab[17]=*"r"; tab1[17]=3; tab[18]=*"s"; tab1[18]=1; tab[19]=*"t"; tab1[19]=2; tab[20]=*"u"; tab1[20]=3; tab[21]=*"v"; tab1[21]=1; tab[22]=*"w"; tab1[22]=2; tab[23]=*"x"; tab1[23]=3; tab[24]=*"y"; tab1[24]=1; tab[25]=*"z"; tab1[25]=2; tab[26]=*"."; tab1[26]=1; tab[27]=*","; tab1[27]=2; tab[28]=*"!"; tab1[28]=3; tab[29]=*" "; tab1[29]=1;
int i=0,j=0,k=0,licznik=0; cin.getline (napis, 1000); k = strlen(napis); for(i=k-1;i>=0;i--){ for(j=0;j<30;j++){ if(napis[i]==tab[j]){ licznik+=tab1[j]; } } napis[i]=0; } cout << licznik; getchar(); return 0; } Change 1000 to 1001 !!!! salam navid,dasdet dard nakone AC gereftam!!!! cena. Thx, but i can not understand why this has helped) After two minutes I realized ... '\0' symbol :D Edited by author 12.10.2014 01:03 |
|