#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?
#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了
所以不用特別去記數