#24409: 可以幫我找出哪裡有出錯的嗎?


s101401@cmes.tn.edu.tw (蒙均霖)

學校 : 不指定學校
編號 : 118294
來源 : [218.187.135.81]
最後登入時間 :
2022-02-28 12:58:02
a537. 10789 - Prime Frequency -- UVa10789 | From: [106.104.97.203] | 發表日期 : 2021-02-15 15:56

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

using namespace std;

int main(){

int a,cases=1;

string b,alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ",alpha1="abcdefghijklmnopqrstuvwxyz",number="0123456789";

cin>>a;

for (int i=1;i<=a;i++){

int num[100]={0},alpha2[100]={0},alpha3[100]={0};

string t="";

cin>>b;

for (int j=0;j<b.size();j++){

if (b[j]>='0' and b[j]<='9'){

for (int k=0;k<number.size();k++){

if (b[j]==number[k]){

num[k]++;

}

}

}

if (b[j]>='a' and b[j]<='z'){

for (int k=0;k<alpha1.size();k++){

if (b[j]==alpha1[k]){

alpha3[k]++;

}

}

}

if (b[j]>='A' and b[j]<='Z'){

for (int k=0;k<alpha.size();k++){

if (b[j]==alpha[k]){

alpha2[k]++;

}

}

}

}

int temp,start;

cout<<"Case "<<cases<<": ";

for (int j=0;j<number.size();j++){

int powers[100000]={0};

temp=num[j];

start=2;

if (num[j]>1){

int flag=0;

while (start<=num[j] and temp!=1){

while (temp%start==0){

powers[start]++;

temp/=start;

}

if (powers[start]>=2){

flag=1;

break;

}

start++;

}

if (flag==0)

t+=number[j];

}

}

for (int j=0;j<alpha.size();j++){

int powers[100000]={0};

temp=alpha2[j];

start=2;

if (alpha2[j]>1){

int flag=0;

while (start<=alpha2[j] and temp!=1){

while (temp%start==0){

powers[start]++;

temp/=start;

}

if (powers[start]>=2){

flag=1;

break;

}

start++;

}

if (flag==0)

t+=alpha[j];

}

}

for (int j=0;j<alpha1.size();j++){

int powers[100000]={0};

temp=alpha3[j];

start=2;

if (alpha3[j]>1){

int flag=0;

while (start<=alpha3[j] and temp!=1){

while (temp%start==0){

powers[start]++;

temp/=start;

}

if (powers[start]>=2){

flag=1;

break;

}

start++;

}

if (flag==0)

t+=alpha1[j];

}

}

if (t=="")

cout<<"empty\n";

else

cout<<t<<"\n";

cases++;

}

return 0;

}

這段code要怎麼修改才能AC?

 
#24730: Re:可以幫我找出哪裡有出錯的嗎?


abs71507@gmail.com (46 5)

學校 : 國立中興大學
編號 : 87742
來源 : [36.235.200.136]
最後登入時間 :
2023-05-24 14:06:42
a537. 10789 - Prime Frequency -- UVa10789 | From: [42.72.147.237] | 發表日期 : 2021-03-18 19:07

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

using namespace std;

int main() {

int a, cases = 1;

string b, alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", alpha1 = "abcdefghijklmnopqrstuvwxyz", number = "0123456789";

cin >> a;

for (int i = 1; i <= a; i++) {

int num[100] = { 0 }, alpha2[100] = { 0 }, alpha3[100] = { 0 };

string t = "";

cin >> b;

for (int j = 0; j < b.size(); j++) {

if (b[j] >= '0' and b[j] <= '9') {

for (int k = 0; k < number.size(); k++) {

if (b[j] == number[k]) {

num[k]++;

}

}

}

if (b[j] >= 'a' and b[j] <= 'z') {

for (int k = 0; k < alpha1.size(); k++) {

if (b[j] == alpha1[k]) {

alpha3[k]++;

}

}

}

if (b[j] >= 'A' and b[j] <= 'Z') {

for (int k = 0; k < alpha.size(); k++) {

if (b[j] == alpha[k]) {

alpha2[k]++;

}

}

}

}

int temp, start;

cout << "Case " << cases << ": ";

for (int j = 0; j < number.size(); j++) {

int powers[10000] = { 0 };

temp = num[j];

start = 2;

if (num[j] > 1) 

{

int flag = 0;

while (start <= num[j] and temp != 1) 

{

if(temp % start == 0&&temp!=start) 

{

flag = 1;

break;

}

start++;

}

if (flag == 0)

t += number[j];

}

}

for (int j = 0; j < alpha.size(); j++) 

{

int powers[10000] = { 0 };

temp = alpha2[j];

start = 2;

if (alpha2[j] > 1) 

{

int flag = 0;

while (start <= alpha2[j] and temp != 1) 

{

if (temp % start == 0 && temp != start)

{

flag = 1;

break;

}

start++;

}

if (flag == 0)

t += alpha[j];

}

}

for (int j = 0; j < alpha1.size(); j++) 

{

int powers[10000] = { 0 };

temp = alpha3[j];

start = 2;

if (alpha3[j] > 1) 

{

int flag = 0;

while (start <= alpha3[j] and temp != 1) 

{

if (temp % start == 0 && temp != start)

{

flag = 1;

break;

}

start++;

}

if (flag == 0)

t += alpha1[j];

}

}

if (t == "")

cout << "empty\n";

else

cout << t << "\n";

cases++;

}

return 0;

}

我只改了你判斷質數的部分

 

判斷質數只要有非0且非自身以外的數能整除時就可以直接break了

 

 

所以不用特別去記數

 
ZeroJudge Forum